手记

springboot2.0整合dubbo

写在前面:
使用springboot作为web框架,方便开发许多,做分布式开发,dubbo又不可少,那么怎么整合在一起呢,
跟我学一遍,至少会用
注意,springboot2.0和springboot1.x与dubbo整合不一样,

1.环境

1.新建一个空的maven项目,作为父工程,新建moudle,,service(接口层,及实现层,没有具体分,),web(web层,springboot项目)

项目结构如下

image.png

父pom如下

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencyManagement>
        <dependencies>


            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.0.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--如果要把springboot工程打包成war执行,需要该jar-->
            <!--<dependency>-->
            <!--<groupId>org.springframework.boot</groupId>-->
            <!--<artifactId>spring-boot-legacy</artifactId>-->
            <!--<version>1.0.2.RELEASE</version>-->
            <!--</dependency>-->

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

            <!--引入zookeeper的客户端工具-->
            <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
            <dependency>
                <groupId>com.github.sgroschupf</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.1</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

web层pom

 <dependencies>
        <dependency>
            <groupId>com.itzmn</groupId>
            <artifactId>dubbo-service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>

        <!--引入zookeeper的客户端工具-->
        <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

service层

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>

        <!--引入zookeeper的客户端工具-->
        <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
    </dependencies>

2.接口设计

image.png

在service模块新建接口,


image.png

接口实现类的注解,service一定是dubbo的注解

3.配置文件

# Spring boot applicationspring.application.name = /springboot-dubbo
server.port = 9099management.port = 9091# Service versiondemo.service.version = 1.0.0# Base packages to scan Dubbo Components (e.g @Service , @Reference)dubbo.scan.basePackages  = com.itzmn.dubbo.service.impl# Dubbo Config properties## ApplicationConfig Beandubbo.application.id = springboot-dubbo
dubbo.application.name = springboot-dubbo## ProtocolConfig Beandubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 20880## RegistryConfig Beandubbo.registry.id = my-registry1
dubbo.registry.address = zookeeper://47.106.64.158:2181

在web层的配置文件中,配置,即可,前提,要先安装zookeeper,才能进行服务的注册,然后启动即可

4.消费者

配置

# Spring boot applicationspring.application.name = dubbo-consumer-demo
server.port = 8080
management.port = 8081# Service Versiondemo.service.version = 1.0.0# Dubbo Config properties## ApplicationConfig Beandubbo.application.id = dubbo-consumer-demo
dubbo.application.name = dubbo-consumer-demo## ProtocolConfig Beandubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 12345



作者:z七夜
链接:https://www.jianshu.com/p/226df7409641


0人推荐
随时随地看视频
慕课网APP