上一节中,我们使用JDK序列化和Socket实现了一个最基本的RPC框架,服务端测试时是这样的
public class TestServer {
public static void main(String[] args) {
HelloService helloService = new HelloServiceImpl();
RpcServer rpcServer = new RpcServer();
rpcServer.register(helloService,9000);
}
}
在注册完helloService后,服务器就自行启动了,也就是说,一个服务器只能注册一个服务,在这一节,我们将服务的注册和服务器启动分离,使得服务器可以提供多个服务。
服务注册表
我们需要一个容器,这个容器很简单,就是保存一些本地服务的信息,并且在获得一个服务名字的时候能够返回这个服务的信息,创建一个ServiceRegistry接口,一个registry注册服务信息,一个getService获取服务信息。
public interface ServiceRegistry {
<T> void register(T service);
Object getService(String serviceName);
}
我们新建一个默认的注册表类DefaultServiceRegistry来实现这个接口,提供服务注册服务:
其中,concurrenthashmap用法请见:简单总结ConcurrentHashMap - 简书
public class DefaultServiceRegistry implements ServiceRegistry{
private static final Logger logger = LoggerFactory.getLogger(DefaultServiceRegistry.class);
private final Map<String,Object> serviceMap = new ConcurrentHashMap<>();
private final Set<String> registeredService = ConcurrentHashMap.newKeySet();
public synchronized <T> void register(T service) {
String serviceName = service.getClass().getCanonicalName();//返回一个完整的类名
if(registeredService.contains(serviceName)) return;
registeredService.add(serviceName);
Class<?>[] interfaces = service.getClass().getInterfaces();
if(interfaces.length==0){
throw new RuntimeException();
}
for(Class<?> i:interfaces){
serviceMap.put(i.getCanonicalName(),service);
}
logger.info("向接口:{} 注册服务:{}",interfaces,serviceName);
}
public synchronized Object getService(String serviceName){
Object service = serviceMap.get(serviceName);
if(service==null){
throw new RuntimeException();
}
return service;
}
}
我们将服务名与提供服务的对象的对应关系保存在一个ConcurrentHashMap中,并且使用一个Set来保存当前有哪些对象已经被注册,在注册服务时,默认采用这个对象实现的接口的完整类名作为服务名,例如某个对象A实现了接口X和Y,那么将A注册进去后,会有两个服务名X和Y对应于A对象,这种处理方式也就说明了某个接口只能有一个对象提供服务。
获得服务的对象,直接去Map里查找就可以了。
其他处理
为了降低耦合度,我们不会把ServiceRegistry和某一个RpcServer绑定在一起,而是在创建Rpcserver对象时,传入一个ServiceRegistry作为这个服务的注册表
那么RpcServer这个类现在就变成了这样:
public class RpcServer {
private final ExecutorService threadPool;
private static final Logger logger = LoggerFactory.getLogger(RpcServer.class);
private static final int CORE_POOL_SIZE = 5;
private static final int MAXIMUM_POOL_SIZE = 50;
private static final int KEEP_ALIVE_TIME = 60;
private static final int BLOCKING_QUEUE_CAPACITY = 100;
private RequestHandler requestHandler = new RequestHandler();
private final ServiceRegistry serviceRegistry;
public RpcServer(ServiceRegistry serviceRegistry) {
this.serviceRegistry = serviceRegistry;
BlockingQueue<Runnable> workingQueue = new ArrayBlockingQueue<>(BLOCKING_QUEUE_CAPACITY);
ThreadFactory threadFactory = Executors.defaultThreadFactory();
threadPool = new ThreadPoolExecutor(CORE_POOL_SIZE,MAXIMUM_POOL_SIZE,KEEP_ALIVE_TIME,TimeUnit.SECONDS,workingQueue,threadFactory);
}
public void start(int port){
try(ServerSocket serverSocket = new ServerSocket(port)){
logger.info("服务启动中");
Socket socket;
while((socket = serverSocket.accept())!=null){
logger.info("消费者连接:{}:{}",socket.getInetAddress(),socket.getPort());
threadPool.execute(new RequestHandlerThread(socket,requestHandler,serviceRegistry));
}
threadPool.shutdown();
} catch (IOException e) {
logger.error("服务启动时有错误发生:",e);
}
}
}
在创建RpcServer时需要传入一个已经注册好服务的ServiceRegistry,而原来的registry方法也被改成了start方法,因为服务的注册已经不由RpcServer处理了,他只需要启动就可以了。
而在每一个请求处理线程(RequestHandlerThread)中也就需要传入ServiceRegistry了,这里把处理线程和处理逻辑分成了两个类:RequestHandlerThread只是一个线程,从ServiceRegistry获取到提供服务的对象后,就会把RpcRequest和服务对象直接交给RequestHandler去处理,反射等过程被放到了RequestHandler里。
RequestHandlerThread.java:处理线程,接受对象
@AllArgsConstructor
public class RequestHandlerThread implements Runnable{
private static final Logger logger = LoggerFactory.getLogger(RequestHandler.class);
private Socket socket;
private RequestHandler requestHandler;
private ServiceRegistry serviceRegistry;
@Override
public void run() {
try(ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream())){
RpcRequest rpcRequest = (RpcRequest) objectInputStream.readObject();
String interfaceName = rpcRequest.getInterfaceName();
Object service = serviceRegistry.getService(interfaceName);
Object result = requestHandler.handle(rpcRequest,service);
objectOutputStream.writeObject(RpcResponse.success(result));
objectOutputStream.flush();
} catch (IOException | ClassNotFoundException e) {
logger.error("调用或发送时有错误发生:",e);
}
}
}
RequestHandler.java:通过反射进行方法调用
public class RequestHandler {
private static final Logger logger = LoggerFactory.getLogger(RequestHandler.class);
public Object handle(RpcRequest rpcRequest,Object service){
Object result = null;
try{
result = invokeTargetMethod(rpcRequest,service);
logger.info("服务:{} 成功调用方法:{}",rpcRequest.getInterfaceName(),rpcRequest.getMethodName());
} catch (IllegalAccessException | InvocationTargetException e) {
logger.error("调用或发送时有错误发生:",e);
} return result;
}
private Object invokeTargetMethod(RpcRequest rpcRequest,Object service) throws InvocationTargetException, IllegalAccessException {
Method method;
try{
method = service.getClass().getMethod(rpcRequest.getMethodName(),rpcRequest.getParamTypes());
} catch (NoSuchMethodException e) {
return RpcResponse.fail(ResponseCode.ERROR);
}
return method.invoke(service,rpcRequest.getParameters());
}
}
在这种情况下,客户端不需要做任何改动
测试
服务端测试:
public class TestServer {
public static void main(String[] args) {
HelloService helloService = new HelloServiceImpl();
ServiceRegistry serviceRegistry = new DefaultServiceRegistry();
serviceRegistry.register(helloService);
RpcServer rpcServer = new RpcServer(serviceRegistry);
rpcServer.start(9000);
}
}
客户端不需要改动,执行后获得和上次相同的结果。
|