本文最后更新于:2025年8月13日 晚上
Zookeeper
官网:https://zookeeper.apache.org/releases.html
以Apache ZooKeeper 3.8.4为例
下载后解压,然后进入config目录,复制文件zoo_sample.cfg,并命名为zoo.cfg
将文件内的临时文件路径修改为:../data
回到上一级目录,创建data文件夹。
进入bin目录,双击zkServer.cmd
启动服务。
Dubbo
官方网站:https://cn.dubbo.apache.org/zh-cn/
Dubbo
依赖注册中心,官方以前默认用Zookeeper
,现在可以支持更多注册中心了,这里还是以zookeeper为例
Dubbo-Admin
【Java版本】下载:https://github.com/apache/dubbo-admin/tree/java
后端服务步骤:
下载代码: git clone https://github.com/apache/dubbo-admin.git
修改 dubbo-admin-server/src/main/resources/application.properties
配置文件,例如注册端点等
打包,通过文件目录内输入cmd打开终端,然后输入一下命令,若mvn未生效,则需要将mvn添加到环境变量中。
mvn clean package -Dmaven.test.skip=true
启动(请确保注册中心已启动)
mvn --projects dubbo-admin-server spring-boot:run
or
cd dubbo-admin-distribution/target; java -jar dubbo-admin-${project.version}.jar
访问 http://localhost:38080
, 默认用户名和密码是 root
前端服务步骤:
1、将目录内的dubbo-admin-ui
项目在vscode中打开(确保已安装nodejs 16)
2、执行命令:
1 2 3 4 5 6 7 8 9 10 11 12 npm install npm run dev npm run build npm run build --report
成功输出:
1 2 3 4 5 6 7 8 You may use special comments to disable some warnings. Use // eslint-disable-next-line to ignore the next line. Use /* eslint-disable */ to ignore all warnings in a file. App running at: - Local: http://localhost:38082/ - Network: http://192.168.1.6:38082/
3、访问页面:http://localhost:38082/
集成使用
依赖(以zookeeper为例)
对应官方文档:https://cn.dubbo.apache.org/zh-cn/overview/mannual/java-sdk/reference-manual/registry/zookeeper/
目前我下载使用的zookeeper是3.8.4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <dependency > <groupId > org.apache.dubbo</groupId > <artifactId > dubbo-spring-boot-starter</artifactId > <version > ${dubbo.version}</version > </dependency > <dependency > <groupId > org.apache.dubbo</groupId > <artifactId > dubbo-zookeeper-curator5-spring-boot-starter</artifactId > <version > ${dubbo.version}</version > </dependency >
公共接口(公共api包)
提供方与调用方共同引入此自定义包,然后由提供方实现接口方法,调用方进行调用。
1 2 3 public interface UserService { String getUser () ; }
服务提供方
配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 server: port: 8285 dubbo: application: name: user-service-provider qos-port: 22231 registry: address: 127.0 .0 .1 :2181 protocol: zookeeper protocol: name: dubbo port: 20885
启动类
1 2 3 4 5 6 7 @EnableDubbo @SpringBootApplication public class UserMain { public static void main (String[] args) { SpringApplication.run(UserMain.class, args); } }
方法类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 @DubboService(group = "user-service-provider", version = "1.0.0", // 版本,可区别用来进行灰度发布 weight = 100, // 权重 timeout = 5000, // 超时时间 retries = 3 // 重试次数 ) @Service public class UserServiceImpl implements UserService { @Override public String getUser () { return "hello dubbo1" ; } }
服务调用方
配置文件
1 2 3 4 5 6 7 8 9 10 11 12 server: port: 8283 dubbo: application: name: order-service-consumer qos-port: 22223 registry: address: zookeeper://127.0.0.1:2181
接口调用方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 @RestController public class OrderController { @DubboReference(group = "user-service-provider", version = "1.0.0", // 对应的版本号 timeout = 5000, // 超时时间 retries = 0, // 重试次数 loadbalance = "roundrobin", // 负载均衡 >> 当前为随机 cluster = "failover", // 集群容错 mock = "fail:return null" // 服务降级 ) private UserService userService; @GetMapping("/getOrder") public String getOrder () { System.out.println(userService.getUser()); return "订单服务" ; } }