一、Dubbo 配置与 SpringBoot 整合 的三种方式
1、方式一
1)导入 dubbo-starter 依赖,在 application.properties 配置属性
dubbo.application.name=boot-order-service-consumer dubbo.registry.protocol=zookeeper dubbo.registry.address=127.0.0.1:2181 dubbo.protocol.name=dubbo dubbo.protocol.port=20881 dubbo.monitor.protocol=registry #dubbo.scan.base-packages=
2)使用 @Service【暴露服务】,使用 @Reference【引用服务】
暴露服务:
@Component @Service //dubbo注解,暴露服务 public class UserServiceImpl implements UserService { @Override public List<UserAddress> getUserAddressList(String userId) { UserAddress address1 = new UserAddress(1, "北京市昌平区宏福科技园综合楼3层", "1", "李老师", "010-56253825", "Y"); UserAddress address2 = new UserAddress(2, "深圳市宝安区西部硅谷大厦B座3层(深圳分校)", "1", "王老师", "010-56253825", "N"); return Arrays.asList(address1,address2); } }
引用服务:
@Service public class OrderServiceImpl implements OrderService { @Reference //引用服务 UserService userService; @Override public List<UserAddress> initOrder(String userId) { System.out.println("用户ID:" + userId); //1.查询用户的收货地址 List<UserAddress> userAddressList = userService.getUserAddressList(userId); System.out.println("userAddressList = " + userAddressList); userAddressList.forEach(t -> System.out.println(t.getUserAddress())); System.out.println("调用完成!"); return userAddressList; } }
3)使用 @EnableDubbo 开启基于注解的 Dubbo 功能或者使用 dubbo.scan.base-packages 指定要扫描的包
@EnableDubbo //开启基于注解的 dubbo 功能 @SpringBootApplication public class BootUserServiceProviderApplication { public static void main(String[] args) { SpringApplication.run(BootUserServiceProviderApplication.class, args); } }
2、方式二:保留 dubbo xml 配置文件
1)导入 dubbo-starter 依赖,使用 @ImportResource 导入类路径下的 配置文件
保留之前的配置文件
2)@ImportResource(locations = "classpath:provider.xml") 导入配置文件
@ImportResource(locations = "classpath:provider.xml") @SpringBootApplication public class BootUserServiceProviderApplication { public static void main(String[] args) { SpringApplication.run(BootUserServiceProviderApplication.class, args); } }
3、方式三:使用注解 API 方式
1)将每一个组件手动创建到容器中
@Configuration public class MyDubboConfig { @Bean public ApplicationConfig applicationConfig() { // 当前应用配置 ApplicationConfig application = new ApplicationConfig(); application.setName("boot-user-provider"); return application; } //<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="20882"></dubbo:protocol> @Bean public ProtocolConfig protocolConfig() { ProtocolConfig protocolConfig = new ProtocolConfig(); protocolConfig.setName("dubbo"); protocolConfig.setPort(20882); return protocolConfig; } //<dubbo:service interface="com.njf.gmall.service.UserService" ref="userServiceImpl01" timeout="1000" version="1.0.0"/> //<!--<dubbo:method name="getUserAddressList" timeout="1000" retries="3"></dubbo:method>--> @Bean public ServiceConfig<UserService> userServiceConfig(UserService userService) { ServiceConfig<UserService> serviceConfig = new ServiceConfig<>(); serviceConfig.setInterface(UserService.class); serviceConfig.setRef(userService); serviceConfig.setTimeout(1000); serviceConfig.setVersion("1.0.1"); //在 service 中配置 method 的信息 MethodConfig methodConfig = new MethodConfig(); methodConfig.setName("getUserAddressList"); methodConfig.setTimeout(1000); //将 method 的设置关联到 service 配置中 serviceConfig.setMethods(Arrays.asList(methodConfig)); return serviceConfig; } }
2)让dubbo 来扫描其他配置的组件
@DubboComponentScan(basePackages = {"com.njf.gmall"}) //扫描dubbo配置组件所在包 @EnableDubbo //开启基于注解的 dubbo 功能 @SpringBootApplication public class BootUserServiceProviderApplication { public static void main(String[] args) { SpringApplication.run(BootUserServiceProviderApplication.class, args); } }
更多API设置参考官网:
手册:https://dubbo.apache.org/zh/docs/references/api/
API配置:https://dubbo.apache.org/zh/docs/references/configuration/api/