Skip to content

Commit

Permalink
远程调用优先注解
Browse files Browse the repository at this point in the history
  • Loading branch information
qinfuxiang committed Nov 2, 2021
1 parent 1e450f3 commit d98b388
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ public static ServerContext connect(String serverName) {
public static ServerContext request(String serverName) {
DynamicServiceSelector dynamicServiceSelector = ApplicationContextUtil.getBean(DynamicServiceSelector.class);
ServiceInstance serviceInstance = dynamicServiceSelector.getNextServer(serverName);
ManagedChannel channel = ManagedChannelBuilder.forAddress(serviceInstance.getHost(), serviceInstance.getPort())
return request(serviceInstance.getHost(), serviceInstance.getPort());
}


public static ServerContext request(String host,Integer port) {
ManagedChannel channel = ManagedChannelBuilder.forAddress(host, port)
// .defaultLoadBalancingPolicy("round_robin")
// .nameResolverFactory(new DnsNameResolverProvider())
.idleTimeout(30, TimeUnit.SECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void start() throws Exception{
startDaemonAwaitThread();
//注册到注册中心
String grpcServerName = grpcProperties.getGrpcServerName();
//抽象注册中心
//TODO 抽象注册中心
namingService.registerInstance(grpcServerName, NetUtils.getLocalHost(),port);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* @author qinfuxiang
*/
@Documented
@Inherited
@Retention(RUNTIME)
Expand All @@ -25,4 +28,8 @@
*/
SerializeType[] serialization() default {};

String host() default "";

int port() default 0;

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import fast.cloud.nacos.grpc.starter.exception.GrpcException;
import fast.cloud.nacos.grpc.starter.service.GrpcRequest;
import fast.cloud.nacos.grpc.starter.service.GrpcResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cglib.proxy.InvocationHandler;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Objects;

public class GrpcServiceProxy<T> implements InvocationHandler {

Expand All @@ -35,19 +37,28 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
Object another = args[0];
return proxy == another;
}
GrpcService annotation = grpcService.getAnnotation(GrpcService.class);
String server = annotation.server();
GrpcService grpcServiceAnnotation = grpcService.getAnnotation(GrpcService.class);
GrpcRequest request = new GrpcRequest();
request.setClazz(className);
request.setMethod(methodName);
request.setArgs(args);
SerializeType[] serializeTypeArray = annotation.serialization();
SerializeType[] serializeTypeArray = grpcServiceAnnotation.serialization();
SerializeType serializeType = null;
if (serializeTypeArray.length > 0) {
serializeType = serializeTypeArray[0];
}
GrpcResponse response = GrpcClient.request(annotation.grpcServer()).handle(serializeType, request);
// GrpcResponse response = GrpcClient.connect(server).handle(serializeType, request);
GrpcResponse response;
String host = grpcServiceAnnotation.host();
int port = grpcServiceAnnotation.port();

//远程调用地址注解优先级最高
if (StringUtils.isNotBlank(host) && Objects.nonNull(port)) {
response = GrpcClient.request(host,port).handle(serializeType, request);
} else {
response = GrpcClient.request(grpcServiceAnnotation.grpcServer()).handle(serializeType, request);
}

//TODO 超时时间按照appId隔离
if (GrpcResponseStatus.ERROR.getCode() == response.getStatus()) {
Throwable throwable = response.getException();
GrpcException exception = new GrpcException(throwable.getClass().getName() + ": " + throwable.getMessage());
Expand Down

0 comments on commit d98b388

Please sign in to comment.