小编给大家分享一下SpringBoot+dubbo+ZooKeeper如何实现分布式开发应用,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!环境搭建项目结构图:1.我们首先做好服务端pom.xml &n
小编给大家分享一下SpringBoot+dubbo+ZooKeeper如何实现分布式开发应用,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
项目结构图:
pom.xml
<dependencies> <!--导入依赖:Dubbo + zookeeper--> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.3</version> </dependency> <!--引入zkclient--> <dependency> <groupId>com.GitHub.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <!--引入zkclient,可能会日志冲突,还需要剔除日志依赖--> <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>
application.properties
server.port=8001# 服务应用名字dubbo.application.name=provider-server# 注册中心地址dubbo.reGIStry.address=zookeeper://127.0.0.1:2181# 哪些服务要被注册dubbo.scan.base-packages=com.wlm.service
TicketService
package com.wlm.service;public interface TicketService { public String getTicket();}
TicketServiceImpl.java
package com.wlm.service;import org.apache.dubbo.config.annotation.Service;import org.springframework.stereotype.Component;//zookeeper:服务注册与发现@Service //可以被扫描到,在项目一启动就自动注册到注册中心@Component //使用了Dubbo后尽量不要用Service注解public class TicketServiceImpl implements TicketService { @Override public String getTicket() { return "咸鱼_翻身"; }}
打开服务端,开启之后不要点掉,让他在后台运行:
运行上面的服务端代码,然后去游览器中查看
Http://localhost:7001/
pom.xml
<dependencies> <!--导入相关的依赖--> <!--导入依赖:Dubbo + zookeeper--> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.3</version> </dependency> <!--引入zkclient--> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <!--引入zkclient,可能会日志冲突,还需要剔除日志依赖--> <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>
application.properties
server.port=8002# 消费者去哪里拿服务,需要暴露自己的名字dubbo.application.name=consumer-server# 注册中心的地址,可以在任何电脑上!dubbo.registry.address=zookeeper://127.0.0.1:2181
TicketService
package com.wlm.service;public interface TicketService { public String getTicket();}
UserService.java
package com.wlm.service;import org.apache.dubbo.config.annotation.Reference;import org.springframework.stereotype.Service;@Service //放到容器中,注意区分public class UserService { //想拿到provider-server提供的票,要去注册中心拿到服务 @Reference //引用, pom坐标,可以定义路径相同的接口名 TicketService ticketService; public void buyTicket(){ String ticket = ticketService.getTicket(); System.out.println("在注册中心拿到=>"+ticket); }}
ConsumerServerApplicationTests.java
package com.wlm;import com.wlm.service.UserService;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;@SpringBootTestclass ConsumerServerApplicationTests { @Autowired UserService userService; @Test void contextLoads() { userService.buyTicket(); }}
运行ConsumerServerApplicationTests.java结果:拿到服务端的”咸鱼_翻身“
前提:zookeeper服务已经开启
首先提供者提供服务
导入依赖
配置注册中心的地址,以及服务发现名,和要扫描的包
在想要被注册的服务上面增加一个注解@Service
消费者如何消费
导入依赖
配置注册中心的地址,配置自己的服务名
从远程注入服务 @Reference
看完了这篇文章,相信你对“springBoot+dubbo+zookeeper如何实现分布式开发应用”有了一定的了解,如果想了解更多相关知识,欢迎关注编程网精选频道,感谢各位的阅读!
--结束END--
本文标题: springBoot+dubbo+zookeeper如何实现分布式开发应用
本文链接: https://lsjlt.com/news/324488.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0