[dubbo学习] 简单的dubbo功能
1.Dubbo结构图
生产者-消费者模型,生产者和消费者都需要在注册中心进行注册,生产者生产接口,通过api暴露给消费者使用
注册中心管理服务提供方的url
监控模块负责监控管理整个流程
生产者(Provider)整个发布,订阅流程:
- 启动容器,加载,运行服务提供者
- 服务提供者在启动时,在注册中心发布注册自己提供的服务
- 服务消费者在启动时,在注册中心订阅自己所需的服务。
如果存在服务失败或者变更的情况,Dubbo就进行如下的操作:
- 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
- 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
- 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
2.Dubbo实例
2.1 zookeeper
2.1.1 zookeeper 下载
zookeeper下载:https://zookeeper.apache.org/releases.html
2.1.2 zookeeper安装
windows 安装
1.解压下载的压缩包
2.修改 zoo_sample.cfg 为 zoo.cfg
3.bin目录下启动 zkServer.cmd
Linux 安装
1.解压下载的压缩包
2.修改 zoo_sample.cfg 为 zoo.cfg
3.修改zoo.cfg
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=/home/tjtl/dubbo/apache-zookeeper-3.6.1-bin/data # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1 admin.serverPort=8888 ## Metrics Providers # # https://prometheus.io Metrics Exporter #metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider #metricsProvider.httpPort=7000 #metricsProvider.exportJvmInfo=true server.1=zoo1:2888:3888
4.在 /home/tjtl/dubbo/apache-zookeeper-3.6.1-bin/ 目录下建立文件夹data
1 | mkdir /home/tjtl/dubbo/apache-zookeeper-3.6.1-bin/data |
5.在第4步建立的文件夹下建立文件myid
6.执行命令
1 | echo “1” > myid |
"1" 对应 zoo.cfg 文件中 “server.1” 中的 “1"
7.在bin目录下启动
./zkServer.sh start-foreground |
Linux 下安装遇到问题处理
1.报错 java.net.SocketException:Unresolved address
处理: 在/etc/hosts中配置主机映射
1 | 127.0.0.1 master |
2.2 dubbo-admin安装
下载Url https://github.com/apache/dubbo
修改/dubbo-admin-2.5.4/WEB-INF/dubbo.properties
1 2 3 | dubbo.registry.address=zookeeper: //127.0.0.1:2181 dubbo.admin.root.password=root dubbo.admin.guest.password=guest |
使用maven自己手动打包mvn package -Dmaven.skip.test=true
将war包放入tomcat下,执行tomcat
访问url:http://127.0.0.1:8090/dubbo-admin-2.5.4
访问用户名:root
访问密码:root
2.3 dubbo 生产者 消费者 API
项目目录
API提供生产者的接口暴露,生产者在service中实现暴露接口,消费者通过controller进行消费
springboot-dubbo.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <? 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 > < groupId >springboot-dubbo</ groupId > < artifactId >springboot-dubbo</ artifactId > < packaging >pom</ packaging > < version >1.0-SNAPSHOT</ version > < modules > < module >dubbo-api</ module > < module >dubbo-provider</ module > < module >dubbo-consumer</ module > </ modules > </ project > |
2.3.1 创建生产者
生产者在service中进行接口实现
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | <? 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.3.0.RELEASE</ version > < relativePath /> <!-- lookup parent from repository --> </ parent > < groupId >com.soft</ groupId > < artifactId >dubbo-provider</ artifactId > < version >0.0.1-SNAPSHOT</ version > < name >dubbo-provider</ name > < description >dubbo-provider</ description > < properties > < java.version >1.8</ java.version > </ properties > < dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-test</ artifactId > < scope >test</ scope > </ dependency > <!--服务提供者的接口API--> < dependency > < groupId >com.soft</ groupId > < artifactId >dubbo-api</ artifactId > < version >v1.0</ version > </ dependency > <!-- dubbo依赖 --> < dependency > < groupId >com.alibaba.spring.boot</ groupId > < artifactId >dubbo-spring-boot-starter</ artifactId > < version >2.0.0</ version > </ dependency > <!-- 引入zookeeper的依赖 --> < dependency > < groupId >com.101tec</ groupId > < artifactId >zkclient</ artifactId > < version >0.10</ version > </ dependency > </ dependencies > < build > < plugins > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > </ plugin > </ plugins > </ build > </ project > |
application.properties
1 2 3 4 5 | ############### dubbo 配置 ############################### # 端口 server.port=9011 spring.dubbo.application.name=dubbo-provider spring.dubbo.application.registry=zookeeper://IP:2181 |
DubboProviderApplication.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.soft.provider; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableDubboConfiguration //开启Dubbo的自动配置 @SpringBootApplication public class DubboProviderApplication { public static void main(String[] args) { SpringApplication.run(DubboProviderApplication.class, args); } } |
ProviderDemoImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.soft.provider.service; import com.alibaba.dubbo.config.annotation.Service; import com.soft.api.service.IProviderDemo; import org.springframework.stereotype.Component; /** * dubbo 服务层测试 * @author suphowe */ //使用com.alibaba.dubbo.config.annotation.Service,作用是暴露服务,不要使用Spring中的@Service @Service @Component public class ProviderDemoImpl implements IProviderDemo { @Override public String providerReturnString(String name) { return "Provider Return:".concat(name); } } |
2.3.2 创建消费者
消费者在controller中进行消费
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | <? 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.3.0.RELEASE</ version > < relativePath /> <!-- lookup parent from repository --> </ parent > < groupId >com.soft</ groupId > < artifactId >dubbo-consumer</ artifactId > < version >0.0.1-SNAPSHOT</ version > < name >dubbo-consumer</ name > < description >dubbo-consumer</ description > < properties > < java.version >1.8</ java.version > </ properties > < dependencies > < 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 > <!--服务提供者的接口API--> < dependency > < groupId >com.soft</ groupId > < artifactId >dubbo-api</ artifactId > < version >v1.0</ version > </ dependency > <!-- dubbo依赖 --> < dependency > < groupId >com.alibaba.spring.boot</ groupId > < artifactId >dubbo-spring-boot-starter</ artifactId > < version >2.0.0</ version > </ dependency > <!-- 引入zookeeper的依赖 --> < dependency > < groupId >com.101tec</ groupId > < artifactId >zkclient</ artifactId > < version >0.10</ version > </ dependency > </ dependencies > < build > < plugins > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > </ plugin > </ plugins > </ build > </ project > |
application.properties
1 2 3 4 5 | ############### dubbo 配置 ############################### # 端口 server.port=9021 spring.dubbo.application.name=dubbo-consumer spring.dubbo.application.registry=zookeeper://Ip:2181 |
DubboConsumerApplication.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.soft.consumer; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableDubboConfiguration //开启Dubbo的自动配置 @SpringBootApplication public class DubboConsumerApplication { public static void main(String[] args) { SpringApplication.run(DubboConsumerApplication.class, args); } } |
ConsumerController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package com.soft.consumer.controller; import com.alibaba.dubbo.config.annotation.Reference; import com.soft.api.service.IProviderDemo; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ConsumerController { @Reference private IProviderDemo providerDemo; @RequestMapping(value = "/callInterfase") public String callInterfase() { String hello = providerDemo.providerReturnString("consumer test "); System.out.println(providerDemo.providerReturnString("consumer print ")); return hello; } } |
2.3.3 创建API
API提供生产者暴露的接口
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <? 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 >springboot-dubbo</ groupId > < artifactId >springboot-dubbo</ artifactId > < version >1.0-SNAPSHOT</ version > </ parent > < groupId >com.soft</ groupId > < artifactId >dubbo-api</ artifactId > < version >v1.0</ version > </ project > |
IProviderDemo.java
1 2 3 4 5 6 | package com.soft.api.service; public interface IProviderDemo { String providerReturnString(String name); } |
2.4 测试
依次启动生产者和消费者
范围http://127.0.0.1:9021/callInterfase
查看http://127.0.0.1:8090/dubbo-admin-2.5.4/
Success!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)