项目环境:
- IntelliJ IDEA
- tomcat8.5.38
- jdk1.8
- apache-zookeeper-3.5.5-bin.tar
一、安装zookeeper本地服务
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已经启动成功。
二、创建服务提供服务(provider)
0、项目结构图:
1、搭建springmvc项目环境
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.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>dubbo-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>dubbo-demo</name>
<url>http://maven.apache.org</url>
<properties>
<motan.version>0.3.0</motan.version>
<!-- 在阿里巴巴内部广泛使用的GA版本为: 2.4.9, 强烈推荐此版本 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>5.1.2.RELEASE</spring.version>
<jackson.version>2.9.7</jackson.version>
<shiro.version>1.4.0</shiro.version>
<junit>4.12</junit>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- spring相关 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring 升级4+ 依赖的JSON包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- aop -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
</dependencies>
</project>
applicationContext.xml:(扫描注解、引用服务配置dubbo-provider.xml)
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- 自动搜索@Component , @Service , @Repository等标注的类 不搜索@Controller的类 -->
<!-- 自动扫描组件,这里要把web下面的 controller去除,他们是在springMVC-servlet.xml中配置的,如果不去除会影响事务管理的。 -->
<context:component-scan base-package="com.test" annotation-config="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- RPC远程调用 -->
<import resource="dubbo-provider.xml" />
</beans>
springMVC-servlet.xml:(扫描controller,开启mvc注解)
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- 只搜索@Controller 标注的类 不搜索其他标注的类 -->
<context:component-scan base-package="com.test" use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 支持JSON -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!-- Json 转换配置 -->
<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-8</value>
</list>
</property>
</bean>
<bean class ="org.springframework.http.converter.StringHttpMessageConverter">
<property name ="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>
dubbo-provider.xml:(配置对外的服务接口)
<?xml version="1.0" encoding="UTF-8"?>
<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" />
<!-- 使用zookeeper注册中心暴露服务地址, 可配置集群逗号分隔 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" threads="10"/>
<!-- 声明需要暴露的服务接口 若comuser未设置,则默认使用provider配置:超时3s, 随机负债策略, 调用最大并发数200,则阻塞-->
<dubbo:service interface="com.test.service.DemoService" ref="demoService" version="1.0" timeout="3000" retries="2" loadbalance="random" executes="200"/>
</beans>
web.xml:(加载配置与监听)
<?xml version="1.0" encoding="UTF-8"?>
<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>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<!-- 字符集过滤器 -->
<filter>
<description>字符集过滤器</description>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- springmvc配置 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:springMVC-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-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;
}
}
三、创建调用服务(customer)
0、项目结构:
customer项目架构与provider项目架构一致,复制一下。删除DemoServiceImpl实现类,我们要的效果是customer的项目调用到provider实现类的接口。
然后将dubbo-provider.xml服务配置修改成dubbo-consumer.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"/>
<!-- 使用multicast广播注册中心暴露发现服务地址, 没有服务提供者则抛异常; 重启时,若注册中心,则从缓存文件读取服务列表-->
<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" check="true" file="${user.home}/output/dubbo.cache"/>
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<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服务接口单独用一个项目去实现,
然后提供者与消费者去继承这个公共接口类就行了,这样更容易管理。推荐
也可以把提供者项目的接口打包,引入到消费者系统中使用,缺点:麻烦,经常需要手动。不推荐
也可以用两个独立的项目,提供者与消费者心目的接口路径与名称需要一致(文章就是采用的这种),优点:容易理解,缺点:改动时要同步手动。不推荐。