SpringBoot三种配置Dubbo的方式
*必须首先导入dubbo-starter
(1)、使用SpringBoot配置文件(application.properties或application.yml)
1 dubbo.application.name=boot-service-provider 2 dubbo.registry.address=192.168.205.128:2181 3 dubbo.registry.protocol=zookeeper 4 dubbo.protocol.name=dubbo 5 dubbo.protocol.port=20880 6 dubbo.monitor.protocol=registry 7 8 server.port=8080
(2)、使用dubbo.xml配置文件的方式
1)、编写xml配置文件(以服务提供者配置文件为例)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" 4 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"> 5 <!--1、指定当前服务/应用的名字(同样的服务名字相同,不要和其它的服务同名)--> 6 <dubbo:application name="user-provider"/> 7 8 <!--2、指定注册中心的位置(注册中心不同,服务地址的写法不同)--> 9 <!--<dubbo:registry address="redis://192.168.205.128:6379"/>--> 10 <dubbo:registry address="zookeeper://192.168.205.128:2181"/> 11 12 <!--3、指定通信规则(通信协议&通信端口)--> 13 <dubbo:protocol name="dubbo" port="20880"/> 14 15 <!--4、声明需要暴露的服务接口,ref属性要指向容器中的接口实现对象--> 16 <dubbo:service ref="userService" interface="cn.coreqi.service.UserService"/> 17 18 <bean id="userService" class="cn.coreqi.service.impl.UserServiceImpl"/> 19 20 </beans>
2)、在SpringBoot主程序启动类上使用@ImportResource注解导入Dubbo的配置文件
1 package cn.coreqi; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.context.annotation.ImportResource; 6 7 @SpringBootApplication 8 @ImportResource(locations = "classpath:provider.xml") 9 public class SpringbootdubboserviceproviderApplication { 10 11 public static void main(String[] args) { 12 SpringApplication.run(SpringbootdubboserviceproviderApplication.class, args); 13 } 14 15 }
(3)、使用Dubbo配置类的方式
1 package cn.coreqi.config; 2 3 import com.alibaba.dubbo.config.ApplicationConfig; 4 import com.alibaba.dubbo.config.RegistryConfig; 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.Configuration; 7 8 @Configuration 9 public class MyDubboConfig { 10 @Bean 11 public ApplicationConfig applicationConfig(){ 12 ApplicationConfig applicationConfig = new ApplicationConfig(); 13 applicationConfig.setName("user-provider"); 14 return applicationConfig; 15 } 16 @Bean 17 public RegistryConfig registryConfig(){ 18 RegistryConfig registryConfig = new RegistryConfig(); 19 registryConfig.setProtocol("zookeeper"); 20 registryConfig.setAddress("192.168.205.128:2181"); 21 return registryConfig; 22 } 23 ///其他的dubbo配置 24 }