fix : Simplify the rpc module, remove unnecessary classes and methods (#1187) (#812)

pull/1212/head
pizihao 2 years ago
parent 62de8fff0f
commit fcec338e29

@ -22,8 +22,6 @@ import cn.hippo4j.common.web.exception.IllegalException;
import cn.hippo4j.rpc.exception.TimeOutException; import cn.hippo4j.rpc.exception.TimeOutException;
import cn.hippo4j.rpc.model.Request; import cn.hippo4j.rpc.model.Request;
import cn.hippo4j.rpc.model.Response; import cn.hippo4j.rpc.model.Response;
import cn.hippo4j.rpc.process.ActivePostProcess;
import cn.hippo4j.rpc.process.ActiveProcessChain;
import cn.hippo4j.rpc.support.NettyConnectPool; import cn.hippo4j.rpc.support.NettyConnectPool;
import cn.hippo4j.rpc.support.NettyConnectPoolHolder; import cn.hippo4j.rpc.support.NettyConnectPoolHolder;
import cn.hippo4j.rpc.support.ResultHolder; import cn.hippo4j.rpc.support.ResultHolder;
@ -35,12 +33,13 @@ import io.netty.channel.pool.ChannelPoolHandler;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.LinkedList; import java.util.Optional;
import java.util.List;
import java.util.concurrent.locks.LockSupport; import java.util.concurrent.locks.LockSupport;
/** /**
* Client implemented using netty * Client implemented using netty
*
* @since 1.5.1
*/ */
@Slf4j @Slf4j
public class NettyClientConnection implements ClientConnection { public class NettyClientConnection implements ClientConnection {
@ -51,33 +50,28 @@ public class NettyClientConnection implements ClientConnection {
*/ */
long timeout = 30000L; long timeout = 30000L;
EventLoopGroup worker = new NioEventLoopGroup(); EventLoopGroup worker = new NioEventLoopGroup();
ActiveProcessChain activeProcessChain;
NettyConnectPool connectionPool; NettyConnectPool connectionPool;
ChannelFuture future; ChannelFuture future;
Channel channel; Channel channel;
public NettyClientConnection(InetSocketAddress address, public NettyClientConnection(InetSocketAddress address,
List<ActivePostProcess> activeProcesses,
ChannelPoolHandler handler) { ChannelPoolHandler handler) {
Assert.notNull(worker); Assert.notNull(worker);
this.address = address; this.address = address;
this.activeProcessChain = new ActiveProcessChain(activeProcesses);
this.connectionPool = NettyConnectPoolHolder.getPool(address, timeout, worker, handler); this.connectionPool = NettyConnectPoolHolder.getPool(address, timeout, worker, handler);
} }
public NettyClientConnection(InetSocketAddress address, ChannelPoolHandler handler) {
this(address, new LinkedList<>(), handler);
}
@Override @Override
public Response connect(Request request) { public Response connect(Request request) {
activeProcessChain.applyPreHandle(request);
this.channel = connectionPool.acquire(timeout); this.channel = connectionPool.acquire(timeout);
Response response = null; boolean debugEnabled = log.isDebugEnabled();
Response response;
try { try {
String key = request.getKey(); String key = request.getKey();
this.future = channel.writeAndFlush(request); this.future = channel.writeAndFlush(request);
log.info("Call successful, target address is {}:{}, request key is {}", address.getHostName(), address.getPort(), key); if (debugEnabled) {
log.debug("Call successful, target address is {}:{}, request key is {}", address.getHostName(), address.getPort(), key);
}
// Wait for execution to complete // Wait for execution to complete
ResultHolder.putThread(key, Thread.currentThread()); ResultHolder.putThread(key, Thread.currentThread());
LockSupport.parkNanos(timeout() * 1000000); LockSupport.parkNanos(timeout() * 1000000);
@ -85,14 +79,13 @@ public class NettyClientConnection implements ClientConnection {
if (response == null) { if (response == null) {
throw new TimeOutException("Timeout waiting for server-side response"); throw new TimeOutException("Timeout waiting for server-side response");
} }
activeProcessChain.applyPostHandle(request, response); if (debugEnabled) {
log.info("The response from {}:{} was received successfully with the response key {}.", address.getHostName(), address.getPort(), key); log.debug("The response from {}:{} was received successfully with the response key {}.", address.getHostName(), address.getPort(), key);
}
return response; return response;
} catch (Exception ex) { } catch (Exception ex) {
activeProcessChain.afterCompletion(request, response, ex);
throw new IllegalException(ex); throw new IllegalException(ex);
} finally { } finally {
activeProcessChain.afterCompletion(request, response, null);
connectionPool.release(this.channel); connectionPool.release(this.channel);
} }
} }
@ -108,13 +101,13 @@ public class NettyClientConnection implements ClientConnection {
} }
@Override @Override
public synchronized void close() { public void close() {
if (this.channel == null) { Optional.ofNullable(this.channel)
return; .ifPresent(c -> {
} worker.shutdownGracefully();
worker.shutdownGracefully(); this.future.channel().close();
this.future.channel().close(); this.channel.close();
this.channel.close(); });
} }
@Override @Override

@ -1,50 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.hippo4j.rpc.coder;
import cn.hippo4j.rpc.exception.CoderException;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.serialization.ClassResolver;
import io.netty.handler.codec.serialization.ObjectDecoder;
/**
* According to the decoder for java objects implemented by ObjectDecoder,
* it is necessary to ensure that the transmitted objects can be serialized
*/
public class NettyDecoder extends ObjectDecoder {
public NettyDecoder(ClassResolver classResolver) {
super(classResolver);
}
@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) {
ByteBuf byteBuf = in.retainedDuplicate();
try {
Object o = super.decode(ctx, in);
if (o == null) {
return byteBuf;
} else {
return o;
}
} catch (Exception e) {
throw new CoderException("The encoding is abnormal, which may be caused by the failure of the transfer object to be deserialized");
}
}
}

@ -24,8 +24,12 @@ import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInboundHandlerAdapter;
import java.util.Optional;
/** /**
* the abstract base of {@link ConnectHandler} and {@link ChannelInboundHandlerAdapter} * the abstract base of {@link ConnectHandler} and {@link ChannelInboundHandlerAdapter}
*
* @since 1.5.1
*/ */
public abstract class AbstractNettyTakeHandler extends ChannelInboundHandlerAdapter implements ConnectHandler { public abstract class AbstractNettyTakeHandler extends ChannelInboundHandlerAdapter implements ConnectHandler {
@ -42,9 +46,10 @@ public abstract class AbstractNettyTakeHandler extends ChannelInboundHandlerAdap
if (channel.isActive()) { if (channel.isActive()) {
ctx.close(); ctx.close();
} }
if (cause != null) { Optional.ofNullable(cause)
throw new ConnectionException(cause); .ifPresent(t -> {
} throw new ConnectionException(cause);
});
} }
/** /**

@ -17,12 +17,16 @@
package cn.hippo4j.rpc.handler; package cn.hippo4j.rpc.handler;
import cn.hippo4j.common.web.exception.IllegalException;
import io.netty.channel.ChannelHandler;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
/** /**
* Manage the Handler used in the processing.<br> * Manage the Handler used in the processing.<br>
* The Handler must be able to exist multiple times and be invoked once in a single execution * The Handler must be able to exist multiple times and be invoked once in a single execution
*
* @since 1.5.1
*/ */
public interface HandlerManager<T> { public interface HandlerManager<T> {
@ -42,20 +46,6 @@ public interface HandlerManager<T> {
*/ */
HandlerManager<T> addFirst(String name, T handler); HandlerManager<T> addFirst(String name, T handler);
/**
* Add handler to the end of the Handler chain, without specifying a name
*
* @param handler handler
*/
HandlerManager<T> addLast(T handler);
/**
* Adds handler to the head of the Handler chain, without specifying a name
*
* @param handler handler
*/
HandlerManager<T> addFirst(T handler);
/** /**
* Whether handler exists * Whether handler exists
* *
@ -72,6 +62,14 @@ public interface HandlerManager<T> {
* @return HandlerEntity * @return HandlerEntity
*/ */
default HandlerEntity<T> getHandlerEntity(long order, T handler, String name) { default HandlerEntity<T> getHandlerEntity(long order, T handler, String name) {
Class<?> cls = handler.getClass();
boolean b = cls.isAnnotationPresent(ChannelHandler.Sharable.class)
|| HandlerManager.class.isAssignableFrom(cls);
if (!b) {
throw new IllegalException("Join the execution of the handler must add io.netty.channel.ChannelHandler." +
"Sharable annotations, Please for the handler class " + cls.getName() + " add io.netty.channel." +
"ChannelHandler.Sharable annotation");
}
return new HandlerEntity<>(order, handler, name); return new HandlerEntity<>(order, handler, name);
} }

@ -19,36 +19,28 @@ package cn.hippo4j.rpc.handler;
import cn.hippo4j.common.toolkit.Assert; import cn.hippo4j.common.toolkit.Assert;
import cn.hippo4j.common.toolkit.ReflectUtil; import cn.hippo4j.common.toolkit.ReflectUtil;
import cn.hippo4j.rpc.process.ActivePostProcess;
import cn.hippo4j.rpc.process.ActiveProcessChain;
import cn.hippo4j.rpc.model.Request;
import cn.hippo4j.rpc.model.DefaultResponse;
import cn.hippo4j.rpc.model.Response;
import cn.hippo4j.rpc.discovery.ClassRegistry; import cn.hippo4j.rpc.discovery.ClassRegistry;
import cn.hippo4j.rpc.discovery.Instance; import cn.hippo4j.rpc.discovery.Instance;
import cn.hippo4j.rpc.model.DefaultResponse;
import cn.hippo4j.rpc.model.Request;
import cn.hippo4j.rpc.model.Response;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;
/** /**
* netty adaptation layer * netty adaptation layer
*
* @since 1.5.1
*/ */
@ChannelHandler.Sharable @ChannelHandler.Sharable
public class NettyServerTakeHandler extends AbstractNettyTakeHandler implements ConnectHandler { public class NettyServerTakeHandler extends AbstractNettyTakeHandler implements ConnectHandler {
ActiveProcessChain activeProcessChain;
Instance instance; Instance instance;
public NettyServerTakeHandler(List<ActivePostProcess> processes, Instance instance) {
this.activeProcessChain = new ActiveProcessChain(processes);
this.instance = instance;
}
public NettyServerTakeHandler(Instance instance) { public NettyServerTakeHandler(Instance instance) {
this(new LinkedList<>(), instance); this.instance = instance;
} }
@Override @Override
@ -63,24 +55,17 @@ public class NettyServerTakeHandler extends AbstractNettyTakeHandler implements
@Override @Override
public Response sendHandler(Request request) { public Response sendHandler(Request request) {
if (!activeProcessChain.applyPreHandle(request)) { Response response;
return null;
}
Response response = null;
try { try {
Class<?> cls = ClassRegistry.get(request.getClassName()); Class<?> cls = ClassRegistry.get(request.getClassName());
Method method = ReflectUtil.getMethodByName(cls, request.getMethodName(), request.getParameterTypes()); Method method = ReflectUtil.getMethodByName(cls, request.getMethodName(), request.getParameterTypes());
Assert.notNull(method); Assert.notNull(method);
Object invoke = ReflectUtil.invoke(instance.getInstance(cls), method, request.getParameters()); Object invoke = ReflectUtil.invoke(instance.getInstance(cls), method, request.getParameters());
response = new DefaultResponse(request.getKey(), invoke.getClass(), invoke); response = new DefaultResponse(request.getKey(), invoke.getClass(), invoke);
activeProcessChain.applyPostHandle(request, response);
return response; return response;
} catch (Exception e) { } catch (Exception e) {
response = new DefaultResponse(request.getKey(), e, e.getMessage()); response = new DefaultResponse(request.getKey(), e, e.getMessage());
activeProcessChain.afterCompletion(request, response, e);
return response; return response;
} finally {
activeProcessChain.afterCompletion(request, response, null);
} }
} }

@ -1,60 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.hippo4j.rpc.process;
import cn.hippo4j.rpc.model.Request;
import cn.hippo4j.rpc.model.Response;
/**
* Callback while the connection is in progress
*/
public interface ActivePostProcess {
/**
* Client: After establishing a connection and before passing parameters<br>
* Server: Receives parameters and performs pre-call operations<br>
*
* @param request request
* @return Whether to continue the execution. If it is a client, the returned value does not affect subsequent execution
*/
default boolean preHandler(Request request) {
return true;
}
/**
* Client: Action after receiving a response<br>
* Server: performs the operation after the call<br>
*
* @param request request
* @param response response
*/
default void postHandler(Request request, Response response) {
// NO SOMETHING
}
/**
* Called when an exception or resource is cleaned
*
* @param request request
* @param response response
* @param e Exception
*/
default void afterCompletion(Request request, Response response, Exception e) {
// NO SOMETHING
}
}

@ -1,102 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.hippo4j.rpc.process;
import cn.hippo4j.rpc.model.Request;
import cn.hippo4j.rpc.model.Response;
import lombok.extern.slf4j.Slf4j;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* Processor chain for easier processing of processors in different scenarios<br>
* reference resources: spring HandlerExecutionChain
*
* @see ActivePostProcess
*/
@Slf4j
public final class ActiveProcessChain {
/**
* A collection of processors that will be applied to their assigned programs.
* Processors will perform different actions on different occasions for both the server and the client,
* but the execution period of that action must be the same
*/
List<ActivePostProcess> processes;
/**
* index <br>
* that identifies where the {@link ActivePostProcess#preHandler(Request)} processing is performed<br>
* This allows for the fact that some processors will add shutable operations to the class<br>
* eg: {@link java.io.Closeable}, The {@link ActivePostProcess#afterCompletion(Request, Response, Exception)}
* operation is not performed after an exception if the preprocessor is not executed
*/
int index = -1;
public ActiveProcessChain(List<ActivePostProcess> processes) {
this.processes = processes;
}
public ActiveProcessChain(ActivePostProcess... processes) {
this((processes != null ? Arrays.asList(processes) : Collections.emptyList()));
}
/**
* Apply postHandle methods of registered processes.
*/
public boolean applyPreHandle(Request request) {
for (int i = 0; i < this.processes.size(); i++) {
ActivePostProcess handle = processes.get(i);
if (!handle.preHandler(request)) {
afterCompletion(request, null, null);
return false;
}
this.index = i;
}
return true;
}
/**
* Apply postHandle methods of registered processes.
*/
public void applyPostHandle(Request request, Response response) {
for (int i = processes.size() - 1; i >= 0; i--) {
ActivePostProcess handle = processes.get(i);
handle.postHandler(request, response);
}
}
/**
* Trigger afterCompletion callbacks on the mapped ActivePostProcess.
* Will just invoke afterCompletion for all interceptors whose preHandle invocation
* has successfully completed and returned true.
*/
public void afterCompletion(Request request, Response response, Exception ex) {
for (int i = this.index; i >= 0; i--) {
ActivePostProcess handle = processes.get(i);
try {
handle.afterCompletion(request, response, ex);
} catch (Throwable e) {
log.error("HandlerInterceptor.afterCompletion threw exception", e);
}
}
}
}

@ -18,7 +18,6 @@
package cn.hippo4j.rpc.server; package cn.hippo4j.rpc.server;
import cn.hippo4j.common.toolkit.Assert; import cn.hippo4j.common.toolkit.Assert;
import cn.hippo4j.rpc.coder.NettyDecoder;
import cn.hippo4j.rpc.coder.NettyEncoder; import cn.hippo4j.rpc.coder.NettyEncoder;
import cn.hippo4j.rpc.discovery.ServerPort; import cn.hippo4j.rpc.discovery.ServerPort;
import cn.hippo4j.rpc.exception.ConnectionException; import cn.hippo4j.rpc.exception.ConnectionException;
@ -29,6 +28,7 @@ import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers; import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.util.Arrays; import java.util.Arrays;
@ -37,6 +37,8 @@ import java.util.List;
/** /**
* adapter to the netty server * adapter to the netty server
*
* @since 1.5.1
*/ */
@Slf4j @Slf4j
public class NettyServerConnection extends AbstractNettyHandlerManager implements ServerConnection { public class NettyServerConnection extends AbstractNettyHandlerManager implements ServerConnection {
@ -70,6 +72,10 @@ public class NettyServerConnection extends AbstractNettyHandlerManager implement
@Override @Override
public void bind(ServerPort port) { public void bind(ServerPort port) {
int serverPort = port.getPort();
if (serverPort < 0 || serverPort > 65535) {
throw new ConnectionException("The port number " + serverPort + " is outside 0~65535, which is not a legal port number");
}
ServerBootstrap server = new ServerBootstrap(); ServerBootstrap server = new ServerBootstrap();
server.group(leader, worker) server.group(leader, worker)
.channel(socketChannelCls) .channel(socketChannelCls)
@ -77,10 +83,10 @@ public class NettyServerConnection extends AbstractNettyHandlerManager implement
.childHandler(new ChannelInitializer<SocketChannel>() { .childHandler(new ChannelInitializer<SocketChannel>() {
@Override @Override
protected void initChannel(SocketChannel ch) throws Exception { protected void initChannel(SocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline(); ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new NettyEncoder()); pipeline.addLast(new NettyEncoder());
pipeline.addLast(new NettyDecoder(ClassResolvers.cacheDisabled(null))); pipeline.addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)));
handlerEntities.stream() handlerEntities.stream()
.sorted() .sorted()
.forEach(h -> { .forEach(h -> {
@ -93,9 +99,11 @@ public class NettyServerConnection extends AbstractNettyHandlerManager implement
} }
}); });
try { try {
this.future = server.bind(port.getPort()).sync(); this.future = server.bind(serverPort).sync();
this.channel = this.future.channel(); this.channel = this.future.channel();
log.info("The server is started and can receive requests. The listening port is {}", port.getPort()); if (log.isDebugEnabled()) {
log.debug("The server is started and can receive requests. The listening port is {}", serverPort);
}
this.port = port; this.port = port;
this.future.channel().closeFuture().sync(); this.future.channel().closeFuture().sync();
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
@ -113,7 +121,9 @@ public class NettyServerConnection extends AbstractNettyHandlerManager implement
this.worker.shutdownGracefully(); this.worker.shutdownGracefully();
this.channel.close(); this.channel.close();
this.future.channel().close(); this.future.channel().close();
log.info("The server is shut down and no more requests are received. The release port is {}", port.getPort()); if (log.isDebugEnabled()) {
log.debug("The server is shut down and no more requests are received. The release port is {}", port.getPort());
}
} }
@Override @Override
@ -136,16 +146,4 @@ public class NettyServerConnection extends AbstractNettyHandlerManager implement
return this; return this;
} }
@Override
public NettyServerConnection addLast(ChannelHandler handler) {
super.addLast(handler);
return this;
}
@Override
public NettyServerConnection addFirst(ChannelHandler handler) {
super.addFirst(handler);
return this;
}
} }

@ -17,6 +17,7 @@
package cn.hippo4j.rpc.support; package cn.hippo4j.rpc.support;
import cn.hippo4j.common.toolkit.Assert;
import cn.hippo4j.rpc.client.Client; import cn.hippo4j.rpc.client.Client;
import cn.hippo4j.rpc.discovery.DiscoveryAdapter; import cn.hippo4j.rpc.discovery.DiscoveryAdapter;
import cn.hippo4j.rpc.exception.ConnectionException; import cn.hippo4j.rpc.exception.ConnectionException;
@ -30,11 +31,30 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.Optional;
/** /**
* A FactoryBean that builds interfaces to invoke proxy objects * A FactoryBean that builds interfaces to invoke proxy objects
* is responsible for managing the entire life cycle of the proxy objects<br> * is responsible for managing the entire life cycle of the proxy objects<br>
* <h3>APPLICATION START</h3>
* When the application is started, the request initiator needs to complete the proxy of the calling interface,
* which ensures that the method can be requested to the server side when the method is called, rather than simply
* request an interface that cannot be instantiated. The classes involved in adding proxy to the interface are:
* <ul>
* <li>{@link NettyClientSupport}</li>
* <li>{@link NettyProxyCenter}</li>
* <li>{@link NettyClientPoolHandler}</li>
* </ul>
* <h3>AND SPRING</h3>
* In order to fully integrate {@link ClientFactoryBean} into the life cycle of spring beans,
* {@link ClientFactoryBean} also needs to implement the following interfaces:
* <ul>
* <li>{@link InitializingBean}</li>
* <li>{@link ApplicationContextAware}</li>
* <li>{@link DisposableBean}</li>
* </ul>
* *
* @since 1.5.1
* @deprecated With {@link cn.hippo4j.config.service.ThreadPoolAdapterService} structure, FactoryBean is not the best choice * @deprecated With {@link cn.hippo4j.config.service.ThreadPoolAdapterService} structure, FactoryBean is not the best choice
*/ */
@Deprecated @Deprecated
@ -52,10 +72,16 @@ public class ClientFactoryBean implements FactoryBean<Object>, InitializingBean,
*/ */
private String discoveryAdapterName; private String discoveryAdapterName;
/**
* The adaptation interface for obtaining ip information in the registry is used together with
* {@link #discoveryAdapterName}, so that the adapter implementation can be obtained in the container
* during the initialization phase
*/
private DiscoveryAdapter discoveryAdapter; private DiscoveryAdapter discoveryAdapter;
/** /**
* the channel handler * the channel handler, To ensure the security and reliability of netty calls,
* {@link ChannelHandler} must be identified by {@link ChannelHandler.Sharable}
*/ */
private ChannelHandler[] handlers; private ChannelHandler[] handlers;
@ -70,11 +96,13 @@ public class ClientFactoryBean implements FactoryBean<Object>, InitializingBean,
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
/** /**
* InetSocketAddress * InetSocketAddress, It is usually converted from {@link #applicationName} and {@link #discoveryAdapter}
*/ */
InetSocketAddress address; InetSocketAddress address;
public ClientFactoryBean(String applicationName, String discoveryAdapterName, Class<?> cls) { public ClientFactoryBean(String applicationName, String discoveryAdapterName, Class<?> cls) {
Assert.notNull(applicationName);
Assert.notNull(cls);
this.applicationName = applicationName; this.applicationName = applicationName;
this.discoveryAdapterName = discoveryAdapterName; this.discoveryAdapterName = discoveryAdapterName;
this.cls = cls; this.cls = cls;
@ -82,16 +110,17 @@ public class ClientFactoryBean implements FactoryBean<Object>, InitializingBean,
@Override @Override
public Object getObject() throws Exception { public Object getObject() throws Exception {
this.address = discoveryAdapter.getSocketAddress(applicationName); this.address = Optional.ofNullable(applicationName)
if (this.address == null) { .map(a -> discoveryAdapter.getSocketAddress(a))
String[] addressStr = applicationName.split(":"); .map(a -> {
if (addressStr.length < 2) { String[] addressStr = applicationName.split(":");
throw new ConnectionException("Failed to connect to the server because the IP address is invalid. Procedure"); if (addressStr.length < 2) {
} throw new ConnectionException("Failed to connect to the server because the IP address is invalid. Procedure");
this.address = InetSocketAddress.createUnresolved(addressStr[0], Integer.parseInt(addressStr[1])); }
} return InetSocketAddress.createUnresolved(addressStr[0], Integer.parseInt(addressStr[1]));
NettyClientPoolHandler handler = new NettyClientPoolHandler(handlers); })
Client client = NettyClientSupport.getClient(this.address, handler); .orElseThrow(() -> new ConnectionException("Failed to connect to the server because the IP address is invalid. Procedure"));
Client client = NettyClientSupport.getClient(this.address, new NettyClientPoolHandler(handlers));
return NettyProxyCenter.createProxy(client, cls, this.address); return NettyProxyCenter.createProxy(client, cls, this.address);
} }
@ -102,15 +131,15 @@ public class ClientFactoryBean implements FactoryBean<Object>, InitializingBean,
@Override @Override
public void afterPropertiesSet() throws Exception { public void afterPropertiesSet() throws Exception {
this.discoveryAdapter = (DiscoveryAdapter) applicationContext.getBean(discoveryAdapterName); this.discoveryAdapter = Optional.ofNullable(discoveryAdapterName)
.map(s -> (DiscoveryAdapter) applicationContext.getBean(discoveryAdapterName))
.orElse(null);
} }
@Override @Override
public void destroy() throws Exception { public void destroy() throws Exception {
if (this.address == null) { Optional.ofNullable(this.address)
return; .ifPresent(a -> NettyClientSupport.closeClient(this.address));
}
NettyClientSupport.closeClient(this.address);
} }
@Override @Override

@ -33,6 +33,7 @@ import java.io.IOException;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
/** /**
@ -51,6 +52,7 @@ import java.util.concurrent.ConcurrentHashMap;
* @see cn.hippo4j.rpc.client.NettyClientConnection * @see cn.hippo4j.rpc.client.NettyClientConnection
* @see NettyServerSupport * @see NettyServerSupport
* @see ClientFactoryBean * @see ClientFactoryBean
* @since 1.5.1
*/ */
@NoArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class NettyClientSupport { public final class NettyClientSupport {
@ -73,7 +75,7 @@ public final class NettyClientSupport {
? (NettyClientPoolHandler) handlerManager ? (NettyClientPoolHandler) handlerManager
: new NettyClientPoolHandler(); : new NettyClientPoolHandler();
if (handler.isEmpty()) { if (handler.isEmpty()) {
handler.addFirst(new NettyClientTakeHandler()); handler.addFirst(null, new NettyClientTakeHandler());
} }
NettyClientConnection connection = new NettyClientConnection(address, handler); NettyClientConnection connection = new NettyClientConnection(address, handler);
return new RPCClient(connection); return new RPCClient(connection);
@ -97,12 +99,13 @@ public final class NettyClientSupport {
*/ */
public static void closeClient(InetSocketAddress address) { public static void closeClient(InetSocketAddress address) {
Client client = clientMap.remove(address); Client client = clientMap.remove(address);
try { Optional.ofNullable(client)
if (client != null) { .ifPresent(c -> {
client.close(); try {
} c.close();
} catch (IOException e) { } catch (IOException e) {
throw new IllegalException(e); throw new IllegalException(e);
} }
});
} }
} }

@ -30,10 +30,13 @@ import io.netty.util.concurrent.Future;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.Optional;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
* This parameter applies only to the connection pool of netty * This parameter applies only to the connection pool of netty
*
* @since 1.5.1
*/ */
@Slf4j @Slf4j
public class NettyConnectPool { public class NettyConnectPool {
@ -58,7 +61,9 @@ public class NettyConnectPool {
this.handler = handler; this.handler = handler;
this.pool = new FixedChannelPool(bootstrap, handler, healthCheck, acquireTimeoutAction, this.pool = new FixedChannelPool(bootstrap, handler, healthCheck, acquireTimeoutAction,
timeout, maxConnect, maxPendingAcquires, true, true); timeout, maxConnect, maxPendingAcquires, true, true);
log.info("The connection pool is established with the connection target {}:{}", address.getHostName(), address.getPort()); if (log.isDebugEnabled()) {
log.info("The connection pool is established with the connection target {}:{}", address.getHostName(), address.getPort());
}
NettyConnectPoolHolder.createPool(address, this); NettyConnectPoolHolder.createPool(address, this);
} }
@ -82,14 +87,15 @@ public class NettyConnectPool {
} }
public void release(Channel channel) { public void release(Channel channel) {
try { Optional.ofNullable(channel)
if (channel != null) { .ifPresent(c -> {
pool.release(channel); try {
} pool.release(channel);
} catch (Exception e) { } catch (Exception e) {
NettyClientSupport.closeClient(address); NettyClientSupport.closeClient(address);
throw new ConnectionException("Failed to release the connection", e); throw new ConnectionException("Failed to release the connection", e);
} }
});
} }
public void close() { public void close() {

@ -40,6 +40,7 @@ import java.util.List;
* @see RPCServer * @see RPCServer
* @see NettyServerConnection * @see NettyServerConnection
* @see NettyClientSupport * @see NettyClientSupport
* @since 1.5.1
*/ */
public class NettyServerSupport implements Server { public class NettyServerSupport implements Server {
@ -95,7 +96,7 @@ public class NettyServerSupport implements Server {
: new NettyServerConnection(); : new NettyServerConnection();
// Assign a default handler if no handler exists // Assign a default handler if no handler exists
if (connection.isEmpty()) { if (connection.isEmpty()) {
connection.addFirst(new NettyServerTakeHandler(new DefaultInstance())); connection.addFirst(null, new NettyServerTakeHandler(new DefaultInstance()));
} }
server = new RPCServer(connection, serverPort); server = new RPCServer(connection, serverPort);
} }

Loading…
Cancel
Save