一、概念

ZooKeeper

​ ZooKeeper 是一个分布式的,开放源码的分布式应用程序协调服务。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。

Dubbo

​ Dubbo 是 Alibaba 开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合)。从服务模型的角度来看, Dubbo 采用的是一种非常简单的模型,要么是提供方提供服务,要么是消费方消费服务,所以基于这一点可以抽象出服务提供方(Provider)和服务消费方(Consumer)两个角色。

image-20201003222225032

二、SpringBoot 2.3 整合 Zookeeper、Dubbo

① 安装 zookeeper 作为注册中心

1
2
3
4
5
// 安装镜像
docker pull zookeeper:latest

// 启动镜像
docker run --name zk01 -p 2181:2181 --restart always -d zookeeper:latest

​ 这里只需要用到 2181 这个端口,只把它暴露,其他的两个端口不需要。

This image includes EXPOSE 2181 2888 3888 8080 (the zookeeper client port, follower port, election port, AdminServer port respectively), so standard container linking will make it automatically available to the linked containers. Since the Zookeeper “fails fast” it’s better to always restart it.

② 编写服务提供者

image-20201003235036091

  1. pom.xml 文件中引入 dubbo 和 zkclient 相关依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.1.0</version>
</dependency>


<!--引入zookeeper的客户端工具-->
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
  1. 配置 dubbo 的扫描包和注册中心地址
1
2
3
4
5
dubbo.registry.address=zookeeper://192.168.64.129:2181

dubbo.application.name=provider-ticker

dubbo.scan.base-packages=com.oy.providerticket.service
  1. 使用@Service 发布服务

【TicketService.java】

1
2
3
public interface TicketService {
public String getTicket();
}

【TicketServiceImpl.java】

1
2
3
4
5
6
7
8
9
10
11
12
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

@Component
// 将服务发送出去
@Service
public class TicketServiceImpl implements TicketService{
@Override
public String getTicket() {
return "《姜子牙》";
}
}

添加在 Applicaiton 上面@EnableDubbo 注解

1
2
3
4
5
6
7
8
9
@SpringBootApplication
@EnableDubbo // 开启Dubbo服务
public class ProviderTickerApplication {

public static void main(String[] args) {
SpringApplication.run(ProviderTickerApplication.class, args);
}

}

③ 编写服务消费者

image-20201004110334471

1.引入依赖‘

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.1.0</version>
</dependency>


<!--引入zookeeper的客户端工具-->
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>

2.配置 dubbo 的注册中心地址

1
2
3
4
5

dubbo.application.name=consumer-user

dubbo.registry.address=zookeeper://192.168.64.129:2181

3.引用服务

image-20201003233918122

【UserService.java】

1
2
3
4
5
6
7
8
9
10
11
12
13
@Service
public class UserService{

@Reference
TicketService ticketService;

public void hello(){
String ticket = ticketService.getTicket();
System.out.println("买到票了:"+ticket);
}


}

④ 启动

  1. 先启动provider-ticket然后再启动consumer-user
  2. **consumer-user **中的 ConsumerUserApplicationTests.java 测试

image-20201004110602386

1
2
3
4
5
6
7
8
9
10
11
12
13
@SpringBootTest
class ConsumerUserApplicationTests {

@Autowired
UserService userService;


@Test
void contextLoads() {
userService.hello();
}

}

image-20201004110700014