欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

手写 dubbo

程序员文章站 2022-05-22 08:36:57
...

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

 

下载demo

 

首先 介绍一下 dubbo大神的 博客地址  :   http://javatar.iteye.com/blog/1123915

 

test


手写 dubbo
            
    
    博客分类: dubbojava 手写dubbospringjava 
 

 

constructor


手写 dubbo
            
    
    博客分类: dubbojava 手写dubbospringjava 
 

 

 

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);
						
						/* Method method = service.getClass().getMethod(methodName, parameterTypes);  
						 execute = method.invoke(service, parameters);  */
                         
	               }
	               
					
					
	                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://knight-black-bob.iteye.com/


手写 dubbo
            
    
    博客分类: dubbojava 手写dubbospringjava 手写 dubbo
            
    
    博客分类: dubbojava 手写dubbospringjava 手写 dubbo
            
    
    博客分类: dubbojava 手写dubbospringjava 
 
 
 谢谢您的赞助,我会做的更好!

  • 手写 dubbo
            
    
    博客分类: dubbojava 手写dubbospringjava 
  • 大小: 59 KB
  • 手写 dubbo
            
    
    博客分类: dubbojava 手写dubbospringjava 
  • 大小: 76.3 KB