手写 dubbo(2017 最后一篇博客,回家相亲...)_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 手写 dubbo(2017 最后一篇博客,回家相亲...)

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

 2018/2/9 18:56:06  knight_black_bob  程序员俱乐部  我要评论(0)
  • 摘要:手写dubbo(2017最后一篇博客,回家相亲...)下载:demo首先介绍一下dubbo大神的博客地址:http://javatar.iteye.com/blog/1123915testconstructorreferpublicclassSocketConsumerimplementsRpcRefer{@Overridepublic<T>Trefer(Class<T>interfaceClass,Stringhost,intport){return(T)Proxy
  • 标签:博客

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

?

下载 :demo

?

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

?

test



?

?

constructor



?

?

?

refer

class="java" name="code">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/



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

  • 大小: 59 KB
  • 大小: 76.3 KB
  • 查看图片附件
发表评论
用户名: 匿名