1、在分布式系统中,国内常用zookeeper+dubbo组合,而Spring Boot推荐使用全栈的Spring,Spring Boot+Spring Cloud(注册中心)。
2、Zookeeper和Dubbo
Dubbo作为分布式服务框架,他来负责A模块和B模块的过程调用。Zookeeper用于做注册中心,保存B模块的地址。A模块访问注册中心,找到B模块后进行远程调用。
3、安装Zookeeper(windows系统)
1)下载.zip安装包(地址:https://archive.apache.org/dist/zookeeper/zookeeper-3.4.12/)
2)conf目录下创建zoo.cfg文件(内容参考zoo_sample.cfg),因为 Zookeeper 在启动时会找这个文件作为默认配置文件,修改
dataDir=D:\\newtools\\zookeeper-3.4.12\\data
dataLogDir=D:\\newtools\\zookeeper-3.4.12\\log
tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
dataDir:是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
dataLogDir:是 Zookeeper 保存日志文件的目录
clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
3)启动服务:Windows 下的启动脚本是 bin 目录下的zkServer.cmd。
4、Zookeeper+Dubbo测试(创建两个工程,一个服务提供者provider-ticket,一个服务的消费者consumer-user)
1)服务提供者创建接口
package com.test.ticket.service;
public interface TicketService {
public String getTicket();
}
2)服务提供者创建接口实现
package com.test.ticket.service;
public class TicketServiceImpl implements TicketService{
@Override
public String getTicket() {
return "《故乡,故乡》";
}
}
3)想要在consumer-user使用TicketService,需要使用dubbo将provider-ticket注册到注册中心;consumer-user想要消费provider-ticket服务,要到注册中心去订阅ticket服务,达到ticket服务的提供者地址列表之后,user就可以通过dubbo调用ticket服务。
4)将服务提供者注册到注册中心
a、导入spring-boot-dubbo依赖包(地址:https://github.com/apache/incubator-dubbo-spring-boot-project)
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
b、引入zookeeper的客户端工具(地址:http://mvnrepository.com/artifact/com.github.sgroschupf/zkclient/0.1)
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
c、配置相关的属性
#当前应用名
dubbo.application.name=provider-ticket
#注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
#扫描包(需要发布的包)
dubbo.scan.base-packages=com.test.ticket.service
d、服务中添加com.alibaba.dubbo.config.annotation.Service注解,将服务发布出去
package com.test.ticket.service;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
//@Component加载在容器中
@Component
@Service
public class TicketServiceImpl implements TicketService{
@Override
public String getTicket() {
return "《故乡,故乡》";
}
}
e、启动Ticket应用,到consumer-user测试(在user中使用ticket的功能)
f、在user应用中引入依赖,配置dubbo的注册中心地址
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
#当前应用名
dubbo.application.name=consumer-user
#注册中心地址(从注册中心消费)
dubbo.registry.address=zookeeper://127.0.0.1:2181
g、将ticket中的接口类放置到user中(全类名相同)
package com.test.ticket.service;
public interface TicketService {
public String getTicket();
}
h、调用ticket
package com.test.user.service;
import com.alibaba.dubbo.config.annotation.Reference;
import com.test.ticket.service.TicketService;
import org.springframework.stereotype.Service;
@Service
public class UserService {
//@Reference:根据全类名TicketService到注册中心匹配,进行远程引用TicketService服务
@Reference
TicketService ticketService;
//测试时调用
public void hello(){
String ticket = ticketService.getTicket();
System.out.println("买到票了:" + ticket);
}
}
i、测试类测试
package com.test.user;
import com.test.user.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ConsumerUserApplicationTests {
@Autowired
UserService userService;
@Test
public void contextLoads() {
//调用hello()方法,此方法会远程调用ticketService服务中的ticket方法
userService.hello();
}
}