dubbo+zookeeper
下载
zookeeper
https://zookeeper.apache.org/releases.html
dubbo-admin
https://github.com/apache/dubbo-admin/tree/master
打包dubbo-admin成jar包
provider-server
依赖
org.apache.dubbo
dubbo-spring-boot-starter
2.7.5
com.github.sgroschupf
zkclient
0.1
org.apache.curator
curator-framework
4.2.0
org.apache.curator
curator-recipes
4.2.0
org.apache.zookeeper
zookeeper
3.5.7
org.slf4j
slf4j-log4j12
配置
server:
port: 8001
dubbo:
application:
name: provider-server
registry:
address: zookeeper://127.0.0.1:2181
scan:
base-packages: cn.pinked.service
服务类
@Service //用于dubbo自动注册到注册中心
@Component //为避免与dubbo的service注解冲突,这里使用component注解注册到spring
public class TicketServiceImpl implements TicketService {
@Override
public String getTicket() {
return "电影票";
}
}
consumer-server
依赖
- 同上
配置
server:
port: 8002
dubbo:
application:
name: consumer-server
registry:
address: zookeeper://127.0.0.1:2181
服务类
@Service //这里是放入spring容器中
public class UserService {
//去注册中心拿到服务
@Reference //引用 通过pom坐标或定义路径相同的接口名
TicketService ticketService;
public void buyTicket(){
String ticket = ticketService.getTicket();
System.out.println(ticket);
}
}
测试类
@Autowired
UserService userService;
@Test
void contextLoads() {
userService.buyTicket();
}