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

feat:provider支持配置SDKContext #48

Merged
merged 1 commit into from
Aug 15, 2023
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
@@ -1,7 +1,7 @@
global:
serverConnector:
addresses:
- 127.0.0.1:8091
- 119.91.66.223:8091
statReporter:
enable: true
plugin:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,35 @@ public class PolarisManagedChannelBuilder extends ManagedChannelBuilder<PolarisM

private final ServiceKey sourceService;

/**
* follow {@link ManagedChannelBuilder#forTarget(String)}
*
* @param target 服务名
* @return {@link PolarisManagedChannelBuilder}
*/
public static PolarisManagedChannelBuilder forTarget(String target) {
return forTarget(target, null, null);
}

/**
* 增强 {@link ManagedChannelBuilder#forTarget(String)}, 在连接到目标服务时允许设置主调服务的相关信息
*
* @param target 服务名
* @param sourceService {@link ServiceKey} 主调服务信息以及标签
* @return {@link PolarisManagedChannelBuilder}
*/
public static PolarisManagedChannelBuilder forTarget(String target, ServiceKey sourceService) {
return new PolarisManagedChannelBuilder(target, sourceService, null);
}

/**
* 增强 {@link ManagedChannelBuilder#forTarget(String)}, 在连接到目标服务时允许设置主调服务的相关信息, 并且可以自定义北极星 SDK 的核心数据结构 {@link SDKContext}
*
* @param target 服务名
* @param sourceService {@link ServiceKey} 主调服务信息以及标签
* @param sdkContext {@link SDKContext} 可以设置北极星 SDK 的相关配置以及行为, 例如服务治理中心地址等等
* @return {@link PolarisManagedChannelBuilder}
*/
public static PolarisManagedChannelBuilder forTarget(String target, ServiceKey sourceService, SDKContext sdkContext) {
return new PolarisManagedChannelBuilder(target, sourceService, sdkContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ public PickResult pickSubchannel(PickSubchannelArgs args) {
}

Instance doLoadBalance(ServiceInstances serviceInstances) {

if (serviceInstances.getInstances().size() == 1) {
return serviceInstances.getInstances().get(0);
}
Expand All @@ -149,12 +148,10 @@ Instance doLoadBalance(ServiceInstances serviceInstances) {
request.setDstInstances(serviceInstances);

ProcessLoadBalanceResponse response = routerAPI.processLoadBalance(request);

return response.getTargetInstance();
}

ServiceInstances doRoute(ServiceInstances serviceInstances, ServiceKey target, PickSubchannelArgs args) {

ProcessRoutersRequest request = new ProcessRoutersRequest();
request.setDstInstances(serviceInstances);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.tencent.polaris.grpc.server;

import com.tencent.polaris.client.api.SDKContext;
import io.grpc.Server;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
Expand All @@ -34,10 +35,13 @@ public final class GraceOffline {

private final Duration maxWaitDuration;

private final SDKContext context;

private final AtomicBoolean executed = new AtomicBoolean(false);

public GraceOffline(Server server, Duration maxWaitDuration) {
public GraceOffline(Server server, Duration maxWaitDuration, SDKContext context) {
this.grpcServer = server;
this.context = context;
this.maxWaitDuration = maxWaitDuration;
}

Expand All @@ -60,6 +64,7 @@ public Server shutdown() {
} catch (InterruptedException ignore) {
Thread.currentThread().interrupt();
}
context.close();
return grpcServer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,12 @@ public Server start() throws IOException {
public Server shutdown() {
if (shutdownOnce.compareAndSet(false, true)) {
executorService.shutdownNow();
// 将自己从注册中心反注册掉
this.deregister(targetServer.getServices());
providerAPI.destroy();
}

return new GraceOffline(targetServer, maxWaitDuration).shutdown();
return new GraceOffline(targetServer, maxWaitDuration, context).shutdown();
}

@Override
Expand All @@ -118,6 +119,7 @@ public Server shutdownNow() {
executorService.shutdownNow();
this.deregister(targetServer.getServices());
providerAPI.destroy();
context.close();
}
return this.targetServer.shutdownNow();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Executor;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -77,7 +78,7 @@ public final class PolarisGrpcServerBuilder extends ServerBuilder<PolarisGrpcSer

private final List<ServerInterceptor> interceptors = new ArrayList<>();

private final SDKContext context = SDKContext.initContext();
private SDKContext context;

/**
* Static factory for creating a new PolarisGrpcServerBuilder.
Expand All @@ -98,6 +99,17 @@ public static PolarisGrpcServerBuilder forPort(int port) {
public PolarisGrpcServerBuilder(ServerBuilder<?> builder) {
this.builder = builder;
}

/**
* Set polaris SDK Context
*
* @param context polaris sdk core object
* @return PolarisGrpcServerBuilder
*/
public PolarisGrpcServerBuilder sdkContext(SDKContext context) {
this.context = context;
return this;
}

/**
* Set grpc service name.
Expand Down Expand Up @@ -136,7 +148,7 @@ public PolarisGrpcServerBuilder metadata(Map<String, String> metadata) {
* set instance weight
*
* @param weight
* @return
* @return PolarisGrpcServerBuilder
*/
public PolarisGrpcServerBuilder weight(int weight) {
this.weight = weight;
Expand Down Expand Up @@ -290,6 +302,9 @@ public Server build() {
}

private void setDefault() {
if (Objects.isNull(context)) {
context = SDKContext.initContext();
}
if (StringUtils.isBlank(namespace)) {
this.namespace = DEFAULT_NAMESPACE;
}
Expand Down
Loading