ServiceConfig serviceConfig1 = new ServiceConfig(); serviceConfig1.setInterface(DemoService.class); serviceConfig1.setRef(new DemoServiceImpl()); serviceConfig1.setVersion(version1); // set executor1 for serviceConfig1, max threads is 10 NamedThreadFactory threadFactory1 = new NamedThreadFactory("DemoService-executor"); ExecutorService executor1 = Executors.newFixedThreadPool(10, threadFactory1); serviceConfig1.setExecutor(executor1);
ServiceConfig serviceConfig2 = new ServiceConfig(); serviceConfig2.setInterface(HelloService.class); serviceConfig2.setRef(new HelloServiceImpl()); serviceConfig2.setVersion(version2); // set executor2 for serviceConfig2, max threads is 100 NamedThreadFactory threadFactory2 = new NamedThreadFactory("HelloService-executor"); ExecutorService executor2 = Executors.newFixedThreadPool(100, threadFactory2); serviceConfig2.setExecutor(executor2);
ServiceConfig serviceConfig3 = new ServiceConfig(); serviceConfig3.setInterface(HelloService.class); serviceConfig3.setRef(new HelloServiceImpl()); serviceConfig3.setVersion(version3); // Because executor is not set for serviceConfig3, the default executor of serviceConfig3 is built using // the threadpool parameter of the protocolConfig ( FixedThreadpool , max threads is 200) serviceConfig3.setExecutor(null);
// It takes effect only if [executor-management-mode=isolation] is configured ApplicationConfig applicationConfig = new ApplicationConfig("provider-app"); applicationConfig.setExecutorManagementMode("isolation");
providerBootstrap .application(applicationConfig) .registry(registryConfig) // export with tri and dubbo protocol .protocol(new ProtocolConfig("tri", 20001)) .protocol(new ProtocolConfig("dubbo", 20002)) .service(serviceConfig1) .service(serviceConfig2) .service(serviceConfig3);
<!-- expose three service with dubbo and tri protocol--> <beanid="demoServiceV1"class="org.apache.dubbo.config.spring.impl.DemoServiceImpl"/> <beanid="helloServiceV2"class="org.apache.dubbo.config.spring.impl.HelloServiceImpl"/> <beanid="helloServiceV3"class="org.apache.dubbo.config.spring.impl.HelloServiceImpl"/>
<!-- customized thread pool --> <beanid="executor-demo-service" class="org.apache.dubbo.config.spring.isolation.spring.support.DemoServiceExecutor"/> <beanid="executor-hello-service" class="org.apache.dubbo.config.spring.isolation.spring.support.HelloServiceExecutor"/>
<!-- this service use [executor="executor-demo-service"] as isolated thread pool--> <dubbo:serviceexecutor="executor-demo-service" interface="org.apache.dubbo.config.spring.api.DemoService"version="1.0.0"group="Group1" timeout="3000"ref="demoServiceV1"registry="registry1"protocol="dubbo,tri"/>
<!-- this service use [executor="executor-hello-service"] as isolated thread pool--> <dubbo:serviceexecutor="executor-hello-service" interface="org.apache.dubbo.config.spring.api.HelloService"version="2.0.0"group="Group2" timeout="5000"ref="helloServiceV2"registry="registry1"protocol="dubbo,tri"/>
<!-- not set executor for this service, the default executor built using threadpool parameter of the protocolConfig --> <dubbo:serviceinterface="org.apache.dubbo.config.spring.api.HelloService"version="3.0.0"group="Group3" timeout="5000"ref="helloServiceV3"registry="registry1"protocol="dubbo,tri"/>
@Configuration @EnableDubbo(scanBasePackages = "org.apache.dubbo.config.spring.isolation.spring.annotation.provider") publicclassProviderConfiguration{ @Bean public RegistryConfig registryConfig(){ RegistryConfig registryConfig = new RegistryConfig(); registryConfig.setAddress("zookeeper://127.0.0.1:2181"); return registryConfig; }
// NOTE: we need config executor-management-mode="isolation" @Bean public ApplicationConfig applicationConfig(){ ApplicationConfig applicationConfig = new ApplicationConfig("provider-app"); applicationConfig.setExecutorManagementMode("isolation"); return applicationConfig; }
// expose services with dubbo protocol @Bean public ProtocolConfig dubbo(){ ProtocolConfig protocolConfig = new ProtocolConfig("dubbo"); return protocolConfig; }
// expose services with tri protocol @Bean public ProtocolConfig tri(){ ProtocolConfig protocolConfig = new ProtocolConfig("tri"); return protocolConfig; }
// customized thread pool @Bean("executor-demo-service") public Executor demoServiceExecutor(){ returnnew DemoServiceExecutor(); }
// customized thread pool @Bean("executor-hello-service") public Executor helloServiceExecutor(){ returnnew HelloServiceExecutor(); } }
自定义的线程池实现
1 2 3 4 5 6 7
// customized thread pool publicclassDemoServiceExecutorextendsThreadPoolExecutor{ publicDemoServiceExecutor(){ super(10, 10, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<>(), new NamedThreadFactory("DemoServiceExecutor")); } }
1 2 3 4 5 6 7
// customized thread pool publicclassHelloServiceExecutorextendsThreadPoolExecutor{ publicHelloServiceExecutor(){ super(100, 100, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<>(), new NamedThreadFactory("HelloServiceExecutor")); } }
自定义的业务服务实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// "executor-hello-service" is beanName @DubboService(executor = "executor-demo-service", version = "1.0.0", group = "Group1") publicclassDemoServiceImplV1implementsDemoService{
@Override public String sayName(String name){ return"server name"; }
@Override public Box getBox(){ returnnull; } }
1 2 3 4 5 6 7 8 9 10
// not set executor for this service, the default executor built using threadpool parameter of the protocolConfig @DubboService(version = "3.0.0", group = "Group3") publicclassHelloServiceImplV2implementsHelloService{ privatestaticfinal Logger logger = LoggerFactory.getLogger(HelloServiceImplV2.class);
@Override public String sayHello(String name){ return"server hello"; } }
1 2 3 4 5 6 7 8 9
@DubboService(executor = "executor-hello-service", version = "2.0.0", group = "Group2") publicclassHelloServiceImplV3implementsHelloService{ privatestaticfinal Logger logger = LoggerFactory.getLogger(HelloServiceImplV3.class);
@Override public String sayHello(String name){ return"server hello"; } }