springBoot+dubbo+zookeeper实现分布式开发应用的项目实践
作者:咸鱼_翻身
本文主要介绍了springBoot+dubbo+zookeeper实现分布式开发应用的项目实践,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
环境搭建
项目结构图:
1.我们首先做好服务端
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 "咸鱼_翻身"; } }
2.启动服务端
打开服务端,开启之后不要点掉,让他在后台运行:
执行服务端的jar包,待会我们要监控服务端:
运行上面的服务端代码,然后去游览器中查看
http://localhost:7001/
3.我们再来做客户端
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; @SpringBootTest class ConsumerServerApplicationTests { @Autowired UserService userService; @Test void contextLoads() { userService.buyTicket(); } }
运行ConsumerServerApplicationTests.java结果:拿到服务端的”咸鱼_翻身“
4.总结
前提:zookeeper服务已经开启
1.首先提供者提供服务
- 导入依赖
- 配置注册中心的地址,以及服务发现名,和要扫描的包
- 在想要被注册的服务上面增加一个注解@Service
2.消费者如何消费
- 导入依赖
- 配置注册中心的地址,配置自己的服务名
- 从远程注入服务 @Reference
到此这篇关于springBoot+dubbo+zookeeper实现分布式开发应用的项目实践的文章就介绍到这了,更多相关springBoot dubbo zookeeper分布式 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!