搭建预备:
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文件信息如下:
1、编写一个对外开放的接口类:IHelloService,代码如下
public interface IHelloService { String sayHello(String name);}
2、然后使用maven命令:mvn clean install -DskipTests在本地构建一下,方便下游服务调用
第三步:创建一个springboot提供者服务:dubbo-spring-cloud-provider1、pom文件信息如下:
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-consumer1、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