Dubbo+Zookeeper
2020-04-14 本文已影响0人
忒无聊了叭
这两个技术都是用于分布式的,通俗的讲:就是把功能细分后,每个服务器需要去调度其他服务器的功能时使用的。Dubbo是一个RPC框架,Zookeeper是作为服务的注册中心,即所有的服务需要在zookeeper上进行注册才可以。
介绍
Dubbo是一个高性能的基于]ava实现的RPC通信框架。负责服务器之间的通信。
Zookeeper:注册中心。所有的服务要现在注册中心中进行注册,才可以进行在每个模块间进行使用。

安装Zookeeper
1、解压zookeeper安装包

2、在conf文件中找到zoo_sample.cfg,然后复制修改命名为zoo.cfg

3、打开服务端

4、然后再打开客户端,就是zkCli
5、可以先测试下客户端,创建节点,显示所有的节点

这就代表zookeeper安装成功了!
安装dubbo-admin
这是dubbo的一个可视化管理界面。
github地址:https://github.com/apache/dubbo-admin/tree/master
下载解压后进行打包。
# 打包命令
mvn clean package -Dmaven.test.skip=true

打包成功后,在target下找到jar

使用命令来启动这个jar包。

然后进入浏览器:访问http://localhost:7001/

Dubbo+Zookeeper+springBoot整合
1、创建一个空项目
2、在空项目中创建两个model模块,一个是生产者,一个是消费者。
相当于两台服务器,消费者服务器去调用生产者服务中的服务。
生产者的端口是8081,消费者端口是8082。
pom.xml(两个应用的pom应该保持一致)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.szw</groupId>
<artifactId>consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--dubbo启动器-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<!-- 客户端zookeeper -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!--日志会冲突,zookeeper将日志排除-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
生产者
Service接口:
package com.szw.service;
public interface BuyService {
public String buy();
}
Service实现类:
package com.szw.service;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
@Service//这个是dubbo的注解service,可以被dubbo扫描到,项目启动就自动到注册中心zookeeper
@Component
public class BuyServiceImpl implements BuyService{
@Override
public String buy() {
return "动物园的票";
}
}
配置文件:
server.port=8081
# 服务应用的名字
dubbo.application.name=provider
# 在注册中心的地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
# 哪些服务需要扫描,需要被暴露
dubbo.scan.base-packages=com.szw.service
消费者:
Service接口:
//注意,如果某台服务器想调用另一台服务器的功能,调用服务的接口必须与那个一样,这里的两个接口名字一样
package com.szw.service;
public interface BuyService {
public String buy();
}
Service实现类:
package com.szw.service;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
@org.apache.dubbo.config.annotation.Service
@Service//放入到容器,这个是spring的service
public class UserService {
@Reference//这里引用的就是另一台服务器的服务
BuyService buyService;
public void buyService(){
String ticket = buyService.buy();
System.out.println("买到了"+ticket);
}
}
server.port=8082
# 服务应用的名字
dubbo.application.name=consumer
# 在注册中心的地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
测试:
package com.szw;
import com.szw.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ConsumerApplicationTests {
@Autowired
UserService userService;
@Test
void contextLoads() {
userService.buyService();
}
}
通过测试:我们可以成功的从消费者服务器调用生产者服务器的服务,完成!

查看Windows下哪些端口被占用