Nacos Dubbo 配置类

1,626 阅读3分钟

Nacos + Dubbo

基本不用改什么东西

就用之前的项目改造一下 ZooKeeper与Dubbo里的项目

  • 依赖:官方强烈推荐使用 Dubbo 2.6.5
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
	https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
    <modules>
        <module>nacosdubbo-api</module>
    </modules>
    <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.ler</groupId>
	<artifactId>nacosdubbo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>nacosdubbo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<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>


		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>

		<!-- Dubbo Nacos registry dependency -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo-registry-nacos</artifactId>
			<version>0.0.1</version>
		</dependency>

		<!-- Dubbo dependency -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.6.5</version>
		</dependency>

		<!-- Alibaba Spring Context extension -->
		<dependency>
			<groupId>com.alibaba.spring</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>1.0.2</version>
		</dependency>

		<!--swagger-->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.9.2</version>
		</dependency>

		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.9.2</version>
		</dependency>

		<dependency>
			<groupId>com.ler</groupId>
			<artifactId>nacosdubbo-api</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>
  • 注册中心配置,使用的是配置类

这里只有注册中心配置,具体请看源码,或者 ZooKeeper与Dubbo 中配置类

/**
 * 连接注册中心配置
 */
@Bean
public RegistryConfig registryConfig() {
    RegistryConfig registryConfig = new RegistryConfig();

    //zk 协议
    //registryConfig.setProtocol("zookeeper");
    //zk 地址
    //registryConfig.setAddress("127.0.0.1:2181");

    //nacos 协议
    registryConfig.setProtocol("nacos");
    //nacos 地址
    registryConfig.setAddress("127.0.0.1:8848");

    //缓存存放路径
    registryConfig.setFile("dubbo.cache");
    //whether to export service on the register center
    registryConfig.setRegister(true);
    registryConfig.setClient("curator");
    return registryConfig;
}
  • OK了,从 ZooKeeper 转到了 Nacos

启动,在Nacos服务列表可以看到

服务列表

info

调用服务

调用服务

如果是xml配置 只需要调整 address 属性配置即可

假设您 Dubbo 应用的使用 Zookeeper 作为注册中心,并且其服务器 IP 地址为:10.20.153.10,并且装配 Spring Bean 在 XML 文件中,如下所示:

<?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://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        
    http://dubbo.apache.org/schema/dubbo        
    http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
 
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="dubbo-provider-xml-demo"  />
 
    <!-- 使用 Zookeeper 注册中心 -->
    <dubbo:registry address="zookeeper://10.20.153.10:2181" />
 	...
</beans>

与 Dubbo Spring 外部化配置 配置类似,只需要调整 address 属性配置即可:

<?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://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        
    http://dubbo.apache.org/schema/dubbo        
    http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
 
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="dubbo-provider-xml-demo"  />
 
    <!-- 使用 Nacos 注册中心 -->
    <dubbo:registry address="nacos://10.20.153.10:8848" />
 	...
</beans>

源码

Dubbo+Nacos注册中心源码