欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Dubbo系列之与ZooKeeper&SpringCloud集成

时间:2023-05-13

搭建预备:

ZooKeeper安装包:https://download.csdn.net/download/qq_38377525/81091193https://download.csdn.net/download/qq_38377525/81091193

dubbo-admin客户端:https://download.csdn.net/download/qq_38377525/81091530https://download.csdn.net/download/qq_38377525/81091530

zk可视化工具:https://download.csdn.net/download/qq_38377525/81090760https://download.csdn.net/download/qq_38377525/81090760


架构说明:

父工程:dubbo-spring-cloud

对外开放的普通maven工程:dubbo-spring-cloud-api

生产者/提供者:dubbo-spring-cloud-provider

消费者:dubbo-spring-cloud-consumer


第一步:创建一个父工程:dubbo-spring-cloud

1、pom文件信息如下:

dubbo-spring-cloud-api dubbo-spring-cloud-consumer dubbo-spring-cloud-provider org.springframework.boot spring-boot-starter-actuator org.springframework spring-core 5.3.6 org.springframework.boot spring-boot-starter-web org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies Finchley.SR1 pom import org.springframework.boot spring-boot-dependencies 2.1.11.RELEASE pom import com.alibaba.cloud spring-cloud-alibaba-dependencies 2.1.1.RELEASE pom import

第二步:创建一个maven工程:dubbo-spring-cloud-api

1、编写一个对外开放的接口类:IHelloService,代码如下

public interface IHelloService { String sayHello(String name);}

2、然后使用maven命令:mvn clean install -DskipTests在本地构建一下,方便下游服务调用

第三步:创建一个springboot提供者服务:dubbo-spring-cloud-provider

1、pom文件信息如下:

org.springframework.boot spring-boot-starter com.alibaba.cloud spring-cloud-starter-dubbo org.springframework.cloud spring-cloud-starter-zookeeper-discovery com.cb dubbo-spring-cloud-api 0.0.1-SNAPSHOT org.apache.curator curator-framework 2.8.0 org.apache.curator curator-recipes 2.8.0 org.apache.curator curator-client 2.13.0

2、实现api接口,代码如下:

@Service //服务注册@org.springframework.stereotype.Service //spring注入public class HelloServiceImpl implements IHelloService { public String sayHello(String name) { return name; }}

3、配置文件如下:

#dubbo相关配置dubbo.protocol.name=dubbodubbo.protocol.port=20880dubbo.registry.address=zookeeper://192.168.1.107:2181dubbo.scan.base-packages=com.cb.dubbospringcloudprovider.impl#cloud相关配置spring.cloud.zookeeper.connect-string=192.168.1.107:2181spring.cloud.zookeeper.discovery.instance-host=192.168.1.107spring.cloud.zookeeper.discovery.instance-port=8088spring.cloud.zookeeper.discovery.register=true#spring相关配置server.port=8088spring.application.name=dubbo-spring-cloud-provider

4、启动类如下:

@SpringBootApplication@EnableDubbopublic class DubboSpringCloudProviderApplication { public static void main(String[] args) { SpringApplication.run(DubboSpringCloudProviderApplication.class, args); }}

5、然后启动该类,启动成功之后,打开zk可视化工具,查看注册信息,截图如下:

说明提供者已被注册,结构也是正确的

第四步:创建一个springboot消费者服务:dubbo-spring-cloud-consumer

1、pom信息与提供者的pom相同即可

2、创建一个controller,远程调用提供者和api接口类,实现远程调用

@RestControllerpublic class HelloController { @Reference(retries = 0,check = false) private IHelloService iHelloService; @GetMapping("/say") public String sayHello(){ return iHelloService.sayHello("hello world"); }}

3、启动类如下

@SpringBootApplication@EnableDiscoveryClientpublic class DubboSpringCloudConsumerApplication { public static void main(String[] args) { SpringApplication.run(DubboSpringCloudConsumerApplication.class, args); }}

4、配置文件如下:

#指定要远程调用的注册服务名,不声明的话默认是扫描全部服务dubbo.cloud.subscribed-services=dubbo-spring-cloud-provider#注册服务名spring.application.name=dubbo-spring-cloud-consumer#是否注册spring.cloud.zookeeper.discovery.register=true#注册中心ip:port字符串spring.cloud.zookeeper.connect-string=192.168.1.107:2181#扫描需要注册的类包路径dubbo.scan.base-packages=com.cb.dubbospringcloudconsumerserver.port=8070#注册节点dubbo.protocol.name=dubbo#dubbo服务端口,默认是20880dubbo.protocol.port=20881#zk链接地址dubbo.registry.address=zookeeper://192.168.179.114:2181

5、然后启动该类,启动成功之后,打开zk可视化工具,查看注册信息,截图如下:

说明消费者注册成功

第五步:远程调用消费者的接口

1、访问 http://localhost:8070/say

返回:hello world

返回正确,集成到这里就结束啦!


demo样例链接:

https://gitee.com/IT_CB/dubbo-spring-cloudhttps://gitee.com/IT_CB/dubbo-spring-cloud

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。