为了账号安全,请及时绑定邮箱和手机立即绑定
首页 手记 springboot2.0整合dubbo

springboot2.0整合dubbo

2018.11.27 18:32 2175浏览

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

1.环境

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

项目结构如下

webp

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.接口设计

webp

image.png

在service模块新建接口,


webp

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人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
手记
粉丝
199
获赞与收藏
857

关注TA,一起探索更多经验知识

同主题相似文章浏览排行榜

风间影月说签约讲师

50篇手记,涉及Java、MySQL、Redis、Spring等方向

进入讨论

Tony Bai 说签约讲师

145篇手记,涉及Go、C、Java、Python等方向

进入讨论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消