spring boot 集成dubbo

Posted zhenghuasheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot 集成dubbo相关的知识,希望对你有一定的参考价值。

springboot集成dubbo 需要导入dubbo所需jar的依赖

  <!-- dubbo dependency -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>


       <!--fastJson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.21</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.3.6</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

dubbo启动时默认会加载classpath下dubbo.properties的配置文件

因为也采用配置文件的方式加载参数

dubbo.application.name=springboot-sample
dubbo.application.owner=zhs
dubbo.application.logger=slf4j
dubbo.registry.address=zookeeper://zookeeper.com:2181
dubbo.monitor.protocol=registry
dubbo.protocol.name=dubbo
dubbo.protocol.port=20887
dubbo.service.loadbalance=roundrobin
dubbo.registry.file=dubboRegistry/registry.file

同时需要导入外部的spring的配置文件来配置dubbo发布服务的配置和调用的配置 spring-dubbo.xml

需要启动的zookeepper注册中心正常流程启动

<?xml version="1.0" encoding="UTF-8"?>
<!--suppress SpringFacetInspection -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">


    <!--&lt;!&ndash; 提供方应用信息,用于计算依赖关系 &ndash;&gt;-->
    <!--<dubbo:application name="yy-web-backend-app"/>-->
    <!--&lt;!&ndash; 使用multicast广播注册中心暴露服务地址 &ndash;&gt;-->
    <!--<dubbo:registry address="zookeeper://zookeeper.com:2181" file="./registry.file"/>-->
    <!--&lt;!&ndash; 用dubbo协议在20880端口暴露服务 &ndash;&gt;-->
    <!--<dubbo:protocol name="dubbo"  port="30908"/>-->


    <dubbo:reference interface="com.order.server.UserServer" id="userServer" check="false"/>
    <dubbo:reference interface="com.order.server.AuthServer" id="authServer"/>
    <dubbo:reference interface="com.order.server.UserMoneyServer" id="userMoneyServer"/>
    <dubbo:reference interface="com.order.server.UserIntegrateServer" id="userIntegrateServer"/>
    <dubbo:reference interface="com.order.server.TaskServer" id="taskServer"/>

</beans>

入口主类添加注解:@ImportResource(“spring-dubbo.xml”)

@SpringBootApplication
@MapperScan("com.sample.dao")
@ImportResource("spring-dubbo.xml")
public class SampleMybatisApplication

        private static Logger logger = Logger.getLogger(SampleMybatisApplication.class);

        /**
         * Main Start
         */
        public static void main(String[] args) 
            SpringApplication.run(SampleMybatisApplication.class, args);
            logger.info("============= SpringBoot Start Success =============");
        

    

dubbo集成完成

以上是关于spring boot 集成dubbo的主要内容,如果未能解决你的问题,请参考以下文章

spring boot 集成dubbo

玩转Spring Boot 集成Dubbo

深入Spring Boot:快速集成Dubbo + Hystrix

spring boot 集成 dubbo 企业完整版

架构实战篇:Spring Boot 集成 Dubbo

spring boot dubbo 集成怎么搞单元测试