Springboot + Dubbo + Nacos微服務框架整合

Fairyyyyyyy發表於2019-05-20

背景

註冊中心Eureka2閉源,zk複雜的paxos一致性演算法、假死腦裂、無管理控制檯不方便管理等問題,以下文中使用到nacos作為註冊中心,作為配置中心後續介紹(對比參考:https://blog.csdn.net/Mr_Errol/article/details/84938993)。

參考:阿里巴巴為什麼不用 ZooKeeper 做服務發現?

Springboot+Dubbo+Nacos微服務框架整合

Nacos server 1.0.0 (文件:https://nacos.io/zh-cn/docs/namespace-endpoint-best-practices.html)

Springboot version 2.0.4 (文件:https://spring.io/projects/spring-boot/)

Dubbo 2.6.5

1、建立maven專案

springboot-dubbo-nacos-demo

2、建立兩個子模組服務提供者和消費者

demo-provider、demo-consumer

3、程式碼

3.1 父pom檔案

<properties>
    <java.version>1.8</java.version>
    <spring-boot.version>2.0.4.RELEASE</spring-boot.version>
    <dubbo-spring-boot-starter.version>0.2.1.RELEASE</dubbo-spring-boot-starter.version>
    <nacos-discovery-spring-boot-starter.version>0.2.1</nacos-discovery-spring-boot-starter.version>
    <dubbo.version>2.6.5</dubbo.version>
    <dubbo-registry-nacos.version>0.0.1</dubbo-registry-nacos.version>
    <dubbo-spring-context-support.version>1.0.2</dubbo-spring-context-support.version>
</properties>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- dubbo + nacos start -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>${dubbo.version}</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo-registry-nacos</artifactId>
    <version>${dubbo-registry-nacos.version}</version>
</dependency>
<dependency>
    <groupId>com.alibaba.spring</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>${dubbo-spring-context-support.version}</version>
</dependency>
<!-- dubbo + nacos end -->

<!-- dubbo + spring-boot + nacos start -->
<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>${dubbo-spring-boot-starter.version}</version>
    <exclusions>
        <exclusion>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>nacos-discovery-spring-boot-starter</artifactId>
    <version>${nacos-discovery-spring-boot-starter.version}</version>
</dependency>
<!-- nacos + springboot + nacos end -->

3.2 demo-provider服務提供方

application.properties新增如下配置:

nacos.config.server-addr=127.0.0.1:8848

##dubbo config
spring.application.name=demo-nacos-provider
server.port=8899
dubbo.application.name=${spring.application.name}
dubbo.registry.address=nacos://127.0.0.1:8848

# dubbo暴露服務對應package
dubbo.scan.base-packages=com.test.demoprovider.service
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
SpringBoot啟動類新增註解:
@NacosPropertySource(dataId = "example", autoRefreshed = true)
暴露的DemoService介面實現類DemoServiceImpl新增@Service註解暴露服務,注意是com.alibaba.dubbo.config.annotation.Service。
@Service
public class DemoServiceImpl implements DemoService {
   @Override
   public String test(String param) {
      return "hello " + param;
   }
}

3.3 demo-consumer服務消費方

application.properties新增如下配置:

nacos.config.server-addr=127.0.0.1:8848

##dubbo config
spring.application.name=demo-nacos-provider
server.port=8899
dubbo.application.name=${spring.application.name}
dubbo.registry.address=nacos://127.0.0.1:8848 
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

消費方消費dubbo服務:

@Controller
@RequestMapping("test")
public class TestNacosConsumer {
   @Reference
   DemoService demoService;

   @RequestMapping(value = "test", method = RequestMethod.GET)
   @ResponseBody
   public String getCounsumerTest() {
      return demoService.test("22");
   }
}

4、naco後端檢視服務

服務名

分組

叢集數目

例項數

健康例項數

操作

providers:com.test.demoprovider.service.DemoService:group1

DEFAULT_GROUP

1

1

1

詳情 刪除

consumers:com.test.demoprovider.service.DemoService:group1

DEFAULT_GROUP

1

1

1

詳情 刪除

相關文章