FreeCMS商业版

功能强大、开源、代码通俗易懂、注释详细、面向二次开发友好!

承接网站建设、软件开发、微信开发项目,价格从优,欢迎咨询合作!

FreeCMS商业版V2.8已发布!购买请联系客服QQ 124812878 查看更新日志



在线演示 立即购买 下载免费版 下载商业版用户手册
客服QQ: 124812878 联系电话: 18339991503 Email: freeteam@foxmail.com

手写 dubbo(2017 最后一篇博客,回家相亲...) - Ctrl+C&Ctrl+V - ITeye博客

加入收藏夹】     【打印】     【关闭】 来源: 日期:2018-02-15 22:00:00 点击量: 收藏

手写 dubbo(2017 最后一篇博客,回家相亲...) 

 

下载demo

 

首先 介绍一下 dubbo大神的 博客地址  :   http:http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/javatar.iteye.comhttp:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/bloghttp:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/1123915

 

test



 

 

constructor



 

 

 

refer

public class SocketConsumer implements RpcRefer {
	 
	 @Override
		public <T> T refer(Class<T> interfaceClass, String host, int port) { 
		 return  (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(),  new Class<?>[] {interfaceClass}, new InvocationHandler() {
			
			@Override
			public Object invoke(Object proxy, Method method, Object[] args)
					throws Throwable {
				Socket socket = null;  
				 ObjectInputStream input = null;
	            try {   
	                socket = new Socket(host, port);    
	                DataOutputStream out = new DataOutputStream(socket.getOutputStream());    
	                InvokeEnity entity = new InvokeEnity();
	                entity.setServiceName(interfaceClass.getName());
	                entity.setMethodName(method.getName());
	                ArrayList<String>  parameterTypes= new ArrayList<String>();
	                for (int i = 0; i < method.getParameterTypes().length; i++) {
	                	Class<?> class1 = method.getParameterTypes()[i];
	                	parameterTypes.add(class1.getName());
					}
	                entity.setParameterTypes(parameterTypes);
	                entity.setParameters(CollectionUtils.arrayToList(args));
	                
	                out.writeUTF(FastJsonUtils.toJSONString(entity).toString());  
	                input = new ObjectInputStream(socket.getInputStream());    
	                Object serviceInvoke = input.readObject(); 
	                return serviceInvoke;
	               
	            } catch (Exception e) {   
	            	e.printStackTrace();
	            } finally {  
	                if (socket != null) {  
	                    try {  
	                        socket.close();  
	                    } catch (IOException e) {  
	                        socket = null;      
	                    }  
	                }  
	                if (input != null) {  
	                    try {  
	                    	input.close();  
	                    } catch (IOException e) {  
	                    	input = null;      
	                    }  
	                }  
	            } 
				return null;
			}
		});  
		} 
}

 

 

export

public class SocketProducter implements RpcExport {
	
	@Override
	 public void export(Object service, int port ) {    
	        try {     
	            ServerSocket serverSocket = new ServerSocket(port);    
	            while (true) {    
	                Socket client = serverSocket.accept();    
	                new HandlerThread(client,service);    
	            }    
	        } catch (Exception e) {    
	        	e.printStackTrace();
	        }    
	    }    
	    
	    private class HandlerThread implements Runnable {    
	        private Socket socket;   
	        private Object service ;
	        public HandlerThread(Socket client,Object service) {    
	            socket = client;    
	            this.service = service;
	            new Thread(this).start();    
	        }    
	    
	        public void run() {    
	            try {    
	                DataInputStream input = new DataInputStream(socket.getInputStream());  
	                String request = input.readUTF(); 
	                Object execute = null;
	               if( FastJsonUtils.isJson(request)){ 
	            	   InvokeEnity entityEnity = InvokeEnity.transefer(request);
						String methodName = entityEnity.getMethodName();
						List<String> parameterTypeses = entityEnity.getParameterTypes();
						List<String> parameteres = entityEnity.getParameters();
						Class<?>[] parameterTypes = new Class<?>[parameterTypeses.size()];
						Object[] parameters = new Object[parameterTypeses.size()];
						for (int i = 0; i <parameterTypeses.size(); i++) {
							parameterTypes[i] = Class.forName(parameterTypeses.get(i));
							parameters[i] = parameteres.get(i);
						}  
						execute = CGLibUtils.execute(service, methodName, parameterTypes, parameters);
						
						http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/* Method method = service.getClass().getMethod(methodName, parameterTypes);  
						 execute = method.invoke(service, parameters);  *http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/
                         
	               }
	               
					
					
	                ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());  
	                out.writeObject(execute);
	                out.close();    
	                input.close();    
	            } catch (Exception e) {    
	            	e.printStackTrace();
	            } finally {    
	                if (socket != null) {    
	                    try {    
	                        socket.close();    
	                    } catch (Exception e) {    
	                        socket = null;    
	                    }    
	                }    
	            }   
	        }    
	    }

		

	
}

 

 

 

public interface UserService {

	 public User getUser(String name);

}

 

public class UserServiceImpl implements UserService {

	@Override
	public User getUser(String name) { 
		User user = new User();
		user.setName(name);
		user.setPassword(name +"1213212");
		return user;
	}

	 

}

 

 

producter 提供者

public class ProducterTest {

	  public static void main(String[] args) {
			RpcFactory rpc = RpcFactory.getInstanceByStrategy("simpleIO");
			RpcExport rpcExport = rpc.getRpcExport();
			UserService service = new UserServiceImpl();
			rpcExport.export(service, Constants.PORT);
	}

}

 

 

consumer消费者

public class ConsumerTest {

	 public static void main(String[] args) {
		 RpcFactory rpc = RpcFactory.getInstanceByStrategy("simpleIO"); 
			RpcRefer rpcRefer = rpc.getRpcRefer();
			UserService userService = rpcRefer.refer(UserService.class, Constants.IP_ADDR, Constants.PORT);
			for (int i = 0; i < 10; i++) {
				User user = userService.getUser("baoyou");
				System.out.println(FastJsonUtils.toJSONString(user).toString());
			}
			 
	}

}

 

 



 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者 

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(支持支付宝和微信 以及扣扣群),没钱捧个人场,谢谢各位。

 

个人主页http:http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/



 
 
 谢谢您的赞助,我会做的更好!


上一条


下一条

相关新闻
自定义表单

本例固定链接: 分享到:
更多