Springboot改造之配置--Dubbo配置篇

接上一篇,api模块很简单,就是对外提供接口,所以就不做介绍了,下面讲一下Sprintboot 中对dubbo的配置。

dubbo的配置在 core 模块,首先来看一下在项目中的位置:
Springboot改造之配置--Dubbo配置篇_第1张图片

左侧选中的三行就是dubbo主要的配置,resources下是 dubbo-provider.xml 和 dubbo-consumer.xml , 用于提供服务、引用服务,文件的内容如下:

dubbo-provider.xml


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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://code.alibabatech.com/schema/dubbo
                           http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="msso-provider" owner="acs-provider" organization="Msso-Center" />
    <dubbo:registry address="${registry-address}" protocol="zookeeper" />
    <dubbo:protocol name="dubbo" port="${provider-port}" accesslog="true" />

    <dubbo:service ref="outInSettleFacade" group="${msso-service-group}" version="${msso-service-version}" timeout="${msso-service-timeout}"
                   interface="com.mljr.acs.boss.settle.service.IOutInSettleFacade" cluster="failfast" />


beans>


dubbo-consumer.xml


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:registry address="${consumer-registry-address}" protocol="zookeeper"/>

    <dubbo:reference id="queryFacade" interface="com.mljr.order.service.QueryFacade" cluster="failfast"
                     group="${acs-settle-service-group}" version="${acs-settle-service-version}" check="false" timeout="${acs-settle-service-timeout}">
    dubbo:reference>
beans>


下面是令dubbo配置生效的注解配置:

package com.mljr.auth.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:dubbo-consumer.properties")
@PropertySource("classpath:dubbo-provider.properties")
@ImportResource({ "classpath:/dubbo/dubbo-*.xml" })
public class DubboConfig {
}

@Configuration注解 是令注解的类中的配置生效
@PropertySource注解 是引入属性配置文件properties,使其中配置的属性变成应用全局变量而被引用
@ImportResource注解 实现xml配置的装载,使应用启动时自动装载文件中的配置


最后讲一下属性配置文件,配置文件在web模块,目前只配置了dev 和 prod 环境,以 dev 为例
Springboot改造之配置--Dubbo配置篇_第2张图片

dubbo-provider.properties

registry-address=192.168.xx.xxx:2181
provider-port=24461

msso-service-group=MssoCenter-dev
msso-service-version=1.0.0
msso-service-timeout=3000000


dubbo-consumer.properties

consumer-registry-address=192.168.xx.xxx:2181
fault-tolerance-type=failfast

acs-settle-service-group=ACS-SettleCenter-dev
acs-settle-service-version=1.0.0
acs-settle-service-timeout=300000

properties中配置的属性,对应着xml文件中 { } 内的变量引用。Dubbo配置的说明就到这里。

你可能感兴趣的:(SpringBoot系列)