Skip to content

Commit

Permalink
feat:provider支持配置SDKContext
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun committed Aug 15, 2023
1 parent 3f6a646 commit 285343f
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 10 deletions.
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 @@ -149,7 +149,6 @@ private void doWatch(Listener2 listener) {

private void notifyListener(Listener2 listener, InstancesResponse response) {
ServiceInstances serviceInstances = response.toServiceInstances();
if (!serviceInstances.getInstances().isEmpty()) {
List<EquivalentAddressGroup> equivalentAddressGroups = serviceInstances.getInstances()
.stream()
.map(this::buildEquivalentAddressGroup)
Expand All @@ -165,7 +164,6 @@ private void notifyListener(Listener2 listener, InstancesResponse response) {
.setAddresses(equivalentAddressGroups)
.setAttributes(builder.build())
.build());
}
}

@Override
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 @@ -75,7 +76,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 @@ -96,6 +97,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 @@ -134,7 +146,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 @@ -277,6 +289,9 @@ public Server build() {
}

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

0 comments on commit 285343f

Please sign in to comment.