springboot+dubbo之多端口注入服务
前面介绍了,springboot+dubbo基础整合,这篇介绍多端口注入服务。
springboot使用@Bean注入dubbo服务,当你是单一的ProviderConfig实例,dubbo的@Service会自动索引到你注入的实例。但实际使用情况下,dubbo服务提供者不可能统统只用一个端口提供服务。
这是你就需要注入多个ProviderConfig,ProtocolConfig实例, 因此你可以封装自己的dubboConfiguration,注入新的ProviderConfig,ProtocolConfig实例。
package hello.configuration; import javax.annotation.Resource; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.alibaba.dubbo.config.ApplicationConfig; import com.alibaba.dubbo.config.ProtocolConfig; import com.alibaba.dubbo.config.ProviderConfig; import com.alibaba.dubbo.config.RegistryConfig; import com.alibaba.dubbo.rpc.Exporter; /** * 多端口提供dubbo服务 * 当你使用多端口提供服务,使用默认端口提供服务:需要加入在service上加上defaultProvider * @author chenlili * */ @Configuration @ConditionalOnClass(Exporter.class) public class DubboAutoConfiguration { @Resource(name="protocolConfig1") private ProtocolConfig protocolConfig; @Resource(name="protocolConfig2") private ProtocolConfig protocolConfig2; /** * 默认基于dubbo协议提供服务 * * @return */ @Bean(name = "protocolConfig1") public ProtocolConfig protocolConfig() { // 服务提供者协议配置 ProtocolConfig protocolConfig = new ProtocolConfig(); protocolConfig.setName("rmi"); protocolConfig.setPort(20881); protocolConfig.setThreads(200); System.out.println("protocolConfig1的hashCode: " + protocolConfig.hashCode()); return protocolConfig; } /** * dubbo服务提供 * * @param applicationConfig * @param registryConfig * @param protocolConfig * @return */ @Bean(name = "providerConfig1") public ProviderConfig providerConfig(ApplicationConfig applicationConfig, RegistryConfig registryConfig) { ProviderConfig providerConfig = new ProviderConfig(); providerConfig.setTimeout(1000); providerConfig.setRetries(1); providerConfig.setDelay(-1); providerConfig.setApplication(applicationConfig); providerConfig.setRegistry(registryConfig); providerConfig.setProtocol(this.protocolConfig); return providerConfig; } /** * 默认基于dubbo协议提供服务 * * @return */ @Bean(name = "protocolConfig2") public ProtocolConfig protocolConfig2() { // 服务提供者协议配置 ProtocolConfig protocolConfig = new ProtocolConfig(); protocolConfig.setName("dubbo"); protocolConfig.setPort(20882); protocolConfig.setThreads(200); System.out.println("protocolConfig2的hashCode: " + protocolConfig.hashCode()); return protocolConfig; } /** * dubbo服务提供 * * @param applicationConfig * @param registryConfig * @param protocolConfig * @return */ @Bean(name = "providerConfig2") public ProviderConfig providerConfig2(ApplicationConfig applicationConfig, RegistryConfig registryConfig) { ProviderConfig providerConfig = new ProviderConfig(); providerConfig.setTimeout(1000); providerConfig.setRetries(1); providerConfig.setDelay(-1); providerConfig.setApplication(applicationConfig); providerConfig.setRegistry(registryConfig); providerConfig.setProtocol(protocolConfig2); return providerConfig; } }
@Service使用时,直接使用@Service(version="1.0.0")会报错,提示你找不到对应的provider,因此需要配上对应的provider,因此在默认dubboConfiguration上加了@Bean(name="defaultProvider"),用于索引默认provider。
package hello.dubbo; import com.alibaba.dubbo.config.annotation.Service; import com.jon.show.service.IDubboDemoService; @Service(version="1.0.0",provider="providerConfig1") public class DubboDemoServiceImpl implements IDubboDemoService{ @Override public String sayHello(String name) { return "hello " + name; } @Override public String sayYourAge(int age) { return null; } }
package hello.dubbo; import com.alibaba.dubbo.config.annotation.Service; import com.google.gson.Gson; import com.jon.show.model.Person; import com.jon.show.service.IDubboPersonService; @Service(version="1.0.0", provider="providerConfig2") public class PersonServiceImpl implements IDubboPersonService{ @Override public String toJson(Person person) { Gson gson = new Gson(); return gson.toJson(person); } }
启动appliction类,dubbo服务就能提供多端口支持了!!
PS:dubbo服务的所需实例均交由spring管理。