1.通过链接下载对应的包 http://www.apache.org/dist/zookeeper/
选择:apache-zookeeper-3.5.5-bin.tar
2、解压后,conf里面,会看到zoo_sample.cfg文件。将zoo_sample.cfg改成bak文件(备份),并复制一个修改为zoo.cfg,修改相关配置内容,添加下面两行参数:
dataDir=D:\Sofeware\apache-zookeeper-3.5.5-bin\tmp
dataLogDir=D:\Sofeware\apache-zookeeper-3.5.5-bin\logs
注意:如果后面启动出现闪退情况,有可能是因为8080端口被占用,需要修改下admin端口,添加参数(端口不重复就行):admin.serverPort=8888
3、修改好配置文件后,双击zkServer.cmd,如果出现闪退了,看是否是上面8080端口被占用的问题不(可以用文本编辑下zkServer.cmd,在倒数第二行加一个 pause,可以控制启动失败时不闪退)。
4、启动,显示下图则表示 zookeeper已经启动成功。
pom.xml:(相关maven包)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.testgroupId>
<artifactId>dubbo-demoartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>pompackaging>
<name>dubbo-demoname>
<url>http://maven.apache.orgurl>
<properties>
<motan.version>0.3.0motan.version>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<spring.version>5.1.2.RELEASEspring.version>
<jackson.version>2.9.7jackson.version>
<shiro.version>1.4.0shiro.version>
<junit>4.12junit>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>3.8.1version>
<scope>testscope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>2.5.3version>
<exclusions>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>springartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>com.github.sgroschupfgroupId>
<artifactId>zkclientartifactId>
<version>0.1version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>${jackson.version}version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>${jackson.version}version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-annotationsartifactId>
<version>${jackson.version}version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjrtartifactId>
<version>1.6.11version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.6.11version>
dependency>
dependencies>
project>
applicationContext.xml:(扫描注解、引用服务配置dubbo-provider.xml)
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<context:component-scan base-package="com.test" annotation-config="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
context:component-scan>
<import resource="dubbo-provider.xml" />
beans>
springMVC-servlet.xml:(扫描controller,开启mvc注解)
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.test" use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
context:component-scan>
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
bean>
property>
bean>
property>
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=utf-8value>
list>
property>
bean>
<list>
<value>text/plain;charset=UTF-8value>
list>
property>
bean>
mvc:message-converters>
mvc:annotation-driven>
beans>
dubbo-provider.xml:(配置对外的服务接口)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbo_provider" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:protocol name="dubbo" port="20880" threads="10"/>
<dubbo:service interface="com.test.service.DemoService" ref="demoService" version="1.0" timeout="3000" retries="2" loadbalance="random" executes="200"/>
beans>
web.xml:(加载配置与监听)
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath*:applicationContext.xmlparam-value>
context-param>
<filter>
<description>字符集过滤器description>
<filter-name>encodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<servlet>
<servlet-name>springMVCservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath*:springMVC-servlet.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springMVCservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class>
listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListenerlistener-class>
listener>
web-app>
2、编写对外的RPC服务接口
DemoService:
package com.test.service;
/**
* 接口
*/
public interface DemoService {
String getMessage(String name);
}
DemoServiceImpl:
package com.test.service;
import org.springframework.stereotype.Service;
/**
* 服务实现类
*/
@Service("demoService")
public class DemoServiceImpl implements DemoService{
public String getMessage(String name) {
String msg = "hello "+name+" . i'm from provider !";
System.out.println(msg);
return msg;
}
}
0、项目结构:
customer项目架构与provider项目架构一致,复制一下。删除DemoServiceImpl实现类,我们要的效果是customer的项目调用到provider实现类的接口。
然后将dubbo-provider.xml服务配置修改成dubbo-consumer.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">
<dubbo:application name="dubbo_consumer"/>
<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" check="true" file="${user.home}/output/dubbo.cache"/>
<dubbo:reference id="demoService2" interface="com.test.service.DemoService" version="1.0" timeout="3000" retries="2" loadbalance="random"/>
beans>
customer项目中写一个controller类来测试一下:
rpcController:
package com.test.controller;
import com.test.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class rpcController {
@Autowired
private DemoService demoService;
@RequestMapping("/getMessage")
public String getName(){
return demoService.getMessage("jack");
}
}
我们看到customer项目调用本项目的DemoService成功的访问到了provider项目的DemoServiceImpl内容。说明我们成功RPC服务调用成功了。
我们也可以用maven的子模块模式把公共RPC服务接口单独用一个项目去实现,
然后提供者与消费者去继承这个公共接口类就行了,这样更容易管理。推荐
也可以把提供者项目的接口打包,引入到消费者系统中使用,缺点:麻烦,经常需要手动。不推荐
也可以用两个独立的项目,提供者与消费者心目的接口路径与名称需要一致(文章就是采用的这种),优点:容易理解,缺点:改动时要同步手动。不推荐。