SpringBoot整合Dubbo2.5.10

逗貓攻城獅發表於2018-06-12

原文地址: luoliangDSGA's blog
部落格地址: luoliangdsga.github.io
歡迎轉載,轉載請註明作者及出處,謝謝!

SpringBoot整合Dubbo2.5.10,使用官方最新spring-boot-starter

開始

Dubbo已經進入了Apache孵化器,並且釋出了官方的spring-boot-starter0.1.0,用於簡化dubbo應用的配置,主要包括了autoconfigure(自動裝配),externalized-configuration(外部化配置),actuator(生產準備)等,可參考官方github dubbo-spring-boot-starter.

準備工作

需要提前安裝好JDK1.8,Maven,Zookeeper。

初始化Maven專案

為了整個專案結構清晰,使用模組化的maven專案。pom檔案如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <groupId>org.boot.dubbo</groupId>
    <artifactId>boot-dubbo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>boot-dubbo</name>
    <description>Dubbo project for Spring Boot</description>

    <modules>
        <module>dubbo-provider</module>
        <module>dubbo-consumer</module>
        <module>dubbo-api</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <dubbo-spring-boot-starter.version>0.1.0</dubbo-spring-boot-starter.version>
        <springboot.version>1.5.8.RELEASE</springboot.version>
        <fastjson-version>1.2.31</fastjson-version>
        <zk-client.version>0.2</zk-client.version>
        <dockerfile-maven.version>1.4.3</dockerfile-maven.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.boot</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>${dubbo-spring-boot-starter.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
                <version>${springboot.version}</version>
            </dependency>

            <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>${zk-client.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>


複製程式碼

主要分為三個模組,api,provider和consumer

SpringBoot整合Dubbo2.5.10

建立生產者

有了spring-boot-starter,dubbo的配置變得非常簡單,再也不用像以前一樣配置一大堆xml檔案,只需要幾個簡單的配置,就可以做到開箱即用。

  1. 先配置生產者的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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>org.boot.dubbo</groupId>
    <artifactId>dubbo-provider</artifactId>
    <version>${parent.version}</version>
    <packaging>jar</packaging>

    <parent>
        <artifactId>boot-dubbo</artifactId>
        <groupId>org.boot.dubbo</groupId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <name>dubbo-provider</name>
    <description>Dubbo project for Spring Boot:Provider</description>

    <dependencies>
        <!-- Spring Boot dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>zookeeper</artifactId>
                    <groupId>org.apache.zookeeper</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.boot.dubbo</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>${parent.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

複製程式碼
  1. 接著使用properties進行SpringBoot和Dubbo的配置,配置如下:
   spring.application.name=springboot-dubbo-provider
   server.port=9090
   #dubbo配置
   dubbo.application.id=springboot-dubbo-provider
   dubbo.application.name=springboot-dubbo-provider
   dubbo.application.owner=luoliang
   #協議配置
   dubbo.protocol.id=dubbo
   dubbo.protocol.name=dubbo
   #把預設的20880埠換成12345
   dubbo.protocol.port=12345
   #服務註冊配置
   dubbo.registry.id=my-registry
   dubbo.registry.address=zookeeper://localhost:2181
   #配置dubbo的包掃描,針對dubbo的@Service, @Reference註解
   dubbo.scan.base-packages=org.boot.dubbo.provider.service
   #dubbo健康監控
   endpoints.dubbo.enabled=true
   management.health.dubbo.status.defaults=memory
   management.health.dubbo.status.extras=load,threadpool
   management.port=9091
複製程式碼
  1. 進行了上面兩步之後,Dubbo已經整合好了,接下來就可以直接開始擼服務程式碼了,可以直接使用註解來暴露服務介面
  • 先在api中寫一個interface
public interface HelloService {
   String sayHello(String name);
}
複製程式碼
  • 實現介面,加上自己的業務邏輯
@Service(version = "1.0.0",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}")
public class HelloServiceImpl implements HelloService {

    @Override
    public String sayHello(String name) {
        return "Hello, " + name + " (from Spring Boot)";
    }
}
複製程式碼

注意,這裡的service註解是com.alibaba.dubbo.config.annotation.Service

建立消費者

  1. 配置消費者者的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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>org.boot.dubbo</groupId>
    <artifactId>dubbo-consumer</artifactId>
    <version>${parent.version}</version>
    <packaging>jar</packaging>

    <parent>
        <artifactId>boot-dubbo</artifactId>
        <groupId>org.boot.dubbo</groupId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <name>dubbo-consumer</name>
    <description>Dubbo project for Spring Boot:Consumer</description>

    <dependencies>
        <!-- Spring Boot dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.boot.dubbo</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>${parent.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

複製程式碼
  1. application.properties配置如下:
spring.application.name=springboot-dubbo-consumer
server.port=8081
#dubbo配置
dubbo.application.id=springboot-dubbo-consumer
dubbo.application.name=springboot-dubbo-consumer
dubbo.application.owner=luoliang
#服務註冊配置
dubbo.registry.id=my-registry
dubbo.registry.address=zookeeper://localhost:2181
management.port=8082
複製程式碼
  1. 編寫service來消費dubbo的服務,主要程式碼如下:
@Service
public class ConsumerServiceImpl implements ConsumerService {
    @Reference(version = "1.0.0",
            application = "${dubbo.application.id}")
    private HelloService helloService;

    @Override
    public String sayHello(String name) {
        return helloService.sayHello(name);
    }
}
複製程式碼
在mvc的controller中注入此服務
複製程式碼
@RestController
@RequestMapping("/user")
public class DefaultController {
    @Resource
    private ConsumerService consumerService;

    @RequestMapping("/sayHello")
    public String register(String name) {
        return consumerService.sayHello(name);
    }
}

複製程式碼
到這裡,整個專案基本結構已經搭建完成,consumer已經能夠消費provider提供的服務。

現在來測試一下,分別啟動provider和consumer,開啟瀏覽器,輸入http://localhost:8081/user/sayHello?name=dubbo

可以看到,返回的結果和預期一樣,說明專案已經成功整合

SpringBoot整合Dubbo2.5.10

需要原始碼請移步本人github,如果能順手star就更好啦! boot-dubbo下一篇部落格接這篇部落格,學習使用Docker容器化SpringBoot+Dubbo應用。

第一次在掘金髮文,各位大佬輕噴~

參考

  • https://github.com/apache/incubator-dubbo-spring-boot-project

相關文章