SpringBoot三种配置Dubbo的方式

  • Post author:
  • Post category:其他


*必须首先导入dubbo-starter

(1)、使用SpringBoot配置文件(application.properties或application.yml)

dubbo.application.name=boot-service-provider
dubbo.registry.address=192.168.205.128:2181
dubbo.registry.protocol=zookeeper
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.monitor.protocol=registry

server.port=8080

2)、使用dubbo.xml配置文件的方式

1)、编写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:dubbo="http://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <!--1、指定当前服务/应用的名字(同样的服务名字相同,不要和其它的服务同名)-->
    <dubbo:application name="user-provider"/>

    <!--2、指定注册中心的位置(注册中心不同,服务地址的写法不同)-->
    <!--<dubbo:registry address="redis://192.168.205.128:6379"/>-->
    <dubbo:registry address="zookeeper://192.168.205.128:2181"/>

    <!--3、指定通信规则(通信协议&通信端口)-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--4、声明需要暴露的服务接口,ref属性要指向容器中的接口实现对象-->
    <dubbo:service ref="userService" interface="cn.coreqi.service.UserService"/>

    <bean id="userService" class="cn.coreqi.service.impl.UserServiceImpl"/>

</beans>

2)、在SpringBoot主程序启动类上使用@ImportResource注解导入Dubbo的配置文件

package cn.coreqi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource(locations = "classpath:provider.xml")
public class SpringbootdubboserviceproviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootdubboserviceproviderApplication.class, args);
    }

}

(3)、使用Dubbo配置类的方式

package cn.coreqi.config;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyDubboConfig {
    @Bean
    public ApplicationConfig applicationConfig(){
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("user-provider");
        return applicationConfig;
    }
    @Bean
    public RegistryConfig registryConfig(){
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setProtocol("zookeeper");
        registryConfig.setAddress("192.168.205.128:2181");
        return registryConfig;
    }
    ///其他的dubbo配置
}

感谢:

https://www.cnblogs.com/fanqisoft/p/10360262.html