Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cluster grpc request add server identity info. #12851

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,22 @@
import com.alibaba.nacos.api.remote.RequestCallBack;
import com.alibaba.nacos.api.remote.request.Request;
import com.alibaba.nacos.api.remote.response.Response;
import com.alibaba.nacos.auth.config.AuthConfigs;
import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.remote.ConnectionType;
import com.alibaba.nacos.common.remote.client.RpcClient;
import com.alibaba.nacos.common.remote.client.RpcClientFactory;
import com.alibaba.nacos.common.remote.client.RpcClientTlsConfig;
import com.alibaba.nacos.common.remote.client.ServerListFactory;
import com.alibaba.nacos.common.remote.client.RpcClientTlsConfigFactory;
import com.alibaba.nacos.common.remote.client.ServerListFactory;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.core.cluster.Member;
import com.alibaba.nacos.core.cluster.MemberChangeListener;
import com.alibaba.nacos.core.cluster.MembersChangeEvent;
import com.alibaba.nacos.core.cluster.ServerMemberManager;
import com.alibaba.nacos.core.utils.Loggers;
import com.alibaba.nacos.sys.env.EnvUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
Expand All @@ -60,8 +61,14 @@ public class ClusterRpcClientProxy extends MemberChangeListener {

private static final long DEFAULT_REQUEST_TIME_OUT = 3000L;

@Autowired
ServerMemberManager serverMemberManager;
final ServerMemberManager serverMemberManager;

final AuthConfigs authConfigs;

public ClusterRpcClientProxy(ServerMemberManager serverMemberManager, AuthConfigs authConfigs) {
this.serverMemberManager = serverMemberManager;
this.authConfigs = authConfigs;
}

/**
* init after constructor.
Expand Down Expand Up @@ -184,6 +191,7 @@ public Response sendRequest(Member member, Request request) throws NacosExceptio
public Response sendRequest(Member member, Request request, long timeoutMills) throws NacosException {
RpcClient client = RpcClientFactory.getClient(memberClientKey(member));
if (client != null) {
injectorServerIdentity(request);
return client.request(request, timeoutMills);
} else {
throw new NacosException(CLIENT_INVALID_PARAM, "No rpc client related to member: " + member);
Expand All @@ -201,6 +209,7 @@ public Response sendRequest(Member member, Request request, long timeoutMills) t
public void asyncRequest(Member member, Request request, RequestCallBack callBack) throws NacosException {
RpcClient client = RpcClientFactory.getClient(memberClientKey(member));
if (client != null) {
injectorServerIdentity(request);
client.asyncRequest(request, callBack);
} else {
throw new NacosException(CLIENT_INVALID_PARAM, "No rpc client related to member: " + member);
Expand Down Expand Up @@ -243,4 +252,10 @@ public boolean isRunning(Member member) {
}
return client.isRunning();
}

private void injectorServerIdentity(Request request) {
if (StringUtils.isNotBlank(authConfigs.getServerIdentityKey())) {
request.putHeader(authConfigs.getServerIdentityKey(), authConfigs.getServerIdentityValue());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.alibaba.nacos.api.remote.ability.ServerRemoteAbility;
import com.alibaba.nacos.api.remote.request.HealthCheckRequest;
import com.alibaba.nacos.api.remote.response.Response;
import com.alibaba.nacos.auth.config.AuthConfigs;
import com.alibaba.nacos.common.remote.ConnectionType;
import com.alibaba.nacos.common.remote.client.RpcClient;
import com.alibaba.nacos.common.remote.client.RpcClientFactory;
Expand Down Expand Up @@ -69,6 +70,9 @@ class ClusterRpcClientProxyTest {
@Mock
private ServerMemberManager serverMemberManager;

@Mock
private AuthConfigs authConfigs;

@Mock
private RpcClient client;

Expand Down Expand Up @@ -96,6 +100,8 @@ void setUp() throws NacosException {
clientMap.remove("Cluster-" + member.getAddress()).shutdown();
clientMap.put("Cluster-" + member.getAddress(), client);
when(client.getConnectionType()).thenReturn(ConnectionType.GRPC);
when(authConfigs.getServerIdentityKey()).thenReturn("MockIdentityKey");
when(authConfigs.getServerIdentityValue()).thenReturn("MockIdentityValue");
}

@Test
Expand Down
Loading