dubbo源码 学习笔记(四)

本文探讨了Dubbo的InjVM通讯协议,指出由于不同进程无法在同一虚拟机内运行,因此通常不适用。通过实验,发现在无法使用InjVM时,Dubbo并不会自动切换到其他协议。尽管InjVM的get/set方法已不推荐,注释却暗示了在特定情况下可能的行为。
摘要由CSDN通过智能技术生成

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


但是这里是这样注释的









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值