grpc 实现

This commit is contained in:
三丙
2024-10-23 17:07:57 +08:00
parent 7445d4e3f0
commit 5a1b4f8303
21 changed files with 715 additions and 141 deletions

View File

@@ -19,7 +19,6 @@ import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
@@ -42,15 +41,18 @@ public class DefaultServiceInfoProvider implements ServiceInfoProvider {
private ServiceInfo serviceInfo;
@Getter
private String serviceWebapiEndpoint;
private String hostAddress;
@Getter
@Value("${server.port}")
private String webapiPort;
private int restPort;
@Getter
@Value("${service.protocol.rpc.port:9090}")
private int grpcPort;
@PostConstruct
public void init() throws UnknownHostException {
if (!StringUtils.hasText(this.serviceId)) {
try {
this.serviceId = InetAddress.getLocalHost().getHostName();
@@ -58,10 +60,11 @@ public class DefaultServiceInfoProvider implements ServiceInfoProvider {
this.serviceId = RandomStringUtils.randomAlphabetic(10);
}
}
log.info("Current Service ID: {}", this.serviceId);
log.info("Current Service ID: {}", serviceId);
serviceWebapiEndpoint = InetAddress.getLocalHost().getHostAddress() + ":" + webapiPort;
log.info("Current Service HostAddress: {}", this.serviceWebapiEndpoint);
hostAddress = InetAddress.getLocalHost().getHostAddress();
log.info("Current Service HostAddress: {}, RestPort:{}, GrpcPort:{}", hostAddress, restPort, grpcPort);
if (serviceType.equalsIgnoreCase("monolith")) {
serviceTypes = List.of(ServiceType.values());
} else {
@@ -86,7 +89,7 @@ public class DefaultServiceInfoProvider implements ServiceInfoProvider {
public ServiceInfo generateNewServiceInfoWithCurrentSystemInfo() {
ServiceInfo.Builder builder = ServiceInfo.newBuilder()
.setServiceId(serviceId)
.addAllServiceTypes(serviceTypes.stream().map(ServiceType::name).collect(Collectors.toList()))
.addAllServiceTypes(serviceTypes.stream().map(ServiceType::name).toList())
.setSystemInfo(getCurrentSystemInfoProto());
return serviceInfo = builder.build();
}

View File

@@ -13,7 +13,11 @@ import sanbing.jcpp.proto.gen.ClusterProto;
public interface ServiceInfoProvider {
String getServiceId();
String getServiceWebapiEndpoint();
String getHostAddress();
int getRestPort();
int getGrpcPort();
String getServiceType();