已经搭建好的dubbo框架为前提
(注解引入dubbo的xml文件 @ImportResource(locations = "classpath:consumer.xml"))
开始替换为nacos
导入依赖:
<!-- Dubbo Nacos registry dependency -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>0.0.1</version>
</dependency>
修改dubbo的xml文件
<!-- 用于配置连接注册中心相关信息 -->
<dubbo:registry address="nacos://自己的ip:8848" check="false" />
下面是我的dubbo文件,只改上面这一行就可以
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--用于配置当前应用信息,不管该应用是提供者还是消费者 -->
<dubbo:application name="dubbo-xml-spring-boot-provider" />
<!-- 用于配置连接注册中心相关信息 -->
<dubbo:registry address="nacos://自己的ip:8848" check="false" />
<!-- 用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受 -->
<dubbo:protocol name="dubbo" port="20882" accesslog="true" />
<!--<dubbo:consumer check="false" />
<dubbo:registry check="false" />-->
<!--用于暴露一个服务,定义服务的元信息,一个服务可以用多个协议暴露,一个服务也可以注册到多个注册中心 -->
<!--每个接口都应定义版本号,为后续不兼容升级提供可能 -->
<!--ref:服务的真正实现类 -->
<dubbo:service interface="com.gjy.service.DefaultApiService" ref="defaultService" version="1.0.0" stub="com.gjy.service.DefaultApiServiceSub"/>
<!--<dubbo:service interface="com.gjy.service.DefaultApiService" ref="defaultService2" version="2.0.0" />-->
<!--监控中心配置 监控中心协议,如果为protocol="registry",表示从注册中心发现监控中心地址,否则直连监控中心。 -->
<!--<dubbo:monitor protocol="registry"></dubbo:monitor>-->
<!-- 直连监控中心服务器地址 -->
<!-- <dubbo:monitor address="localhost:6379"></dubbo:monitor> -->
</beans>
我的 application.properties 文件如下:
server.port=9090
#nacos.config.namespace=b0c23b23-0652-4aa3-af22-9c977dec1a28
#nacos.config.server-addr=192.168.0.102:8848
#nacos.config.enabled=true
# Nacos registry address
#spring.application.name=provider
## Nacos registry address
#dubbo.registry.address=192.168.0.103:8848
# 注册中心
#dubbo.registry.id = dubboRegistry
#dubbo.registry.timeout=5000
#dubbo.registry.protocol=nacos
#dubbo.registry.address = 127.0.0.1:2181
#dubbo.registry.client = curator
#dubbo.registry.protocol=zookeeper
启动成功
调用方: 导入刚才的依赖,修改注册中心地址就可以了
<dubbo:registry address="nacos://自己的ip:8848" check="false"/>
启动: