不灭的焱

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

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

进入了基本的测试篇,首先我们先进入基本的代码测试,我们先跑通代码:

 

首先我们先写两个简单的接口:

HelloService.java

package org.laopopo.example.demo.service;

/**
 * 
 * @author BazingaLyn
 * @description 
 * @time
 * @modifytime
 */
public interface HelloSerivce {
	
	String sayHello(String str);

}

 

ByeService.java

package org.laopopo.example.demo.service;

public interface ByeService {
	
	String sayBye(String str);

}

 

给出具体的实现:

HelloSerivceImpl.java

package org.laopopo.example.demo.service;

import org.laopopo.client.annotation.RPCService;

/**
 * 
 * @author BazingaLyn
 * @description Demo
 * @time 2016年8月19日
 * @modifytime
 */
public class HelloSerivceImpl implements HelloSerivce {

	@Override
	@RPCService(responsibilityName="xiaoy",
				serviceName="LAOPOPO.TEST.SAYHELLO",
				isVIPService = false,
				isSupportDegradeService = true,
				degradeServicePath="org.laopopo.example.demo.service.HelloServiceMock",
				degradeServiceDesc="默认返回hello")
	public String sayHello(String str) {
		
		//真实逻辑可能涉及到查库
		return "hello "+ str;
		
	}

}

 

它对应的mock类HelloServiceMock.java

package org.laopopo.example.demo.service;

public class HelloServiceMock implements HelloSerivce {

	@Override
	public String sayHello(String str) {
		
		//直接给出默认的返回值
		return "hello";
	}

}

 

ByeServiceImpl.java

package org.laopopo.example.demo.service;

import org.laopopo.client.annotation.RPCService;

public class ByeServiceImpl implements ByeService {

	@Override
	@RPCService(responsibilityName="fly100%",serviceName ="LAOPOPO.TEST.SAYBYE",isVIPService = true,isSupportDegradeService = false)
	public String sayBye(String str) {
		return "bye " + str;
	}

}

 

接下来我们看看服务提供者的配置:

 

package org.laopopo.example.generic.test_1;

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;
import org.laopopo.remoting.netty.NettyClientConfig;
import org.laopopo.remoting.netty.NettyServerConfig;

public class ProviderTest {

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

		DefaultProvider defaultProvider = new DefaultProvider(new NettyClientConfig(), new NettyServerConfig());

		defaultProvider.registryAddress("127.0.0.1:18010") // 注册中心的地址
				.serviceListenPort(8899) // 暴露服务的端口
				.publishService(new HelloSerivceImpl(), new ByeServiceImpl()) // 暴露的服务
				.start(); // 启动服务

	}

}

 

注册中心的测试类:

 

package org.laopopo.example.generic.test_1;

import io.netty.util.internal.ConcurrentSet;

import java.util.concurrent.ConcurrentMap;

import org.laopopo.base.registry.DefaultRegistryServer;
import org.laopopo.base.registry.RegistryServerConfig;
import org.laopopo.common.rpc.RegisterMeta;
import org.laopopo.common.rpc.RegisterMeta.Address;
import org.laopopo.remoting.netty.NettyServerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RegistryTest {
	
	private static final Logger logger = LoggerFactory.getLogger(RegistryTest.class);
	
	private static DefaultRegistryServer defaultRegistryServer;
	
	public static void main(String[] args) {
		
		Thread t = new Thread(new RegistryTest1Scanner(), "timeout.scanner");
        t.setDaemon(true);
        t.start();
        
		NettyServerConfig config = new NettyServerConfig();
		RegistryServerConfig registryServerConfig = new RegistryServerConfig();
		//注册中心的端口号
		config.setListenPort(18010);
		defaultRegistryServer = new DefaultRegistryServer(config,registryServerConfig);
		defaultRegistryServer.start();
		
	}
	
	
	private static class RegistryTest1Scanner implements Runnable {

        @Override
        public void run() {
        	
            for (;;) {
                try {
                	logger.info("统计中");
                	Thread.sleep(10000);
                	ConcurrentMap<String, ConcurrentMap<Address, RegisterMeta>>  concurrentMap = defaultRegistryServer.getProviderManager().getGlobalRegisterInfoMap();
                    if(null != concurrentMap){
                    	for(String serviceName:concurrentMap.keySet()){
                    		ConcurrentMap<Address, RegisterMeta> map = concurrentMap.get(serviceName);
                    		if(map != null){
                    			for(Address address : map.keySet()){
                    				logger.info("serviceName [{}] address [{}] and detail [{}]",serviceName,address,map.get(address).toString());
                    			}
                    		}
                    	}
                    }
                    
                    ConcurrentMap<Address, ConcurrentSet<String>>  serviceMap = defaultRegistryServer.getProviderManager().getGlobalServiceMetaMap();
                    if(null != serviceMap){
                    	for(Address address : serviceMap.keySet()){
                    		if(null != serviceMap.get(address)){
                    			for(String str : serviceMap.get(address)){
                    				logger.info("address [{}] provider serivcename [{}]",address,str);
                    			}
                    		}
                    	}
                    }
                } catch (Throwable t) {
                    logger.error("An exception has been caught while scanning the timeout acknowledges {}.", t);
                }
            }
        }
    }

}

 

服务消费者测试类:

 

package org.laopopo.example.generic.test_1;

import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;

import org.laopopo.client.consumer.Consumer.SubscribeManager;
import org.laopopo.client.consumer.ConsumerClient;
import org.laopopo.client.consumer.ConsumerConfig;
import org.laopopo.common.utils.ChannelGroup;
import org.laopopo.remoting.netty.NettyClientConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConsumerTest {

	private static final Logger logger = LoggerFactory.getLogger(ConsumerTest.class);
	
	private static ConsumerClient commonclient;

	public static void main(String[] args) throws Throwable {

		Thread t = new Thread(new SubcribeResultScanner(), "timeout.scanner");
		t.setDaemon(true);
		t.start();

		NettyClientConfig registryNettyClientConfig = new NettyClientConfig();
		registryNettyClientConfig.setDefaultAddress("127.0.0.1:18010");

		NettyClientConfig provideClientConfig = new NettyClientConfig();

		ConsumerClient client = new ConsumerClient(registryNettyClientConfig, provideClientConfig, new ConsumerConfig());

		client.start();

		commonclient = client;
		SubscribeManager subscribeManager = client.subscribeService("LAOPOPO.TEST.SAYHELLO");

		if (!subscribeManager.waitForAvailable(3000l)) {
			throw new Exception("no service provider");
		}

		Object obj = client.call("LAOPOPO.TEST.SAYHELLO", "shine");
		if (obj instanceof String) {
			System.out.println((String) obj);
		}
		
		SubscribeManager subscribeManager2 = client.subscribeService("LAOPOPO.TEST.SAYBYE");
		
		if (!subscribeManager2.waitForAvailable(3000l)) {
			throw new Exception("no service provider");
		}

		Object obj1 = client.call("LAOPOPO.TEST.SAYBYE", "shine");
		if (obj1 instanceof String) {
			System.out.println((String) obj1);
		}

	}

	private static class SubcribeResultScanner implements Runnable {

		@Override
		public void run() {

			for (;;) {
				try {
					logger.info("统计中");
					Thread.sleep(10000);
					
					@SuppressWarnings("static-access")
					ConcurrentMap<String, CopyOnWriteArrayList<ChannelGroup>> result = commonclient.getGroups();
					if(result != null){
						for(String serviceName:result.keySet()){
							System.out.println(serviceName);
							for(ChannelGroup channelGroup :result.get(serviceName)){
								System.out.println(channelGroup.toString());
							}
						}
					}
				} catch (Throwable t) {
					logger.error("An exception has been caught while scanning the timeout acknowledges {}.", t);
				}
			}
		}
	}

}

 

现在一一启动服务,首先先启动注册中心:

 

然后运行ProviderTest的main函数:

ProviderTest的运行控制台打印:

 

 

我们再回头看看注册中心的打印:

 

 

最后我们启动ConsumerTest的main函数类:

 

程序基本上跑通了~接下来我们测试没有注册中心,服务消费者直连服务提供者的测试

 

测试源代码请详见:

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

 

 

摘自:https://blog.csdn.net/linuu/article/details/52795314