Spring Boot:整合Dubbo
一.dubbo-boot-provider模块
maven依赖:
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 | <dependencies> <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> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!-- 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> </dependencies> |
application.properties
1 2 3 4 5 6 | dubbo.application.name=dubbo-boot-provider dubbo.registry.address= 192.168 . 10.132 : 2181 dubbo.registry.protocol=zookeeper dubbo.protocol.name=dubbo dubbo.protocol.port= 10000 |
service类:
注意这里的@Service是Dubbo的注解,不是Spring的Service注解
1 2 3 4 5 6 7 8 9 10 | @Service public class UserServiceImpl implements UserService { @Override public List<User> getAll() { User user1 = new User( 1 , "张三" , 12 , "北京" ); User user2 = new User( 2 , "李四" , 13 , "北京" ); return Arrays.asList(user1, user2); } } |
启动类:
1 2 3 4 5 6 7 8 9 | @SpringBootApplication @EnableDubbo //开启基于注解的dubbo功能 public class DubboBootProviderApplication { public static void main(String[] args) { SpringApplication.run(DubboBootProviderApplication. class , args); } } |
二.dubbo-boot-consumer模块
maven依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </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> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version> 2.7 . 3 </version> </dependency> |
消费者:
调用UserService时,用@Reference表示远程调用,而不是用@Autowired
1 2 3 4 5 6 7 8 9 10 | @Controller public class UserController { @Reference //远程引用userService UserService userService; @RequestMapping ( "/getAll" ) @ResponseBody public List<User> getAll(){ return userService.getAll(); } } |
application.properties
1 2 3 4 | dubbo.application.name=dubbo-boot-consumer dubbo.registry.address=zookeeper: //192.168.10.132:2181 server.port= 7070 |
启动类:
1 2 3 4 5 6 7 8 9 | @SpringBootApplication @EnableDubbo public class DubboBootConsumerApplication { public static void main(String[] args) { SpringApplication.run(DubboBootConsumerApplication. class , args); } } |
三.运行两个boot主应用,在浏览器输入localhost:7070/getAll
标签:
Spring Boot
, Dubbo
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix