spring整合bubbo配置

spring 集成dubbo

说明:

​ 本例以zookeeper做服务注册中心:

1 zookeeper和dubbo的相关依赖包

 <!-- zk -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>jline</groupId>
                    <artifactId>jline</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.jboss.netty</groupId>
                    <artifactId>netty</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.7</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
<!-- dubbox自身的依赖  -->
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>3.0.7.Final</version>
            <exclusions>
                <exclusion>
                    <artifactId>commons-io</artifactId>
                    <groupId>commons-io</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-client</artifactId>
            <version>3.0.7.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jackson-provider</artifactId>
            <version>3.0.7.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.2.4.Final</version>
        </dependency>
 <!-- dubbo服务 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.8.4</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.httpcomponents</groupId>
                    <artifactId>httpcore</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-expression</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-beans</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-aop</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>guava</artifactId>
                    <groupId>com.google.guava</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>httpclient</artifactId>
                    <groupId>org.apache.httpcomponents</groupId>
                </exclusion>
            </exclusions>
        </dependency>

2 配置文件 dubbo.properties

#zookeeper地址1
dubbo.master.registry.address=192.168.0.1:2181
#zookeeper地址2
dubbo.slave.registry.address=192.168.0.2:2181
#主机地址
dubbo.protocol.host=192.168.0.1
#dubbo的log日志存储路径
dubbo.protocol.log=/opt/log/wutongyu/dubbo/dubbo.log
#dubbo端口
dubbo.siem.protocol.port=9096
#queues
dubbo.siem.protocol.queues=5
#开启的线程数
dubbo.siem.protocol.threads=5
#dubbo协议序列化
dubbo.protocol.serialization=kryo
#all 所有消息都派发到线程池,包括请求,响应,连接事件,断开事件,心跳等。
#direct 所有消息都不派发到线程池,全部在IO线程上直接执行。
#message 只有请求响应消息派发到线程池,其它连接断开事件,心跳等消息,直接在IO线程上执行。
#execution 只请求消息派发到线程池,不含响应,响应和其它连接断开事件,心跳等消息,直接在IO线程上执行。
#connection 在IO线程上,将连接断开事件放入队列,有序逐个执行,其它消息派发到线程池。
dubbo.protocol.dispatcher=message
#线程池类型
#fixed 固定大小线程池,启动时建立线程,不关闭,一直持有。(缺省)
#cached 缓存线程池,空闲一分钟自动删除,需要时重建。
#limited 可伸缩线程池,但池中的线程数只会增长不会收缩。(为避免收缩时突然来了大流量引起的性能问题)。
dubbo.protocol.threadpool=fixed

3 dubbo注册配置 dubbo-register-info.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
​
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:c="http://www.springframework.org/schema/c"
​
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
​
    xsi:schemaLocation=
        "http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd 
​
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
​
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
​
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
​
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
​
    <!--多注册中心,HA环境下,主备机上的zookeeper都注册-->
    <dubbo:registry id="master_registry" protocol="zookeeper"
                    address="${dubbo.master.registry.address}"
                    check="false"
    />
    <dubbo:registry id="slave_registry" protocol="zookeeper"
                    address="${dubbo.slave.registry.address}"
                    check="false"
    />
</beans>

4 消费端consumer端代码示例

public interface ElasticsearchDubboService {
    //所有非基本类型的参数,必须序列化
    public Map<String,Object> getAllLogInfo(Criteria criteria);
​
    public boolean batchDelete(ArrayList<String> index_type_idList);
​
    public List<Map<String, Object>> top10LogSourceFor24Hours();
​
    public Map<String,Long> getIndexCount(HashMap<String,String> dateMap, String  logSourceUuid);
}

5 服务提供方provider端代码示例

@Service("elasticsearchDubboService")
public class ElasticsearchDubboServiceImpl implements ElasticsearchDubboService {
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private SiemLogSourceService siemLogSourceService;
    @Override
    public Map<String, Object> getAllLogInfo(Criteria criteria) {
        //具体实现
    }
​
    @Override
    public boolean batchDelete(ArrayList<String> index_type_idList) {
        //具体实现
    }
​
    @Override
    public List<Map<String, Object>> top10LogSourceFor24Hours() {
        //具体实现
    }
​
    @Override
    public Map<String, Long> getIndexCount(HashMap<String, String> dateMap,String  logSourceUuid) {
        //具体实现
    }
}

6 consumer端配置 spring-dubbo-consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation=
               "http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
​
    <import resource="dubbo-register-info.xml"/>
​
    <!-- 调用方应用信息,用于计算依赖关系 -->
    <dubbo:application name="wutongyu-action"/>
​
    <!-- 日志审计消费配置,配置从 master_registry 注册中心获取服务-->
    <dubbo:reference id="elasticsearchDubboService" interface="com.wutongyu.service.dubbo.ElasticsearchDubboService"
                     version="1.0.0" check="false" registry="master_registry">
        <dubbo:method name="getAllLogInfo" async="false" return="true"/>
        <dubbo:method name="batchDelete" async="false" return="true"/>
        <dubbo:method name="top10LogSourceFor24Hours" async="false" return="true"/>
        <dubbo:method name="getIndexCount" async="false" return="true"/>
    </dubbo:reference>
    
    <!-- 配置从 slave_registry 注册中心获取服务-->
    <dubbo:reference id="其他" interface="xx.xx.xx.xx"
                     version="1.0.0" check="false" registry="slave_registry" >
        <dubbo:method name="xx" async="false" return="true"/>
        <dubbo:method name="xxx" async="false" return="true"/>
    </dubbo:reference>
</beans>

7 provider 端配置 spring-dubbo-provider.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
​
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
​
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
​
       xsi:schemaLocation=
               "http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd 
​
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
​
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
​
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
​
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
    <!-- 提供方应用信息,用于计算依赖关系,zookeeper的注册信息中可以看到 -->
    <dubbo:application name="wutongyu-siem"/>
    <!-- 指定传输协议 -->
    <dubbo:protocol id="dubbo-siem"
                    name="suninfodubbo"
                    accesslog="${dubbo.protocol.log}"
                    host="${dubbo.protocol.host}"
                    port="${dubbo.siem.protocol.port}"
                    serialization="${dubbo.protocol.serialization}"
                    dispatcher="${dubbo.protocol.dispatcher}"
                    threadpool="${dubbo.protocol.threadpool}"
                    threads="${dubbo.siem.protocol.threads}"
                    queues="${dubbo.siem.protocol.queues}"
    />
​
    <!-- 声明需要暴露的服务接口,配置需要暴露服务的注册中心,master_registry,slave_registry -->
    <dubbo:service version="1.0.0" protocol="dubbo-siem"
                   interface="com.wutongyu.service.dubbo.ElasticsearchDubboService"
                   ref="elasticsearchDubboService" timeout="10000" registry="master_registry,slave_registry">
        <dubbo:method name="getAllLogInfo" async="false" return="true"/>
        <dubbo:method name="batchDelete" async="false" return="true"/>
        <dubbo:method name="top10LogSourceFor24Hours" async="false" return="true"/>
        <dubbo:method name="getIndexCount" async="false" return="true"/>
    </dubbo:service>
    
</beans>
​

8 consumer端启动dubbo服务

(1) spring-consumer-context.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-4.0.xsd">
​
    <!-- 引入属性文件 -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
                <value>classpath:dubbo.properties</value>
            </list>
        </property>
    </bean>
​
    <import resource="spring-mybatis.xml"/>
    <import resource="spring-dubbo-consumer.xml"/>
</beans>
​

(2) 启动

public class ConsumerDubboMain {
    public static void main(String[] args) {
        //spring初始化beans
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-consumer-context.xml");
        context.start();
        Syslog.out.println("消费端已启动");
    }
}

9 provider端启动dubbo服务

(1) spring-provider-context.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-4.0.xsd">
​
    <!-- 引入属性文件 -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
                <value>classpath:dubbo.properties</value>
            </list>
        </property>
    </bean>
​
    <import resource="spring-mybatis.xml"/>
    <import resource="spring-dubbo-provider.xml"/>
</beans>
​

(2) 启动

public class ProviderDubboMain {
    public static void main(String[] args) {
        //spring初始化beans
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-provider-context.xml");
        context.start();
        Syslog.out.println("服务提供方已启动");
    }
}

注:查看zookeeper的dubbo注册信息:https://blog.youkuaiyun.com/wutongyuWxc/article/details/87374780

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值