Spring Boot+Dubbo+Zookeeper简单集成分布式
Spring Boot+Dubbo+Zookeeper简加粗样式单集成分布式
dubbo是阿里公司推出解决分布式服务问题的框架,是一个基于SOA面向服务体系结构的基础设施,提供了诸如服务发布注册、容错调用、部署、调用次数监控、每个服务的性能监控等很多功能。
Dubbo建议使用Zookeeper作为服务的注册中心。
下面是简单的实现Spring Boot+Dubbo+Zookeeper简单集成
1.首先在win或linux下安装zookeeper,这里以win环境下进行
安装Zookeeper. 在官网https://mirrors.cnnic.cn/apache/zookeeper/下载zookeeper.
把下载的zookeeper的文件解压到指定目录
D:\machine\zookeeper-3.3.6>
修改conf下增加一个zoo.cfg
内容如下:
# The number of milliseconds of each tick 心跳间隔 毫秒每次
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting anacknowledgement
syncLimit=5
# the directory where the snapshot isstored. //镜像数据位置
dataDir=D:\\data\\zookeeper
#日志位置
dataLogDir=D:\\logs\\zookeeper
# the port at which the clients willconnect 客户端连接的端口
clientPort=2181
注:如果启动有报错提示cfg文件有错误,可以用zoo_sample.cfg内内容替代也是可以的
进入到bin目录,并且启动zkServer.cmd,这个脚本中会启动一个java进程
D:\machine\zookeeper-3.3.6>cd bin
D:\machine\zookeeper-3.3.6\bin>
D:\machine\zookeeper-3.3.6\bin >zkServer.cmd
2.创建生产者及消费者demo
目录格式如下:
将必须的jar导入到dubbo及dubboapi的pom.xml中
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!--引入dubbo-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--引入zookeeper客户端-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
3.dubbo配置文件
分别在服务端和消费端下的resources下创建provider.xml及customer.xml
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="provider" />
<!--配置服务注册中心,dubbo不仅仅支持zookeeper-->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!--声明对外暴露的服务-->
<dubbo:service interface="com.example.dubbo.service.UserService" ref="UserService" />
<bean id="demoService" class="com.example.dubbo.service.impl.UserServiceImpl"></bean>
</beans>
customer.xml:这里配置和provider.xml的时候有点不一样,不用注入bean
<?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="customer" />
<!--配置服务注册中心,dubbo不仅仅支持zookeeper-->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!--声明服务引用,与服务声明接口类型一致-->
<dubbo:reference interface="com.example.dubbo.service.UserService" id="UserService" />
</beans>
4.编写测试类
这里的接口与实体类是服务端和客户端共同都有的
编写po:
package com.example.dubbo.po;
import lombok.Data;
import java.io.Serializable;
/**
* @Description :
* @Author : 苏俊强
* @Date : 2019/3/25
*/
@Data
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String password;
}
编写service
package com.example.dubbo.service;
import com.example.dubbo.po.User;
/**
* @Description :
* @Author : 苏俊强
* @Date : 2019/3/25
*/
public interface UserService {
User selectUserById(int id);
}
在服务端编写impl
package com.example.dubbo.service.impl;
import com.example.dubbo.po.User;
import com.example.dubbo.service.UserService;
import org.springframework.stereotype.Service;
/**
* @Description :
* @Author : 苏俊强
* @Date : 2019/3/25
*/
@Service("UserService")
public class UserServiceImpl implements UserService {
@Override
public User selectUserById(int id) {
System.out.println("这里是dubbo的服务端");
System.out.println("需要查询的用户为" + id);
return null;
}
}
在客户端编写controller
package com.example.dubbo.controller;
import com.example.dubbo.po.User;
import com.example.dubbo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description :
* @Author : 苏俊强
* @Date : 2019/3/25
*/
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("selectUserById/{id}")
public String selectUserById(@PathVariable("id") int id){
System.out.println(id);
User user= userService.selectUserById(id);
return "ok";
}
}
至于如何使用dubbo-admin 监控搭建,读者可参考https://blog.****.net/evankaka/article/details/47858707
源码下载https://github.com/su-junqiang/dubbo-zookeper-demo.git