您的位置:首页 > 编程语言 > Java开发

dubbo源码 学习笔记(四)

2017-10-26 10:49 330 查看
dubbo 通讯协议之injvm

public class Provider1 {
public static void main(String[] args) throws IOException {
ApplicationConfig app = new ApplicationConfig("provider");

List<RegistryConfig> registries = new ArrayList<RegistryConfig>(10);
registries.add(new RegistryConfig("redis://127.0.0.1:6379"));
registries.add(new RegistryConfig("zookeeper://127.0.0.1:2181"));

List<ProtocolConfig> protocols = new ArrayList<ProtocolConfig>(10);
protocols.add(new ProtocolConfig("dubbo", 20880));
protocols.add(new ProtocolConfig("injvm", 20000));

ServiceConfig<Object> service = new ServiceConfig<Object>();
service.setInterface(HelloService.class);
service.setRef(new HelloServiceImpl());
service.setProtocols(protocols);
service.setRegistries(registries);
service.setApplication(app);
service.export();

System.out.println("success");
System.in.read(); // 按任意键退出
}
}


生产者使用两个注册中心 redis 和 zookeeper的

分别对外提供两种协议的接口 dubbo 和 injvm

public class Consumer1 {
public static void main(String[] args) throws IOException {
ApplicationConfig application = new ApplicationConfig("consumer");

List<RegistryConfig> registries = new ArrayList<RegistryConfig>(10);
registries.add(new RegistryConfig("redis://127.0.0.1:6379"));
registries.add(new RegistryConfig("zookeeper://127.0.0.1:2181"));

ReferenceConfig<HelloService> reference = new ReferenceConfig<HelloService>();
reference.setApplication(application);
reference.setRegistries(registries);
reference.setInterface(HelloService.class);
//使用injvm协议
reference.setScope(Constants.SCOPE_LOCAL);

HelloService helloService = reference.get();
System.out.println(reference.getProtocol());

System.out.println(helloService.say("hahah"));

}
}


当我们使用另一个进程启动的时候 这里会抛出异常

因为两个进程不是同一个虚拟机 所以不能使用injvm协议

我们通过修改代码 在生产者中启动一个线程 去运行消费者 这样两个就会在同一个虚拟机中

public class Provider1 {
public static void main(String[] args) throws IOException {
ApplicationConfig app = new ApplicationConfig("provider");

List<RegistryConfig> registries = new ArrayList<RegistryConfig>(10);
registries.add(new RegistryConfig("redis://127.0.0.1:6379"));
registries.add(new RegistryConfig("zookeeper://127.0.0.1:2181"));

List<ProtocolConfig> protocols = new ArrayList<ProtocolConfig>(10);
protocols.add(new ProtocolConfig("dubbo", 20880));
protocols.add(new ProtocolConfig("injvm", 20000));

ServiceConfig<Object> service = new ServiceConfig<Object>();
service.setInterface(HelloService.class);
service.setRef(new HelloServiceImpl());
service.setProtocols(protocols);
service.setRegistries(registries);
service.setApplication(app);
service.export();

new Thread(new Consumer()).start();

System.out.println("success");
System.in.read(); // 按任意键退出
}
static class Consumer implements Runnable{

public void run() {
ApplicationConfig application = new ApplicationConfig("consumer");

List<RegistryConfig> registries = new ArrayList<RegistryConfig>(10);
registries.add(new RegistryConfig("redis://127.0.0.1:6379"));
registries.add(new RegistryConfig("zookeeper://127.0.0.1:2181"));

ReferenceConfig<HelloService> reference = new ReferenceConfig<HelloService>();
reference.setApplication(application);
reference.setRegistries(registries);
reference.setInterface(HelloService.class);
//使用injvm协议
reference.setScope(Constants.SCOPE_LOCAL);

HelloService helloService = reference.get();

System.out.println(helloService.say("hahah"));
}

}
}


此时 运行正常



在dubbo源码中有这么个注释 ,发现结果并不是这样的,在去不到jvm的情况下不会去使用别的协议



关于injvm的get set方法已经被淘汰 推荐使用 socpe



但是这里是这样注释的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java dubbo 源码