前言
《dubbo之集群》一文所提到功能已经基本完善,dubbo如何进一步封装,使用之简单易用呢?
服务代理
通过《浅谈AOP》中的Javassist技术,dubbo为我们简化代码如下
public class ProxyTest {
public static void main(String[] args) throws Exception {
ServiceRepository repository = ApplicationModel.getServiceRepository();
repository.registerService(GreetingsService.class);
URL url = URL.valueOf("dubbo://127.0.0.1:28092/dubbo.GreetingsService?monitor=mm&timeout=12000");
Protocol protocol = new ProtocolFilterWrapper(new dubboProtocol());
protocol.export(new JdkProxyFactory().getInvoker(new GreetingsServiceImpl(), GreetingsService.class, url));
Invoker<GreetingsService> invoker = protocol.refer(GreetingsService.class, url);
invoker = Cluster.getCluster(null, false).join(new StaticDirectory(Lists.newArrayList(invoker)));
GreetingsService greetingsService = new JdkProxyFactory().getProxy(invoker, new Class[]{ GreetingsService.class});
System.out.println( greetingsService.sayHi("yoyo"));
}
}
服务配置
public class ConfigTest {
public static void main(String[] args) throws InterruptedException {
// 基本信息全局配置
ConfigManager configManager = ApplicationModel.getConfigManager();
configManager.setApplication(new ApplicationConfig("test"));
// 注册中心全局配置
RegistryConfig registryConfig = new RegistryConfig("127.0.0.1", "zookeeper");
registryConfig.setPort(2181);
configManager.addRegistry(registryConfig);
// 服务提供配置
ServiceConfig<GreetingsService> serviceConfig = new ServiceConfig();
serviceConfig.setInterface(GreetingsService.class);
serviceConfig.setRef(new GreetingsServiceImpl());
serviceConfig.export();
// 服务调用配置
ReferenceConfig<GreetingsService> referenceConfig = new ReferenceConfig();
referenceConfig.setInterface(GreetingsService.class);
referenceConfig.setProtocol("dubbo");
GreetingsService greetingsService = referenceConfig.get();
System.out.println(greetingsService.sayHi("rrrrrr"));
}
}
除了上述JavaBean的启动方式dubbo还提供了spring的注解及配置文件方式完成以上配置。以配置文件方式为例子:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/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">
<!--定义了提供方应用信息,用于计算依赖关系;在 dubbo-admin 或 dubbo-monitor 会显示这个名字,方便辨识-->
<dubbo:application name="test"/>
<!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!--使用 dubbo 协议实现定义好的 api.PermissionService 接口-->
<dubbo:service interface="dubbo.GreetingsService" ref="greetingsServiceImpl" protocol="dubbo" />
<!--具体实现该接口的 bean-->
<bean id="greetingsServiceImpl" class="dubbo.GreetingsServiceImpl"/>
<dubbo:reference id="greetingsService" interface="dubbo.GreetingsService"/>
</beans>
public class SpringdubboTest {
public static void main(String[] args) throws InterruptedException {
ClasspathXmlApplicationContext context=new ClasspathXmlApplicationContext("spring-dubbo.xml");
GreetingsService greetingsService = (GreetingsService) context.getBean("greetingsService");
System.out.println(greetingsService.sayHi("oyooo"));
}
}
标签名称 | 类名 |
---|---|
dubbo:application | ApplicationConfig |
dubbo:module | ModuleConfig |
dubbo:registry | RegistryConfig |
dubbo:monitor | MonitorConfig |
dubbo:provider | ProviderConfig |
dubbo:consumer | ConsumerConfig |
dubbo:protocol | ProtocolConfig |
dubbo:service | ServiceBean |
dubbo:reference | ReferenceBean |
- service服务提供者启动流程
- 消费者启动时
总结
- 服务启动时,先是服务准备好并且本地爆露完再向注册中心提交连接。
- 防止过早的向注册中心写入,请求找不到服务.
- @see RegistryProtocol.export
- 阻止新请求过来,然后再等待正在运行的请求处理完成
- @see RegistryProtocol.ExporterChangeableWrapper.unexport
主要参考
《源码分析Dubbo前置篇-寻找注册中心、服务提供者、服务消费者功能入口》
《源码分析Dubbo服务消费端启动流程》
《源码分析Dubbo服务提供者启动流程》
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。