dubbo与springboot的三种整合方式

SpringBoot与dubbo整合的三种方式:
1、导入dubbo-starter,在application.properties配置属性,使用@Service暴露服务,使用@Reference引用服务,
使用@EnableDubbo开启dubbo注解(或者在application.properties中配置dubbo.scan.base-packages=com.lina02.gmall)
2、保留dubbo.xml配置文件;导入dubbo-starter,使用@ImportResource导入dubbo的配置文件即可
3、使用注解API的方式:将每一个组件手动创建到容器中,让dubbo来扫描其他的组件

配置类如下:

package com.lina02.gmall.config;

import com.alibaba.dubbo.config.*;
import com.lina02.gmall.service.UserService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class MyDubboConfig {

    //<dubbo:application name="user-service-provider"></dubbo:application>
    @Bean
    public ApplicationConfig applicationConfig(){
        ApplicationConfig applicationConfig=new ApplicationConfig();
        applicationConfig.setName("boot-user-service-provider");
        return applicationConfig;
    }

    //<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
    @Bean
    public RegistryConfig registryConfig(){
        RegistryConfig registryConfig=new RegistryConfig();
        registryConfig.setProtocol("zookeeper");
        registryConfig.setAddress("127.0.0.1:2181");
        return registryConfig;
    }

    //<dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
    @Bean
    public ProtocolConfig protocolConfig(){
        ProtocolConfig protocolConfig=new ProtocolConfig();
        protocolConfig.setName("dubbo");
        protocolConfig.setPort(20882);
        return protocolConfig;
    }

    /**
     * <dubbo:service interface="com.lina02.gmall.service.UserService" ref="userServiceImpl" timeout="1000" version="1.0.0">
     *      <dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method>
     * </dubbo:service>
     */
    @Bean
    public ServiceConfig<UserService> userServiceServiceConfig(UserService userService){
        ServiceConfig<UserService> serviceConfig = new ServiceConfig<>();
        serviceConfig.setInterface(UserService.class);
        serviceConfig.setRef(userService);
        serviceConfig.setVersion("1.0.0");

        //配置每一个method的信息
        MethodConfig methodConfig = new MethodConfig();
        methodConfig.setName("getUserAddressList");
        methodConfig.setTimeout(1000);

        //将method的设置关联到service配置中
        List<MethodConfig> methods = new ArrayList<>();
        methods.add(methodConfig);
        serviceConfig.setMethods(methods);

        return serviceConfig;
    }
}

开启dubbo扫描组件:

@DubboComponentScan(basePackages = "com.lina02.gmall")

或者

@EnableDubbo(scanBasePackages = "com.lina02.gmall")

posted on 2018-10-28 14:37  lina2014  阅读(530)  评论(0编辑  收藏  举报

导航