一、Zookeeper是什么?为何使用Zookeeper?
Zookeeper是一个分布式的服务框架,是树型的目录服务的数据存储,能做到集群管理数据 ,这里能很好的作为Dubbo服务的注册中心。
Dubbo能与Zookeeper做到集群部署,当提供者出现断电等异常停机时,Zookeeper注册中心能自动删除提供者信息,当提供者重启时,能自动恢复注册数据,以及订阅请求
二、创建MAVEN项目
主要分三大模块:
dubbo-api1 : 存放公共接口; 最终会打为jar包,供consumer和provider1引用。
dubbo-provider1 : 提供远程服务; 包含api1接口的实现类,提供服务的实现逻辑。
dubbo-api1 : 存放公共接口; 最终会打为jar包,供consumer和provider1引用。
dubbo-provider1 : 提供远程服务; 包含api1接口的实现类,提供服务的实现逻辑。
dubbo-consumer : 调用远程服务;
2.1 dubbo-api1 maven
项目只有一个文件,是一个接口,他定义了统一的接口让provider1和consumer来引用:
public interface UserService {
String sayHello(String name);
}
之后打包jar!
2.2 dubbo-provider1 maven
项目只有两个类,一个是服务实现类UserServiceImpl
package com.hlx.provider;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.alibaba.dubbo.rpc.RpcContext;
import com.hlx.service.UserService;
/**
* 实现业务接口
* @author Administrator
*
*/
public class UserServiceImpl implements UserService {
public String sayHello(String arg0) {
System.out.println("["
+ new SimpleDateFormat("HH:mm:ss").format(new Date())
+ "] Hello " + arg0 + ", request from consumer: "
+ RpcContext.getContext().getRemoteAddress());
return "Hello " + arg0 + ", response form provider: "
+ RpcContext.getContext().getLocalAddress();
}
}
Main测试
package com.hlx.provider;
/**
* 该类是dubbo框架提供,作用是启动dubbo服务,dubbo会在启动服务时,
* 读取classpath下一个名为dubbo.properties文件的属性值
*
* @author Administrator
*
*/
public class Main {
public static void main(String[] args) {
com.alibaba.dubbo.container.Main.main(args);
}
}
dubbo.properties文件配置:
dubbo.container=spring
dubbo.spring.config=classpath:dubbo-provider.xml
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
<?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 protocol="zookeeper" address="127.0.0.1:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 指定了集群容错模式,此处为快速失败 -->
<!-- 和本地bean一样实现服务 -->
<bean id="userService" class="com.hlx.provider.UserServiceImpl" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.hlx.service.UserService"
ref="userService" />
</beans>
pom.xml文件
<dependencies>
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.netty/netty -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
<version>3.7.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jline/jline -->
<dependency>
<groupId>jline</groupId>
<artifactId>jline</artifactId>
<version>0.9.94</version>
</dependency>
<!-- 自己写的公共接口 -->
<dependency>
<groupId>com.dubbo.demo</groupId>
<artifactId>dubbo-api1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
2.3 dubbo-consumer maven
UserController类就是控制器,使用requestMapping注解提供http接口服务
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/hello")
@ResponseBody
public String hello(String name) throws Exception {
System.out.println("controol==>");
return userService.sayHello(name);
}
}
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:p="http://www.springframework.org/schema/p"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="dubbo-consumer" />
<!-- 使用zookeeper分布式服务注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="userService" check="false" interface="com.hlx.service.UserService"/>
</beans>
spring-config.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="byName">
<!-- 开启注解配置 -->
<context:annotation-config/>
<import resource="classpath:dubbo-consumer.xml"/>
</beans>
spring-mvc.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- 配置自动扫描的包路径 -->
<context:component-scan base-package="com.hlx.controller" />
</beans>
web.xml配置文件
<!-- 配置Spring配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:spring-config.xml
</param-value>
</context-param>
<!-- 配置Spring上下文监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
配置pom.xml文件
<dependencies>
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
</dependency>
<!-- 自己写的公共接口 -->
<dependency>
<groupId>com.dubbo.demo</groupId>
<artifactId>dubbo-api1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- spring包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
</dependencies>
注意:provider1与consumer必须导入公共api1包哦!
(1)zookeeper服务启动起来。
(2)dubbo-provider1 服务器运行起来;
(3)dubbo-consumer 部署到Tomcat上!运行起来;
显示效果:
IE : http://localhost:8888/dubbo-consumer/hello.action?name=Mike