zoukankan      html  css  js  c++  java
  • 简单的网络调用

    自己在github上找的项目,是springforall社区的项目,自己写写,练练手

    package com.fh.rpc;
    
    public interface HelloService {
        
        public String hello(String message);
    
    }
    package com.fh.rpc;
    
    public class HelloServiceImpl implements HelloService{
    
        @Override
        public String hello(String message) {
            return message+",it is Ok";
        }
    
        
    
    }
    package com.fh.rpc;
    
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.lang.reflect.Method;
    import java.net.InetSocketAddress;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    
    public class Publish {
    
        static ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
        
        public static void publish(String host,int port) throws IOException {
            ServerSocket server = new ServerSocket();
            server.bind(new InetSocketAddress(host, port));
            try {
                while(true) {
                    pool.execute(new ExecutorTask(server.accept()));    
                }
            }catch (Exception e) {
                e.printStackTrace();
            }finally {
                server.close();
            }
        }
        
        private static class ExecutorTask implements Runnable{
            
            public Socket socket;
            
            public ExecutorTask(Socket socket) {
                this.socket=socket;
            }
    
            @Override
            public void run() {
                ObjectInputStream input = null;
                ObjectOutputStream output = null;
                try {
                    input = new ObjectInputStream(socket.getInputStream());
                    
                    String interfaceName = input.readUTF();
                    String methodName = input.readUTF();
                    Class<?>[] paramTypes = (Class<?>[])input.readObject();
                    Object[] arguments = (Object[])input.readObject();
                    Class<?> service = Class.forName(interfaceName);
                    Method method = service.getMethod(methodName, paramTypes);
                    Object result = method.invoke(service.newInstance(), arguments);
                    
                    output = new ObjectOutputStream(socket.getOutputStream());
                    output.writeObject(result);
                }catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    if(output!=null) {
                        try {
                            output.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if(input!=null) {
                        try {
                            input.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if(socket!=null) {
                        try {
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            
        }
    
    }
    package com.fh.rpc;
    
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    import java.net.InetSocketAddress;
    import java.net.Socket;
    
    public class CallRemote<S> {
    
        @SuppressWarnings("unchecked")
        public S callRemote(final Class<?> serviceClass,final InetSocketAddress address) {
            return (S)Proxy.newProxyInstance(serviceClass.getClassLoader(), new Class<?>[]{serviceClass.getInterfaces()[0]}, new InvocationHandler() {
                
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    Socket socket = null;
                    ObjectOutputStream output = null;
                    ObjectInputStream input = null;
                    try {
                        socket = new Socket();
                        socket.connect(address);
                        
                        output = new ObjectOutputStream(socket.getOutputStream());
                        output.writeUTF(serviceClass.getName());
                        output.writeUTF(method.getName());
                        output.writeObject(method.getParameterTypes());
                        output.writeObject(args);
                        
                        input = new ObjectInputStream(socket.getInputStream());
                        return input.readObject();
                    }catch (Exception e) {
                        e.printStackTrace();
                        return null;
                    }finally {
                        if(socket!=null) {
                            socket.close();
                        }
                        if(input!=null) {
                            input.close();
                        }
                        if(output!=null) {
                            output.close();
                        }
                    }
                }
            });
        }
    }
    package com.fh.rpc;
    
    import java.io.IOException;
    import java.net.InetSocketAddress;
    
    public class RpcTest {
    
        public static void main(String[] args) {
            new Thread(new Runnable() {
                
                @Override
                public void run() {
                    try {
                        Publish.publish("localhost", 8088);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
            
            CallRemote<HelloService> call = new CallRemote<>();
            
            HelloService hello = call.callRemote(HelloServiceImpl.class, new InetSocketAddress("localhost",8088));
            System.out.println(hello.hello("fenghao"));
        }
    
    }
  • 相关阅读:
    第三方登录(QQ登录)开发流程详解
    编译PHP并与Ngnix整合
    Ngnix的日志管理和用定时任务完成日志切割
    Ngnix 安装、信号量、虚拟主机配置
    Redis命令操作详解
    Redis的安装和部署
    消息队列
    Ubuntu中Google Chrome安装
    关于双系统下Ubuntu不能访问Windows中某个盘的问题
    numpy.random.shuffle()与numpy.random.permutation()的区别
  • 原文地址:https://www.cnblogs.com/nihaofenghao/p/9627458.html
Copyright © 2011-2022 走看看