-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4baf31f
commit 6bfc0d2
Showing
23 changed files
with
5,361 additions
and
4 deletions.
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
...src/main/java/com/tencent/tsf/femas/adaptor/paas/config/FemasConfigGrpcClientManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package com.tencent.tsf.femas.adaptor.paas.config; | ||
|
||
import com.tencent.tsf.femas.common.context.Context; | ||
import com.tencent.tsf.femas.common.context.factory.ContextFactory; | ||
import com.tencent.tsf.femas.common.entity.Service; | ||
import com.tencent.tsf.femas.common.util.StringUtils; | ||
import com.tencent.tsf.femas.config.AbstractConfigHttpClientManager; | ||
import com.tencent.tsf.femas.config.grpc.paas.*; | ||
import io.grpc.ManagedChannel; | ||
import io.grpc.netty.NettyChannelBuilder; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class FemasConfigGrpcClientManager extends AbstractConfigHttpClientManager { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(FemasConfigGrpcClientManager.class); | ||
private volatile Context context = ContextFactory.getContextInstance(); | ||
|
||
private ManagedChannel channel; | ||
|
||
public FemasConfigGrpcClientManager() { | ||
String host = GrpcHepler.getPaasServerHost(); | ||
int port = GrpcHepler.getPaasGrpcPort(); | ||
channel = NettyChannelBuilder | ||
.forAddress(host,port) | ||
.usePlaintext() | ||
.build(); | ||
} | ||
@Override | ||
public String getType() { | ||
return PollingType.grpc.name(); | ||
} | ||
@Override | ||
public void reportEvent(Service service, String eventId, String data) { | ||
if (context.isEmptyPaasServer()) { | ||
log.debug("reportEvent failed, could not find the paas address profile"); | ||
return; | ||
} | ||
ReportEventRequest eventRequest = ReportEventRequest.newBuilder() | ||
.setNamespaceId(service.getNamespace()) | ||
.setEventId(eventId) | ||
.setServiceName(service.getName()) | ||
.setData(data) | ||
.build(); | ||
try { | ||
PollingResult result = createStub().reportServiceEvent(eventRequest); | ||
boolean success = GrpcHepler.success(result); | ||
if(!success){ | ||
log.error("init namespace failed,message={}",result.getMessage()); | ||
} | ||
}catch (Exception e) { | ||
log.error("init namespace failed", e); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void reportApis(String namespaceId, String serviceName, String applicationVersion, String data) { | ||
if (context.isEmptyPaasServer()) { | ||
log.debug("reportApis failed ,could not find the paas address profile"); | ||
return; | ||
} | ||
ServiceApiRequest serviceApiRequest =ServiceApiRequest.newBuilder() | ||
.setNamespaceId(namespaceId) | ||
.setServiceName(serviceName) | ||
.setApplicationVersion(applicationVersion) | ||
.setData(data).build(); | ||
try { | ||
PollingResult pollingResult = createStub().reportServiceApi(serviceApiRequest); | ||
boolean success = GrpcHepler.success(pollingResult); | ||
if(!success){ | ||
log.error("reportApis failed,message={}",pollingResult.getMessage()); | ||
} | ||
} catch (Exception e) { | ||
// 无配置 paas server 时,报错不打印 | ||
log.warn("config http manager reportApis failed, msg:{}", e.getMessage()); | ||
} | ||
} | ||
|
||
@Override | ||
public String fetchKVValue(String key, String namespaceId) { | ||
if (context.isEmptyPaasServer()) { | ||
log.debug("fetchKVValue failed , could not find the paas address profile"); | ||
return null; | ||
} | ||
try { | ||
SimpleParam simpleParam = SimpleParam.newBuilder().setParam(key).build(); | ||
PollingResult pollingResult = createStub().fetchBreakerRule(simpleParam); | ||
boolean success = GrpcHepler.success(pollingResult); | ||
if(!success){ | ||
log.error("config http manager fetchKVValue failed,message={}",pollingResult.getMessage()); | ||
}else { | ||
return pollingResult.getResult(); | ||
} | ||
} catch (Exception e) { | ||
log.error("config http manager fetchKVValue failed", e); | ||
} | ||
return null; | ||
} | ||
|
||
|
||
|
||
@Override | ||
public void initNamespace(String registryAddress, String namespaceId) { | ||
if (StringUtils.isEmpty(namespaceId)) { | ||
log.error("namespace is empty"); | ||
} | ||
if (context.isEmptyPaasServer()) { | ||
log.debug("initNamespace failed , could not find the paas address profile"); | ||
return; | ||
} | ||
InitNamespaceRequest initNamespaceRequest = InitNamespaceRequest.newBuilder() | ||
.setNamespaceId(namespaceId) | ||
.setRegistryAddress(registryAddress) | ||
.build(); | ||
try { | ||
PollingResult pollingResult = createStub().initNamespace(initNamespaceRequest); | ||
boolean success = GrpcHepler.success(pollingResult); | ||
if(!success){ | ||
log.error("init namespace failed,message={}",pollingResult.getMessage()); | ||
} | ||
} catch (Exception e) { | ||
log.error("init namespace failed, msg:{}", e.getMessage()); | ||
} | ||
} | ||
|
||
private PaasPollingGrpc.PaasPollingBlockingStub createStub() { | ||
return PaasPollingGrpc.newBlockingStub(channel).withDeadlineAfter(10,TimeUnit.SECONDS); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
...in/src/main/java/com/tencent/tsf/femas/endpoint/configlisten/GrpcLongPollingEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package com.tencent.tsf.femas.endpoint.configlisten; | ||
|
||
import com.tencent.tsf.femas.config.grpc.paas.*; | ||
import com.tencent.tsf.femas.context.ApplicationContextHelper; | ||
import com.tencent.tsf.femas.service.namespace.NamespaceMangerService; | ||
import com.tencent.tsf.femas.service.registry.ServiceManagerService; | ||
import com.tencent.tsf.femas.service.rule.ConvertService; | ||
import io.grpc.Context; | ||
import io.grpc.Status; | ||
import io.grpc.stub.StreamObserver; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
|
||
/** | ||
* @ClassName Grpc | ||
* @Description TODO | ||
* @Author cstongwei | ||
* @Date 2022/5/30 10:28 | ||
* @Version 1.0.0 | ||
**/ | ||
@Slf4j | ||
public class GrpcLongPollingEndpoint extends PaasPollingGrpc.PaasPollingImplBase{ | ||
|
||
ConvertService convertService; | ||
|
||
ServiceManagerService serviceManagerService; | ||
|
||
NamespaceMangerService namespaceMangerService; | ||
|
||
public GrpcLongPollingEndpoint() { | ||
convertService = ApplicationContextHelper.getBean(ConvertService.class); | ||
serviceManagerService = ApplicationContextHelper.getBean(ServiceManagerService.class); | ||
namespaceMangerService = ApplicationContextHelper.getBean(NamespaceMangerService.class); | ||
} | ||
|
||
@Override | ||
public void fetchBreakerRule(SimpleParam request, StreamObserver<PollingResult> responseObserver) { | ||
try { | ||
String fetchBreakerRule = convertService.convert(request.getParam()); | ||
if(Context.current().isCancelled()){ | ||
log.debug("request is cancelled!"); | ||
responseObserver.onError(Status.CANCELLED.withDescription("request is cancelled!").asRuntimeException()); | ||
return; | ||
} | ||
PollingResult result; | ||
if(fetchBreakerRule!=null){ | ||
result = GrpcHepler.ok(fetchBreakerRule); | ||
}else{ | ||
result = GrpcHepler.ok(); | ||
} | ||
responseObserver.onNext(result); | ||
responseObserver.onCompleted(); | ||
}catch (Throwable e){ | ||
responseObserver.onNext(GrpcHepler.fail(e.toString())); | ||
responseObserver.onCompleted(); | ||
log.error("error",e); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void reportServiceApi(ServiceApiRequest request, StreamObserver<PollingResult> responseObserver) { | ||
if (log.isDebugEnabled()) { | ||
log.debug("ServiceApiRequest: {}", request); | ||
} | ||
try { | ||
serviceManagerService.reportServiceApi(request.getNamespaceId(), | ||
request.getServiceName(), request.getApplicationVersion(), | ||
request.getData()); | ||
if(Context.current().isCancelled()){ | ||
log.debug("request is cancelled!"); | ||
responseObserver.onError(Status.CANCELLED.withDescription("request is cancelled!").asRuntimeException()); | ||
return; | ||
} | ||
responseObserver.onNext(GrpcHepler.ok()); | ||
responseObserver.onCompleted(); | ||
}catch (Throwable e){ | ||
responseObserver.onNext(GrpcHepler.fail(e.getCause().toString())); | ||
responseObserver.onCompleted(); | ||
log.error("error",e); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void reportServiceEvent(ReportEventRequest request, StreamObserver<PollingResult> responseObserver) { | ||
try { | ||
serviceManagerService.reportServiceEvent(request.getNamespaceId(), request.getServiceName(), request.getEventId(), request.getData()); | ||
if(Context.current().isCancelled()){ | ||
log.debug("request is cancelled!"); | ||
responseObserver.onError(Status.CANCELLED.withDescription("request is cancelled!").asRuntimeException()); | ||
return; | ||
} | ||
responseObserver.onNext(GrpcHepler.ok()); | ||
responseObserver.onCompleted(); | ||
}catch (Throwable e){ | ||
responseObserver.onNext(GrpcHepler.fail(e.getCause().toString())); | ||
responseObserver.onCompleted(); | ||
log.error("error",e); | ||
} | ||
} | ||
|
||
@Override | ||
public void initNamespace(InitNamespaceRequest request, StreamObserver<PollingResult> responseObserver) { | ||
try { | ||
namespaceMangerService.initNamespace(request.getRegistryAddress(), request.getNamespaceId()); | ||
if(Context.current().isCancelled()){ | ||
log.debug("request is cancelled!"); | ||
responseObserver.onError(Status.CANCELLED.withDescription("request is cancelled!").asRuntimeException()); | ||
return; | ||
} | ||
responseObserver.onNext(GrpcHepler.ok()); | ||
responseObserver.onCompleted(); | ||
}catch (Throwable e){ | ||
responseObserver.onNext(GrpcHepler.fail(e.getCause().toString())); | ||
responseObserver.onCompleted(); | ||
log.error("error",e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.