dubbo从入门到精通 与springBoot集成的三种方式(十一)
1 方式一(注解方式)
导入dubbo-starter,在application.properties配置属性,使用@Service【暴露服务】使用Reference【引用服务】
新版本的dubbo 是@DubboService 和 @DubboReference
然后在启动类上面 标 @EnableDubbo 开启基于注解的dubbo功能
package com.mangoubiubiu; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableDubbo @SpringBootApplication public class UserProviderBootApplication { public static void main(String[] args) { SpringApplication.run(UserProviderBootApplication.class, args); } }
或者在 application.properties 配置 (这个和@EnableDubbo注解 互斥 任选一个)
#要扫描的包 dubbo.scan.base-packages=com.mangoubiubiu
发现提供方也能启动成功
2 方式二(保留xml的配置文件的方式)
导入dubbo-starter,保留dubbo xml配置文件 使用导入dubbo配置文件即可
引入provider.xml,注解导入配置文件 @ImportResource(locations = "classpath: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: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://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <!-- 1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) --> <dubbo:application name="user-service-provider" /> <!--指定注册中心的地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.mongoubiubiu.service.UserService" ref="userServiceImpl001" version="1.0.0" > <!-- <dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method> --> </dubbo:service> <!-- 和本地bean一样实现服务 --> <bean id="userServiceImpl001" class="com.mangoubiubiu.service.impl.UserServiceImpl" /> <!-- 自动发现监控中心 --> <dubbo:monitor protocol="registry"></dubbo:monitor> <!-- 直接指定监控中心的地址 --> <!-- <dubbo:monitor address="127.0.0.1:7070"></dubbo:monitor> --> </beans>
package com.mangoubiubiu; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; //@EnableDubbo @ImportResource(locations = "classpath:provider.xml") @SpringBootApplication public class UserProviderBootApplication { public static void main(String[] args) { SpringApplication.run(UserProviderBootApplication.class, args); } }
也能发布成功
3 方式三(使用配置类)
package com.mangoubiubiu.conf; import com.mongoubiubiu.service.UserService; import javafx.application.Application; import org.apache.dubbo.config.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.ArrayList; import java.util.List; /** * dubbo 配置类 */ @Configuration public class DubboConfig { // <dubbo:application name="user-service-provider" /> @Bean public ApplicationConfig applicationConfig(){ ApplicationConfig applicationConfig=new ApplicationConfig(); //应用名 applicationConfig.setName("user-service-provider-conf"); return applicationConfig; } // <dubbo:registry address="zookeeper://127.0.0.1:2181" /> @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" /> @Bean public ProtocolConfig protocolConfig(){ ProtocolConfig protocolConfig=new ProtocolConfig(); protocolConfig.setName("dubbo"); protocolConfig.setPort(20883); return protocolConfig; } /** * <!-- 声明需要暴露的服务接口 --> * <dubbo:service interface="com.mongoubiubiu.service.UserService" * ref="userServiceImpl001" 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); //设置method的信息 MethodConfig methodConfig=new MethodConfig(); methodConfig.setName("getUserAddressList"); methodConfig.setTimeout(1000); //将method的设置关联到 service配置 List<MethodConfig> list=new ArrayList<>(); //可以添加多个方法配置 list.add(methodConfig); serviceConfig.setMethods(list); return serviceConfig; } }