Dubbo、Zookeeper整合分布式服务操作
Zookeepe负责服务的注册,Dubbo负责服务之间的通讯
主要步骤:
0.新建一个空项目
1.新建一个SpringBoot模块,导入SpringWeb依赖
2.导入以下依赖,因为Dubbo没有固定的生态,所以只能自己写,或者使用第三方库,dubbo需要依赖curator等包,而zkClient是一个第三方库
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.13</version>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
3.编写一个服务接口,服务实现
TicketService
package cn.steveyu.providerdemo.service;
public interface TicketService {
String getTicket();
}
TicketServiceImpl
package cn.steveyu.providerdemo.service;
import org.apache.dubbo.config.annotation.Service;
@Service
public class TicketServiceImpl implements TicketService {
@Override
public String getTicket() {
return "ticket";
}
}
@Service是org.apache.dubbo包中的,并不是Spring的中的Service,主要负责服务之间的通讯,如果在另外一个模块中调用,则要编写一个同样的接口。
4.现在修改Zookeeper配置
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 an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/Users/steveyu/Downloads/apache-zookeeper-3.5.7-bin/dataDir
# the port at which the clients will connect
clientPort=2181
dataDir是存储的目录,clientPort是端口通讯。
执行,开启zk注册中心
./apache-zookeeper-3.5.7-bin/bin/zkServer.sh start
5.开启dubbo-admin监控设备
https://github.com/apache/dubbo-admin
进入上面地址,clone一下。进入dubbo-admin进行打包
mvn package
开启dubbo监控设备,进入localhost:7001
点击服务治理,无服务
6.配置application.properties
server.port=8002
# dubbo提供者的别名,只是个标识
dubbo.application.name=provider-server
# zk地址
dubbo.registry.address=zookeeper://localhost:2181
# dubbo协议
dubbo.protocol.name=dubbo
# duboo端口号
dubbo.protocol.port=10057
# 这是你要发布到dubbo的接口所在包位置
dubbo.scan.base-packages=cn.steveyu.providerdemo.service
# log设置
logging.level.root=info
logging.file.name=log/system-run-dev.log
启动该Spring项目,进入7001端口,发现注册生效
7.编写服务端
我们需要在同样的包内,引入相同的接口
package cn.steveyu.providerdemo.service;
public interface TicketService {
String getTicket();
}
写一个服务调用
接口
package cn.steveyu.consumerdemo.service;
public interface GetTicket {
String getTicket();
}
实现
package cn.steveyu.consumerdemo.service;
import cn.steveyu.providerdemo.service.TicketService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Service
@Component
@RequestMapping("123")
public class GetTicketImpl implements GetTicket{
@Reference
TicketService ticketService;
@Override
@ResponseBody
@RequestMapping("123")
public String getTicket() {
return ticketService.getTicket();
}
}
@Reference是一个进行远程装配的。直接调用即可,这边进行了RequestMapping,启动了一个Web服务。
8.启动服务端
服务生效
进行访问Web端口
成功