Skip to content

Commit

Permalink
[ISSUE alibaba#11456]Optimize code.
Browse files Browse the repository at this point in the history
  • Loading branch information
stone-98 committed Dec 25, 2023
1 parent 3851794 commit 51801ee
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
* @version $Id: RpcClientFactory.java, v 0.1 2020年07月14日 3:41 PM liuzunfei Exp $
*/
public class RpcClientFactory {

private static final Logger LOGGER = LoggerFactory.getLogger("com.alibaba.nacos.common.remote.client");

private static final Map<String, RpcClient> CLIENT_MAP = new ConcurrentHashMap<>();

/**
* get all client.
*
Expand All @@ -47,7 +47,7 @@ public class RpcClientFactory {
public static Set<Map.Entry<String, RpcClient>> getAllClientEntries() {
return CLIENT_MAP.entrySet();
}

/**
* shut down client.
*
Expand All @@ -59,11 +59,11 @@ public static void destroyClient(String clientName) throws NacosException {
rpcClient.shutdown();
}
}

public static RpcClient getClient(String clientName) {
return CLIENT_MAP.get(clientName);
}

/**
* create a rpc client.
*
Expand All @@ -74,18 +74,18 @@ public static RpcClient getClient(String clientName) {
public static RpcClient createClient(String clientName, ConnectionType connectionType, Map<String, String> labels) {
return createClient(clientName, connectionType, null, null, labels);
}

public static RpcClient createClient(String clientName, ConnectionType connectionType, Map<String, String> labels,
RpcClientTlsConfig tlsConfig) {
return createClient(clientName, connectionType, null, null, labels, tlsConfig);

}

public static RpcClient createClient(String clientName, ConnectionType connectionType, Integer threadPoolCoreSize,
Integer threadPoolMaxSize, Map<String, String> labels) {
return createClient(clientName, connectionType, threadPoolCoreSize, threadPoolMaxSize, labels, null);
}

/**
* create a rpc client.
*
Expand All @@ -98,48 +98,60 @@ public static RpcClient createClient(String clientName, ConnectionType connectio
*/
public static RpcClient createClient(String clientName, ConnectionType connectionType, Integer threadPoolCoreSize,
Integer threadPoolMaxSize, Map<String, String> labels, RpcClientTlsConfig tlsConfig) {

if (!ConnectionType.GRPC.equals(connectionType)) {
throw new UnsupportedOperationException("unsupported connection type :" + connectionType.getType());
}

return CLIENT_MAP.computeIfAbsent(clientName, clientNameInner -> {
LOGGER.info("[RpcClientFactory] create a new rpc client of " + clientName);
return new GrpcSdkClient(clientNameInner, threadPoolCoreSize, threadPoolMaxSize, labels, tlsConfig);
});
}

/**
* create a rpc client.
* Creates an RPC client for cluster communication with default thread pool settings.
*
* @param clientName client name.
* @param connectionType client type.
* @return rpc client.
* @param clientName The name of the client.
* @param connectionType The type of client connection.
* @param labels Additional labels for RPC-related attributes.
* @return An RPC client for cluster communication.
*/
public static RpcClient createClusterClient(String clientName, ConnectionType connectionType,
Map<String, String> labels) {
return createClusterClient(clientName, connectionType, null, null, labels);
}


/**
* Creates an RPC client for cluster communication with TLS configuration.
*
* @param clientName The name of the client.
* @param connectionType The type of client connection.
* @param labels Additional labels for RPC-related attributes.
* @param tlsConfig TLS configuration for secure communication.
* @return An RPC client for cluster communication with TLS configuration.
*/
public static RpcClient createClusterClient(String clientName, ConnectionType connectionType,
Map<String, String> labels, RpcClusterClientTlsConfig tlsConfig) {
return createClusterClient(clientName, connectionType, null, null, labels, tlsConfig);
}

/**
* create a rpc client.
* Creates an RPC client for cluster communication with custom thread pool settings.
*
* @param clientName client name.
* @param connectionType client type.
* @param threadPoolCoreSize grpc thread pool core size
* @param threadPoolMaxSize grpc thread pool max size
* @return rpc client.
* @param clientName The name of the client.
* @param connectionType The type of client connection.
* @param threadPoolCoreSize The core size of the gRPC thread pool.
* @param threadPoolMaxSize The maximum size of the gRPC thread pool.
* @param labels Additional labels for RPC-related attributes.
* @return An RPC client for cluster communication with custom thread pool settings.
*/
public static RpcClient createClusterClient(String clientName, ConnectionType connectionType,
Integer threadPoolCoreSize, Integer threadPoolMaxSize, Map<String, String> labels) {
return createClusterClient(clientName, connectionType, threadPoolCoreSize, threadPoolMaxSize, labels, null);
}



/**
* createClusterClient.
*
Expand All @@ -158,7 +170,7 @@ public static RpcClient createClusterClient(String clientName, ConnectionType co
if (!ConnectionType.GRPC.equals(connectionType)) {
throw new UnsupportedOperationException("unsupported connection type :" + connectionType.getType());
}

return CLIENT_MAP.computeIfAbsent(clientName,
clientNameInner -> new GrpcClusterClient(clientNameInner, threadPoolCoreSize, threadPoolMaxSize, labels,
tlsConfig));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,35 @@ public class RpcConstants {

public static final String NACOS_SDK_CLIENT_RPC = "nacos.remote.sdk.client.rpc";

@RpcConfigLabel
@RpcSdkConfigLabel
public static final String RPC_SDK_CLIENT_TLS_ENABLE = NACOS_SDK_CLIENT_RPC + ".tls.enable";

@RpcConfigLabel
@RpcSdkConfigLabel
public static final String RPC_SDK_CLIENT_TLS_PROVIDER = NACOS_SDK_CLIENT_RPC + ".tls.provider";

@RpcConfigLabel
@RpcSdkConfigLabel
public static final String RPC_SDK_CLIENT_MUTUAL_AUTH = NACOS_SDK_CLIENT_RPC + ".tls.mutualAuth";

@RpcConfigLabel
@RpcSdkConfigLabel
public static final String RPC_SDK_CLIENT_TLS_PROTOCOLS = NACOS_SDK_CLIENT_RPC + ".tls.protocols";

@RpcConfigLabel
@RpcSdkConfigLabel
public static final String RPC_SDK_CLIENT_TLS_CIPHERS = NACOS_SDK_CLIENT_RPC + ".tls.ciphers";

@RpcConfigLabel
@RpcSdkConfigLabel
public static final String RPC_SDK_CLIENT_TLS_CERT_CHAIN_PATH = NACOS_SDK_CLIENT_RPC + ".tls.certChainFile";

@RpcConfigLabel
@RpcSdkConfigLabel
public static final String RPC_SDK_CLIENT_TLS_CERT_KEY = NACOS_SDK_CLIENT_RPC + ".tls.certPrivateKey";

@RpcConfigLabel
@RpcSdkConfigLabel
public static final String RPC_SDK_CLIENT_TLS_TRUST_PWD = NACOS_SDK_CLIENT_RPC + ".tls.certPrivateKeyPassword";

@RpcConfigLabel
@RpcSdkConfigLabel
public static final String RPC_SDK_CLIENT_TLS_TRUST_COLLECTION_CHAIN_PATH =
NACOS_SDK_CLIENT_RPC + ".tls.trustCollectionChainPath";

@RpcConfigLabel
@RpcSdkConfigLabel
public static final String RPC_SDK_CLIENT_TLS_TRUST_ALL = NACOS_SDK_CLIENT_RPC + ".tls.trustAll";

private static final Set<String> CONFIG_NAMES = new HashSet<>();
Expand Down Expand Up @@ -104,19 +104,12 @@ public class RpcConstants {

private static final Set<String> CLUSTER_CONFIG_NAMES = new HashSet<>();

@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
protected @interface RpcConfigLabel {

}

static {
Field[] declaredFields = RpcConstants.class.getDeclaredFields();
for (Field declaredField : declaredFields) {
declaredField.setAccessible(true);
if (declaredField.getType().equals(String.class) && null != declaredField.getAnnotation(
RpcConfigLabel.class)) {
RpcSdkConfigLabel.class)) {
try {
CONFIG_NAMES.add((String) declaredField.get(null));
} catch (IllegalAccessException ignored) {
Expand All @@ -136,11 +129,18 @@ public class RpcConstants {
}
}

public static Set<String> getClusterRpcParams() {
public static Set<String> getSdkRpcParams() {
return Collections.unmodifiableSet(CONFIG_NAMES);
}

public static Set<String> getRpcParams() {
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
protected @interface RpcSdkConfigLabel {

}

public static Set<String> getClusterRpcParams() {
return Collections.unmodifiableSet(CONFIG_NAMES);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,21 @@
import static org.junit.Assert.assertEquals;

public class RpcConstantsTest {

@Test
public void testGetRpcParams() {
Class clazz = RpcConstants.class;
Field[] declaredFields = clazz.getDeclaredFields();
Field[] declaredFields = RpcConstants.class.getDeclaredFields();
int i = 0;
for (Field declaredField : declaredFields) {
declaredField.setAccessible(true);
if (declaredField.getType().equals(String.class) && null != declaredField.getAnnotation(
RpcConstants.RpcConfigLabel.class)) {
RpcConstants.RpcSdkConfigLabel.class)) {
i++;
}
}
assertEquals(i, RpcConstants.getRpcParams().size());
assertEquals(i, RpcConstants.getSdkRpcParams().size());
}

@Test
public void testGetClusterRpcParams() {
Field[] declaredFields = RpcConstants.class.getDeclaredFields();
Expand Down
34 changes: 17 additions & 17 deletions core/src/main/java/com/alibaba/nacos/core/remote/BaseRpcServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,29 @@
* @version $Id: BaseRpcServer.java, v 0.1 2020年07月13日 3:41 PM liuzunfei Exp $
*/
public abstract class BaseRpcServer {

static {
PayloadRegistry.init();
}

/**
* Start sever.
*/
@PostConstruct
public void start() throws Exception {
String serverName = getClass().getSimpleName();
Loggers.REMOTE.info("Nacos {} Rpc server starting at port {}", serverName, getServicePort());

startServer();

if (RpcServerSslContextRefresherHolder.getSdkInstance() != null) {
RpcServerSslContextRefresherHolder.getSdkInstance().refresh(this);
}

if (RpcServerSslContextRefresherHolder.getClusterInstance() != null) {
RpcServerSslContextRefresherHolder.getClusterInstance().refresh(this);
}

Loggers.REMOTE.info("Nacos {} Rpc server started at port {}", serverName, getServicePort());
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
Loggers.REMOTE.info("Nacos {} Rpc server stopping", serverName);
Expand All @@ -65,40 +65,40 @@ public void start() throws Exception {
Loggers.REMOTE.error("Nacos {} Rpc server stopped fail...", serverName, e);
}
}));

}

/**
* get connection type.
*
* @return connection type.
*/
public abstract ConnectionType getConnectionType();

/**
* Reload protocol context if necessary.
*
* <p>
* protocol like:
* <li>Tls</li>
* protocol like:
* <li>Tls</li>
* </p>
*/
public abstract void reloadProtocolContext();

/**
* Start sever.
*
* @throws Exception exception throw if start server fail.
*/
public abstract void startServer() throws Exception;

/**
* the increase offset of nacos server port for rpc server port.
*
* @return delta port offset of main port.
*/
public abstract int rpcPortOffset();

/**
* get service port.
*
Expand All @@ -107,7 +107,7 @@ public void start() throws Exception {
public int getServicePort() {
return EnvUtil.getPort() + rpcPortOffset();
}

/**
* Stop Server.
*
Expand All @@ -116,11 +116,11 @@ public int getServicePort() {
public final void stopServer() throws Exception {
shutdownServer();
}

/**
* the increase offset of nacos server port for rpc server port.
*/
@PreDestroy
public abstract void shutdownServer();

}
Loading

0 comments on commit 51801ee

Please sign in to comment.