1. HTTP3W博客首页
  2. Java

dubbo集成nacos或zookeeper

项目前期采用spring-boot+dubbo+zookeeper架构搭建,业务方需要将注册中心切换为nacos,故在前期基础上进行项目改造,实现步骤如下:

改造前:dubbo+zookeeper配置

<!--公共版本配置---> 
<properties>
        <java.version>1.8</java.version>
        <dubbo.starter.version>0.2.0</dubbo.starter.version>


 </properties>
  
 <!-- Spring Boot Dubbo 依赖 -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo.starter.version}</version>
        </dependency>

yml文件

spring:
  application:
    name: dubbo-icon-provider
server:
  port: 6080
dubbo:
  application:
    name: dubbo-icon-provider
  protocol:
    name: dubbo
    port: 20880
  provider:
    timeout: 1000
  monitor:
    protocol: registry
  registry:

    address: zookeeper://ip1:2181?backup=ip2:2181,ip3:2181

改造后:dubbo+nacos配置

<!--公共版本配置-->
  <properties>
        <java.version>1.8</java.version>
        <dubbo-registry-nacos.version>2.7.3</dubbo-registry-nacos.version>
        <nacos-client.version>1.1.4</nacos-client.version>
    </properties>
 <!-- Spring Boot Dubbo 依赖 -->
  <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo-registry-nacos.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
  </dependency>
 <!-- Dubbo + Nacos 依赖 -->
 <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-nacos</artifactId>
            <version>${dubbo-registry-nacos.version}</version>
  </dependency>
  <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>${nacos-client.version}</version>
   </dependency>

yml文件

spring:
  application:
    name: dubbo-icon-provider
server:
  port: 6080

dubbo:
  application:
    name: dubbo-icon-provider
  protocol:
    name: dubbo
    port: 20880
  provider:
    timeout: 1000
  monitor:
    protocol: registry
  registry:
    address: nacos://ip:8848

通过上面前后对比,可以随心去修改我们的架构组合了。

dubbo使用

服务注册:在启动类增加注解 @EnableDubbo

服务发现:service使用注解 @Reference

    @Reference
    private IIconService iconService;

至此一个springBoot+dubbo+zk 到 spring+dubbo+nacos的项目就改造完毕了~~~///(^v^)\\\~~~

注意上面的加粗和标红位置

1、原创文章,作者:诺米,如若转载,请注明出处:https://www.http3w.com/archives/702

2、本站内容若有雷同从属巧合,若侵犯了您的权益,请联系本站删除,E-mail: wtao219@qq.com

发表评论

您的电子邮箱地址不会被公开。

联系我们

254007489

在线咨询:点击这里给我发消息

邮件:wtao219@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息