小编给大家分享一下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 "咸鱼_翻身"; } }
打开服务端,开启之后不要点掉,让他在后台运行:
执行服务端的jar包,待会我们要监控服务端:
运行上面的服务端代码,然后去游览器中查看
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; @SpringBootTest class ConsumerServerApplicationTests { @Autowired UserService userService; @Test void contextLoads() { userService.buyTicket(); } }
运行ConsumerServerApplicationTests.java结果:拿到服务端的”咸鱼_翻身“
前提:zookeeper服务已经开启
1.首先提供者提供服务
导入依赖
配置注册中心的地址,以及服务发现名,和要扫描的包
在想要被注册的服务上面增加一个注解@Service
2.消费者如何消费
导入依赖
配置注册中心的地址,配置自己的服务名
从远程注入服务 @Reference
看完了这篇文章,相信你对“springBoot+dubbo+zookeeper如何实现分布式开发应用”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。