搭建预备:
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
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
1、编写一个对外开放的接口类:IHelloService,代码如下
public interface IHelloService {
String sayHello(String name);
}
2、然后使用maven命令:mvn clean install -DskipTests在本地构建一下,方便下游服务调用
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=dubbo
dubbo.protocol.port=20880
dubbo.registry.address=zookeeper://192.168.1.107:2181
dubbo.scan.base-packages=com.cb.dubbospringcloudprovider.impl
#cloud相关配置
spring.cloud.zookeeper.connect-string=192.168.1.107:2181
spring.cloud.zookeeper.discovery.instance-host=192.168.1.107
spring.cloud.zookeeper.discovery.instance-port=8088
spring.cloud.zookeeper.discovery.register=true
#spring相关配置
server.port=8088
spring.application.name=dubbo-spring-cloud-provider
4、启动类如下:
@SpringBootApplication
@EnableDubbo
public class DubboSpringCloudProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboSpringCloudProviderApplication.class, args);
}
}
5、然后启动该类,启动成功之后,打开zk可视化工具,查看注册信息,截图如下:
说明提供者已被注册,结构也是正确的
1、pom信息与提供者的pom相同即可
2、创建一个controller,远程调用提供者和api接口类,实现远程调用
@RestController
public class HelloController {
@Reference(retries = 0,check = false)
private IHelloService iHelloService;
@GetMapping("/say")
public String sayHello(){
return iHelloService.sayHello("hello world");
}
}
3、启动类如下
@SpringBootApplication
@EnableDiscoveryClient
public 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.dubbospringcloudconsumer
server.port=8070
#注册节点
dubbo.protocol.name=dubbo
#dubbo服务端口,默认是20880
dubbo.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