springBoot+dubbo+zookeeper实现分布式开发应用的项目实践
时间:2022-11-04 09:53:44|栏目:JAVA代码|点击: 次
环境搭建
项目结构图:
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
栏 目:JAVA代码
下一篇:Fluent Mybatis让你摆脱Xml文件的技巧
本文标题:springBoot+dubbo+zookeeper实现分布式开发应用的项目实践
本文地址:http://www.codeinn.net/misctech/218149.html