在公司里使用微服务后觉得自己不应该只会用。还要了解一下底层的东西。所以就手写了一篇最简单的微服务。作为介绍微服务的开始。代码主要分以下结构
启动顺序:
1.先启动Server服务端
将服务注册到注册中心ReqsterService,并对外暴露服务
2.启动App客户端
即可完成调用。
package io.mytest.service;
import java.util.HashMap;
//注册服务
public class RegsterService {
public static HashMap<String, Class> regsterService =new HashMap<>();//注册中心
/**
*
* @param interfaceName 服务接口名
* @param serviceName 服务实现类名
*/
public static void regsterService(String interfaceName, Class interfaceImpl){
System.out.println("注册服务:"+interfaceName+"实现服务"+interfaceImpl.getName());
regsterService.put(interfaceName, interfaceImpl);//注册服务
}
}
package io.mytest.service;
import java.util.HashMap;
//注册服务
public class RegsterService {
public static HashMap<String, Class> regsterService =new HashMap<>();//注册中心
/**
*
* @param interfaceName 服务接口名
* @param serviceName 服务实现类名
*/
public static void regsterService(String interfaceName, Class interfaceImpl){
System.out.println("注册服务:"+interfaceName+"实现服务"+interfaceImpl.getName());
regsterService.put(interfaceName, interfaceImpl);//注册服务
}
}
package io.mytest.service;
import java.io.Serializable;
public class HelloServiceImpl implements HelloService,Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String sayHello(String name) {
return "尊敬的会员"+name+"您好";
}
}
package io.mytest.service;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
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 Server {
//线程处理接收到的请求
private static ExecutorService executorService = Executors.newFixedThreadPool(10);
@SuppressWarnings("resource")
public void start(int port){
//1.注册服务
RegsterService.regsterService(HelloService.class.getName(), HelloServiceImpl.class);
//2.启动服务
try {
final ServerSocket serverSocket = new ServerSocket(port);
//serverSocket.bind(new InetSocketAddress(port));
while (true) {
executorService.execute(new Runnable() {
@Override
public void run() {
Socket clientScoket =null;
InputStream inputStream =null;
ObjectInputStream objectInputStream =null;
ObjectOutputStream objectOutputStream =null;
try {
clientScoket = serverSocket.accept();
System.out.println("服务端收到请求");
OutputStream outputStream = clientScoket.getOutputStream();
objectOutputStream = new ObjectOutputStream(outputStream);
inputStream = clientScoket.getInputStream();
objectInputStream = new ObjectInputStream(inputStream);
//服务名
String serviceName = objectInputStream.readUTF();
//方法名
String methodName = objectInputStream.readUTF();
Class<?>[] parameterTypes = (Class<?>[]) objectInputStream.readObject();
Object[] args = (Object[]) objectInputStream.readObject();
//查找服务
Class<?> serviceClass = RegsterService.regsterService.get(serviceName);
if (serviceClass==null) {
throw new ClassNotFoundException("服务未找到"+serviceName);
}
//System.out.println("name"+serviceName);
Method method = serviceClass.getMethod(methodName, parameterTypes);
Object result = method.invoke(serviceClass.newInstance(), args);
System.out.println("服务端打印结果"+(String)result);
objectOutputStream.writeObject(result);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
objectOutputStream.close();
objectInputStream.close();
clientScoket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
Server server = new Server();
server.start(8082);
System.out.println("服务启动");
}
}
package io.mytest.service;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.Socket;
public class Client {
@SuppressWarnings("unchecked")
public static <T> T getClient(final Class interfaceClass ,final String host,final int port){
T t =(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;
InputStream inputStream =null;
ObjectInputStream objectInputStream =null;
ObjectOutputStream objectOutputStream =null;
try {
socket = new Socket(host, port);
OutputStream outputStream = socket.getOutputStream();
objectOutputStream = new ObjectOutputStream(outputStream);
inputStream = socket.getInputStream();
objectInputStream = new ObjectInputStream(inputStream);
//服务名
String serviceName = interfaceClass.getName();
//方法名
String methodName = method.getName();
//方法的参数类型
Class<?>[] parameterTypes = method.getParameterTypes();
//方法参数 args
objectOutputStream.writeUTF(serviceName);
objectOutputStream.writeUTF(methodName);
objectOutputStream.writeObject(parameterTypes);
objectOutputStream.writeObject(args);
Object readObject = objectInputStream.readObject();
return readObject;
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
objectInputStream.close();
objectOutputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
});
return t;
}
}
package io.mytest.service;
public class App {
public static void main(String[] args) {
System.out.println("客户端调用开始");
HelloService client = Client.getClient(HelloService.class, "192.168.20.123", 8082);
System.out.println(client.sayHello("测试人员"));
System.out.println("客户端调用结束");
}
}