2024.6.11 Tuesday
接上文【WEEK15】 【DAY4】Dubbo和Zookeeper集成第一部分【中文版】
目录
- 18.Dubbo和Zookeeper集成
18.Dubbo和Zookeeper集成
18.3.测试环境搭建
18.3.4.Windows下安装dubbo-admin
dubbo本身并不是一个服务软件。它其实就是一个jar包,能够帮你的java程序连接到zookeeper,并利用zookeeper消费、提供服务。
但是为了让用户更好的管理监控众多的dubbo服务,官方提供了一个可视化的监控程序dubbo-admin,不过这个监控即使不装也不影响使用。
18.3.4.1.下载dubbo-admin
https://github.com/apache/dubbo-admin/tree/develop
现在官网只剩下develop版本,master版本从这个链接下载:https://github.com/wikerx/dubbo-admin-master
使用master版本进行后续操作。
18.3.4.2.解压进入目录
修改 dubbo-admin\src\main\resources \application.properties 指定zookeeper地址(刚才没有修改则不需要修改)
server.port=7001
spring.velocity.cache=false
spring.velocity.charset=UTF-8
spring.velocity.layout-url=/templates/default.vm
spring.messages.fallback-to-system-locale=false
spring.messages.basename=i18n/message
spring.root.password=root
spring.guest.password=guest
#以下是注册中心的地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
18.3.4.3.在项目目录下打包dubbo-admin
mvn clean package -Dmaven.test.skip=true
18.3.4.4.执行 dubbo-admin\target 下的dubbo-admin-0.0.1-SNAPSHOT.jar
java -jar dubbo-admin-0.0.1-SNAPSHOT.jar
此时zookeeper的服务一定要处于打开状态,否则无法查询到。
执行完毕,访问http://localhost:7001/,输入默认账号和密码(都是root)
点击“登录”后:
可以看一下系统日志:
18.4.SpringBoot + Dubbo + zookeeper
18.4.1.框架搭建
18.4.1.1.创建空项目Dubbo_zookeeper
18.4.1.2.新建provider-server模块
添加web支持
删除多余文件
18.4.1.2.1.新建service文件夹,新建TicketService.java和TicketServiceImpl.java
18.4.1.2.2.TicketService.java
package com.P60.service;
public interface TicketService {
public String getTicket();
}
18.4.1.2.3.TicketServiceImpl.java
package com.P60.service;
public class TicketServiceImpl implements TicketService {
@Override //alt+insert重写方法
public String getTicket() {
return "Dubbo+Zookeeper";
}
}
18.4.1.3.新建consumer-server模块
添加web支持
删除多余文件
18.4.1.3.1.新建service文件夹,新建UserService.java
package com.P60.service;
public interface UserService {
//需要到注册中心取出provider-server中的票
}
18.4.2.修改provider-server(服务提供者)
18.4.2.1.导入依赖
将服务提供者注册到注册中心,我们需要整合Dubbo和zookeeper,所以需要导包。
18.4.2.1.1.Dubbo Spring Boot Starter
https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter/2.7.3
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
18.4.2.1.2.ZooKeeper Client
https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
18.4.2.2.修改pom.xml
按照惯例修改settings中的maven,jdk和Java版本,Project Structure中的jdk和Java版本。修改pom中的springframework版本到2.7.13,重新加载Maven。
<?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.7.13</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>provider-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>provider-server</name>
<description>provider-server</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<!--Dubbo+zookeeper-->
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</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>
<!--zookeeper服务端及其框架包-->
<!-- 引入zookeeper -->
<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.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<!--排除这个slf4j-log4j12:为了解决日志冲突,需要剔除日志依赖-->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</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>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
18.4.2.3.修改application.properties
# 可参考D:\dubbo-admin-master-master\dubbo-admin\src\main\resources\application.properties中的配置文件
spring.application.name=provider-server
server.port=8001
# 当前应用名
dubbo.application.name=provider-server
# 注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
# 扫描指定包下的服务(需要注册)
dubbo.scan.base-packages=com.P60.service
18.4.2.4.修改TicketServiceImpl.java
package com.P60.service;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
//使用了Dubbo后尽量不用Service注解:因为Spring和Dubbo有同名注解@Service
@Service //使项目被扫描,自动注册到注册中心
@Component //存放在容器中
public class TicketServiceImpl implements TicketService {
@Override //alt+insert重写方法
public String getTicket() {
return "Dubbo+Zookeeper";
}
}
18.4.2.5.运行ProviderServerApplication
记得先运行zkServer.cmd,可以通过dubbo-admin-0.0.1-SNAPSHOT.jar访问。
访问http://localhost:7001/
选择“服务治理”->“提供者”
可见“提供者”:
点击172.18.20.16:20880查看提供者
18.4.3.服务消费者
18.4.3.1.修改pom.xml(和provider类似)
<?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.7.13</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>consumer-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consumer-server</name>
<description>consumer-server</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<!--Dubbo+zookeeper-->
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</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>
<!--zookeeper服务端及其框架包-->
<!-- 引入zookeeper -->
<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.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<!--排除这个slf4j-log4j12:为了解决日志冲突,需要剔除日志依赖-->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</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>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
18.4.3.2.修改application.properties
spring.application.name=consumer-server
server.port=8002
# 消费者获取服务需要暴露自己的名字
dubbo.application.name=consumer-server
# 注册中心的地址,可以在任何电脑上
dubbo.registry.address=zookeeper://127.0.0.1:2181
18.4.3.3.修改UserService.java
package com.P60.service;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
@Service //只是调用接口,注入到容器中
public class UserService { //不能写成Interface接口类,因为接口是抽象的,不能再调用抽象类
//需要到注册中心取出provider-server中的票->远程引用指定的服务,按照全类名进行匹配,看谁给注册中心注册了这个全类名
@Reference //引用:两种方式可解决(1.使用pom坐标;2.定义路径相同的接口名)
//此处采取第二种方式:直接将provider-server中的TicketService复制一份到consumer-server中
TicketService ticketService;
public void buyTicket(){
String ticket = ticketService.getTicket();
System.out.println("从注册中心获取"+ticket);
}
}
18.4.3.4.修改并运行ConsumerServerApplicationTests.java
package com.P60;
import com.P60.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ConsumerServerApplicationTests {
@Autowired //注入本地应用使用Autowired而非Reference
UserService userService;
@Test
void contextLoads() {
userService.buyTicket();
}
}
这就是SpingBoot + dubbo + zookeeper实现分布式开发的应用,其实就是一个服务拆分的思想。