SpringBoot整合Dubbo,註冊中心nacos

東東小瑞發表於2020-07-28

目錄

開發環境

開始搭建Demo

1. 建立父工程

2. 刪除無用src目錄

3. 建立Dubbo服務提供者工程dubbo-provider

4. 建立Dubbo介面工程dubbo-client

5. 在父工程引入pom座標

6. 建立介面DubboDemo

7. dubbo-provider引入dubbo-client

8. 建立DubboDemo的實現類DubboDemoImpl

9. 在實現類上新增註解@DubboService

10. 在resources中建立配置檔案application.yml

11. 建立啟動類

12. 建立消費者工程dubbo-consumer

13. 引入pom座標

14. 在resources下建立application.yml

15. 建立controller進行消費

16. 建立啟動類ConsumerApplication

17. 啟動dubbo-provider和dubbo-consumer,檢視nacos

18. 測試訪問:localhost:8080/hello


開發環境

  1. 工具:IDEA
  2. java環境:JDK1.8

開始搭建Demo

1. 建立父工程

2. 刪除無用src目錄

3. 建立Dubbo服務提供者工程dubbo-provider

4. 建立Dubbo介面工程dubbo-client

5. 在父工程引入pom座標

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sunrd</groupId>
    <artifactId>springboot-dubbo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>dubbo-provider</module>
        <module>dubbo-client</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-registry-nacos -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-nacos</artifactId>
            <version>2.7.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba.boot/nacos-config-spring-boot-starter -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-starter</artifactId>
            <version>0.2.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.esotericsoftware/kryo -->
        <!--序列化-->
        <dependency>
            <groupId>com.esotericsoftware</groupId>
            <artifactId>kryo</artifactId>
            <version>4.0.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/de.javakaffee/kryo-serializers -->
        <dependency>
            <groupId>de.javakaffee</groupId>
            <artifactId>kryo-serializers</artifactId>
            <version>0.45</version>
        </dependency>
    </dependencies>

</project>

6. 建立介面DubboDemo

 

package com.sunrd.service;

public interface DubboDemo {
    /**
     * 測試介面
     * @return
     */
    String sayHello();
}

7. dubbo-provider引入dubbo-client

<dependencies>
    <dependency>
        <groupId>com.sunrd</groupId>
        <artifactId>dubbo-client</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

8. 建立DubboDemo的實現類DubboDemoImpl

package com.sunrd.service.impl;

import com.sunrd.service.DubboDemo;

/**
 * @author sunrd
 * @description
 * @date 2020/7/28
 */
public class DubboDemoImpl implements DubboDemo {
    @Override
    public String sayHello() {
        return "hello, dubbo";
    }
}

9. 在實現類上新增註解@DubboService

@DubboService(interfaceClass = DubboDemo.class)
public class DubboDemoImpl implements DubboDemo {
    @Override
    public String sayHello() {
        return "hello, dubbo";
    }
}

10. 在resources中建立配置檔案application.yml

spring:
  application:
    name: DUBBO-PROVIDER
dubbo:
  protocol:
    name: dubbo
    serialization: kryo
    port: 20980
  registry:
    address: nacos://192.168.5.3:8848 # 自己的nacos地址
    check: false
    group: noncore
    version: 1.0
    protocol: nacos
    parameters:
      namespace: dev
  provider:
    version: 1.0
    group: noncore

11. 建立啟動類

package com.sunrd.service;

import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author sunrd
 * @description
 * @date 2020/7/28
 */
@SpringBootApplication
// dubbo服務掃描的包
@DubboComponentScan(basePackages = {
    "com.sunrd.service.impl"
})
public class ProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}

12. 建立消費者工程dubbo-consumer

13. 引入pom座標

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springboot-dubbo</artifactId>
        <groupId>com.sunrd</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-consumer</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.sunrd</groupId>
            <artifactId>dubbo-client</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

14. 在resources下建立application.yml

spring:
  application:
    name: DUBBO-CONCUMER
dubbo:
  protocol:
    name: dubbo
    serialization: kryo
    port: 20980
  registry:
    address: nacos://42.194.181.247:8848 # 自己的nacos地址
    check: false
    group: noncore
    version: 1.0
    protocol: nacos
    parameters:
      namespace: dev
  consumer:
    group: noncore
    check: false
    version: 1.0

15. 建立controller進行消費

package com.sunrd.controller;

import com.sunrd.service.DubboDemo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author sunrd
 * @description
 * @date 2020/7/28
 */
@RestController
public class DubboTestController {
    @DubboReference
    private DubboDemo dubboDemo;

    @RequestMapping("/hello")
    public String sayHello(){
        return dubboDemo.sayHello();
    }
}

16. 建立啟動類ConsumerApplication

package com.sunrd.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author sunrd
 * @description
 * @date 2020/7/28
 */
@SpringBootApplication
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

17. 啟動dubbo-provider和dubbo-consumer,檢視nacos

18. 測試訪問:localhost:8080/hello

 

相關文章