二、Dubbo框架源码分析:dubbo使用方法
一、Spring集成dubbo
local.xml:
<bean id=“xxxService” class=“com.xxx.XxxServiceImpl” />
“xxxAction” class=“com.xxx.XxxAction”>
“xxxService” ref=“xxxService” />
远程服务 Spring 配置
在本地服务的基础上,只需做简单配置,即可完成远程化:
将上面的
local.xml 配置拆分成两份,将服务定义部分放在服务提供方 remoteprovider.xml ,将服务引用部分放在服务消费方 remote-consumer.xml 。
并在提供方增加暴露服务配置
<dubbo:service> ,在消费方增加引用服务配置
<dubbo:reference> 。
remote-provider.xml:
<!-- 和本地服务一样实现远程服务 -->
<bean id=“xxxService” class=“com.xxx.XxxServiceImpl” />
<!-- 增加暴露远程服务配置 -->
<dubbo:service interface=“com.xxx.XxxService” ref=“xxxService” />
remote-consumer.xml:
<!-- 增加引用远程服务配置 -->
<dubbo:reference id=“xxxService” interface=“com.xxx.XxxService” />
<!-- 和本地服务一样使用远程服务 -->
<bean id=“xxxAction” class=“com.xxx.XxxAction”>
“xxxService” ref=“xxxService” />
二、SpringBoot集成dubbo
一、提供者:
1.实现服务:
1.@Service导出注解:
@Service //dubbo的注解public class AnnotationServiceImpl implements AnnotationService {
@Override public String sayHello(String name) {
return "annotation: hello, " + name;
}
}
2.添加应用程序共享配置:
# dubbo-provider.propertiesdubbo.application.name=annotation-providerdubbo.registry.address=zookeeper://127.0.0.1:2181dubbo.protocol.name=dubbodubbo.protocol.port=20880
3.扫描路径:
@Configuration@EnableDubbo(scanBasePackages = "org.apache.dubbo.samples.simple.annotation.impl")@PropertySource("classpath:/spring/dubbo-provider.properties")static public class ProviderConfiguration {
}
注:如果版本不支持@EnableDubbo注解,可以用spring.dubbo.scan配置:
spring.dubbo.application.name=pms-business-data
spring.dubbo.registry.address=172.25.54.155:2181,172.25.54.156:2181,172.25.54.157:2181
spring.dubbo.registry.protocol=zookeeper
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=8888
spring.dubbo.scan=net.hubs1.businessdata.api,net.hubs1.businessdata.service
spring.dubbo.application.registries.timeout=10000
spring.dubbo.application.registries.session=100000
二、消费者:
1.消费服务:@Reference
@Component("annotationAction")public class AnnotationAction {
@Reference private AnnotationService annotationService;
public String doSayHello(String name) {
return annotationService.sayHello(name);
}
}
2.添加应用程序共享配置
# dubbo-consumer.properties
dubbo.application.name=annotation-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.consumer.timeout=3000
3.扫描路径@EnableDubbo
@Configuration@EnableDubbo(scanBasePackages = "org.apache.dubbo.samples.simple.annotation.action")@PropertySource("classpath:/spring/dubbo-consumer.properties")@ComponentScan(value = {"org.apache.dubbo.samples.simple.annotation.action"})static public class ConsumerConfiguration {
}
三、调用服务:
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
context.start();
final AnnotationAction annotationAction = (AnnotationAction) context.getBean("annotationAction");
String hello = annotationAction.doSayHello("world");
}
三、原生Api使用Dubbo
1.提供方:
// ImplementationXxxService xxxService = new XxxServiceImpl();
// Application InfoApplicationConfig application = new ApplicationConfig();
application.setName("xxx");
// Registry InfoRegistryConfig registry = new RegistryConfig();
registry.setAddress("10.20.130.230:9090");
registry.setUsername("aaa");
registry.setPassword("bbb");
// ProtocolProtocolConfig protocol = new ProtocolConfig();
protocol.setName("dubbo");
protocol.setPort(12345);
protocol.setThreads(200);
// NOTES: ServiceConfig holds the serversocket instance and keeps connections to registry, please cache it for performance. // ExportingServiceConfig<XxxService> service = new ServiceConfig<XxxService>(); // In case of memory leak, please cache.service.setApplication(application);
service.setRegistry(registry); // Use setRegistries() for multi-registry caseservice.setProtocol(protocol); // Use setProtocols() for multi-protocol caseservice.setInterface(XxxService.class);
service.setRef(xxxService);
service.setVersion("1.0.0");
// Local export and registerservice.export();
2.消费者方:
// Application InfoApplicationConfig application = new ApplicationConfig();
application.setName("yyy");
// Registry InfoRegistryConfig registry = new RegistryConfig();
registry.setAddress("10.20.130.230:9090");
registry.setUsername("aaa");
registry.setPassword("bbb");
// NOTES: ReferenceConfig holds the connections to registry and providers, please cache it for performance. // Refer remote serviceReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // In case of memory leak, please cache.reference.setApplication(application);
reference.setRegistry(registry);
reference.setInterface(XxxService.class);
reference.setVersion("1.0.0");
// Use xxxService just like a local beanXxxService xxxService = reference.get();
3.方法级别的配置:
// Method level configList<MethodConfig> methods = new ArrayList<MethodConfig>();
MethodConfig method = new MethodConfig();
method.setName("createXxx");
method.setTimeout(10000);
method.setRetries(0);
methods.add(method);
// ReferringReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>();
...
reference.setMethods(methods);
4.点对点:
ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // If you know the address of the provider and want to bypass the registry, use `reference.setUrl()` to specify the provider directly. Refer [How to Invoke a specific provider](../demos/explicit-target.md) for details.reference.setUrl("dubbo://10.20.130.230:20880/com.xxx.XxxService");