use of cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties in project polaris-java-agent by polarismesh.
the class PolarisDiscoveryHandler method getInstances.
/**
* Return all instances for the given service.
*
* @param service serviceName
* @return 服务实例列表
*/
public InstancesResponse getInstances(String service) {
LogUtils.logInvoke(this, "getInstances");
PolarisAgentProperties polarisAgentProperties = PolarisAgentPropertiesFactory.getPolarisAgentProperties();
String namespace = polarisAgentProperties.getNamespace();
GetAllInstancesRequest request = new GetAllInstancesRequest();
request.setNamespace(namespace);
request.setService(service);
return consumerAPI.getAllInstance(request);
}
use of cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties in project polaris-java-agent by polarismesh.
the class PolarisServiceRouter method getInstances.
/**
* 利用ConsumerAPI进行路由
*
* @param service
* @return
*/
public static ServiceInstances getInstances(String service) {
LogUtils.logInvoke(PolarisServiceRouter.class, "getInstances");
PolarisAgentProperties polarisAgentProperties = PolarisAgentPropertiesFactory.getPolarisAgentProperties();
String namespace = polarisAgentProperties.getNamespace();
GetInstancesRequest getInstancesRequest = new GetInstancesRequest();
getInstancesRequest.setNamespace(namespace);
getInstancesRequest.setService(service);
String srcNamespace = polarisAgentProperties.getNamespace();
String srcService = polarisAgentProperties.getService();
if (StringUtils.isNotBlank(srcNamespace) || StringUtils.isNotBlank(srcService)) {
ServiceInfo sourceService = new ServiceInfo();
sourceService.setNamespace(srcNamespace);
sourceService.setService(srcService);
getInstancesRequest.setServiceInfo(sourceService);
}
InstancesResponse response = PolarisAPIFactory.getConsumerApi().getInstances(getInstancesRequest);
LOGGER.info("success to route by Polaris with instance size:{}", response.getInstances().length);
return response.toServiceInstances();
}
use of cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties in project polaris-java-agent by polarismesh.
the class PolarisServiceRouter method getOneInstance.
/**
* 利用ConsumerAPI进行路由、负载均衡
*
* @param service
* @return
*/
public static ServiceInstance getOneInstance(String service) {
LogUtils.logInvoke(PolarisServiceRouter.class, "getOneInstance");
PolarisAgentProperties polarisAgentProperties = PolarisAgentPropertiesFactory.getPolarisAgentProperties();
String namespace = polarisAgentProperties.getNamespace();
GetOneInstanceRequest getOneInstanceRequest = new GetOneInstanceRequest();
getOneInstanceRequest.setService(service);
getOneInstanceRequest.setNamespace(namespace);
String srcNamespace = polarisAgentProperties.getNamespace();
String srcService = polarisAgentProperties.getService();
if (StringUtils.isNotBlank(srcNamespace) || StringUtils.isNotBlank(srcService)) {
ServiceInfo sourceService = new ServiceInfo();
sourceService.setNamespace(srcNamespace);
sourceService.setService(srcService);
getOneInstanceRequest.setServiceInfo(sourceService);
}
Map<String, String> metadata = InvokeContextHolder.get().getMetadata();
if (metadata != null) {
getOneInstanceRequest.setMetadata(metadata);
}
InstancesResponse response = PolarisAPIFactory.getConsumerApi().getOneInstance(getOneInstanceRequest);
List<Instance> instances = response.toServiceInstances().getInstances();
if (CollectionUtils.isNotEmpty(instances)) {
LOGGER.info("success to route and loadBalance by Polaris with instance:{}", instances.get(0));
}
return new PolarisServiceInstance(instances.get(0));
}
use of cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties in project polaris-java-agent by polarismesh.
the class PolarisServiceRouter method getRoutedServiceInstance.
/**
* 路由服务实例(不具有熔断功能)
*
* @param dstInstances
* @return
*/
public static ServiceInstances getRoutedServiceInstance(ServiceInstances dstInstances) {
LogUtils.logInvoke(PolarisServiceRouter.class, "getRoutedServiceInstance");
PolarisAgentProperties agentProperties = PolarisAgentPropertiesFactory.getPolarisAgentProperties();
// 执行服务路由
ProcessRoutersRequest processRoutersRequest = new ProcessRoutersRequest();
// 主调方信息
ServiceInfo srcSourceInfo = new ServiceInfo();
String srcService = agentProperties.getService();
String srcNamespace = agentProperties.getNamespace();
if (StringUtils.isNotBlank(srcNamespace) && StringUtils.isNotBlank(srcService)) {
srcSourceInfo.setNamespace(srcNamespace);
srcSourceInfo.setService(srcService);
processRoutersRequest.setSourceService(srcSourceInfo);
}
ProcessRoutersRequest.RouterNamesGroup routerNamesGroup = new ProcessRoutersRequest.RouterNamesGroup();
List<String> coreRouters = new ArrayList<>();
coreRouters.add(ServiceRouterConfig.DEFAULT_ROUTER_RULE);
coreRouters.add(ServiceRouterConfig.DEFAULT_ROUTER_METADATA);
coreRouters.add(ServiceRouterConfig.DEFAULT_ROUTER_NEARBY);
// 设置走规则路由
routerNamesGroup.setCoreRouters(coreRouters);
processRoutersRequest.setDstInstances(dstInstances);
processRoutersRequest.setSourceService(srcSourceInfo);
processRoutersRequest.setRouters(routerNamesGroup);
ProcessRoutersResponse processRoutersResponse = PolarisAPIFactory.getRouterApi().processRouters(processRoutersRequest);
LOGGER.info("success to route by Polaris with instance size:{}", processRoutersResponse.getServiceInstances().getInstances().size());
return processRoutersResponse.getServiceInstances();
}
use of cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties in project polaris-java-agent by polarismesh.
the class PolarisAgentPropertiesInterceptor method beforeInterceptor.
@Override
public void beforeInterceptor(Object target, Object[] args, PolarisAgentProperties agentProperties) {
// check if servlet applicationContext or reactive applicationContext
Object configurableContext = args[0];
if (configurableContext instanceof GenericWebApplicationContext || configurableContext instanceof GenericReactiveWebApplicationContext) {
// log
LogUtils.logTargetFound(target);
// convert to applicationContext, actual AnnotationConfigServletWebApplicationContext or AnnotationConfigReactiveWebServerApplicationContext
ApplicationContext applicationContext = (ApplicationContext) configurableContext;
// get basic info from applicationContext
PORT = applicationContext.getEnvironment().getProperty("server.port");
SERVICE = applicationContext.getEnvironment().getProperty("spring.application.name");
HOST = applicationContext.getEnvironment().getProperty("spring.cloud.client.ip-address");
if (PORT == null) {
log.warn("the server port is empty loaded from application config, use '8080' instead");
}
Assert.notNull(SERVICE, "the application name can't be null, please check your spring config");
log.info("Polaris service is set with name: {}, host: {}, port: {}", SERVICE, HOST, PORT);
// get init info from system
String host = HostUtils.getHost();
String namespace = System.getProperty("polaris.namespace");
String serverAddress = System.getProperty("polaris.server.address");
String protocol = System.getProperty("polaris.server.protocol");
Assert.notNull(serverAddress, "the polaris server address can't be null, please check your polaris agent parameter");
if (StringUtils.isEmpty(namespace)) {
log.warn("the input namespace is empty, use 'default' instead");
}
if (StringUtils.isEmpty(protocol)) {
log.warn("the input protocol is empty, use 'grpc' instead");
}
// init polaris config and reserve
PolarisAgentProperties polarisAgentProperties = PolarisAgentProperties.builder().withHost(host).withPort(PORT).withProtocol(protocol).withServerAddress(serverAddress).withNamespace(namespace).withService(SERVICE).build();
PolarisAgentPropertiesFactory.setPolarisAgentProperties(polarisAgentProperties);
// init polarisContext and api
PolarisContext polarisContext = new PolarisContext(polarisAgentProperties);
PolarisAPIFactory.init(polarisContext);
}
}
Aggregations