不灭的焱

革命尚未成功,同志仍须努力

作者:php-note.com  发布于:2018-11-04 20:26  分类:Java库/系统 

因为在某些场合下,服务提供者和服务消费者是直接可以调用的,不需要通过注册中心,本小节测试直连的调用:

 

服务提供者的代码:

 

package org.laopopo.example.generic.test_4;

import org.laopopo.client.provider.DefaultProvider;
import org.laopopo.common.exception.remoting.RemotingException;
import org.laopopo.example.demo.service.ByeServiceImpl;
import org.laopopo.example.demo.service.HelloSerivceImpl;

public class ProviderTest {

	public static void main(String[] args) throws InterruptedException, RemotingException {

		DefaultProvider defaultProvider = new DefaultProvider();

		defaultProvider.serviceListenPort(8899) // 暴露服务的地址
				.publishService(new HelloSerivceImpl(), new ByeServiceImpl()) // 暴露的服务
				.start(); // 启动服务

	}

}

 

服务消费者的代码:

 

package org.laopopo.example.generic.test_4;

import org.laopopo.client.consumer.ConsumerClient;
import org.laopopo.client.consumer.proxy.ProxyFactory;
import org.laopopo.common.utils.UnresolvedAddress;

/**
 * 
 * @author BazingaLyn
 * @description 测试consumer直连provider,进行服务调用
 * @time
 * @modifytime
 */
public class ConsumerTest {
	
	public static void main(String[] args) throws Exception {
		

		ConsumerClient client = new ConsumerClient();

		client.start();
		
		UnresolvedAddress addresses = new UnresolvedAddress("127.0.0.1", 8899);
		
		HelloService helloService = ProxyFactory.factory(HelloService.class).consumer(client).addProviderAddress(addresses).timeoutMillis(3000l).newProxyInstance();
		
		String str = helloService.sayHello("Lyncc");
			
		System.out.println(str);
		
		
	}

}

 

先运行ProviderTest.java

 


 

再运行ConsumerTest.java

 

 

本章节的测试代码:

https://github.com/BazingaLyn/laopopo-rpc/tree/master/laopopo-example/src/main/java/org/laopopo/example/generic/test_4

 

基本上是没有问题,下一个章节,是关于注册中心的测试