Search in sources :

Example 1 with PolarisAgentProperties

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);
}
Also used : PolarisAgentProperties(cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties) GetAllInstancesRequest(com.tencent.polaris.api.rpc.GetAllInstancesRequest)

Example 2 with PolarisAgentProperties

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();
}
Also used : ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) PolarisAgentProperties(cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties) GetInstancesRequest(com.tencent.polaris.api.rpc.GetInstancesRequest) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Example 3 with PolarisAgentProperties

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));
}
Also used : ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) PolarisAgentProperties(cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties) ServiceInstance(org.springframework.cloud.client.ServiceInstance) Instance(com.tencent.polaris.api.pojo.Instance) PolarisServiceInstance(cn.polarismesh.agent.core.spring.cloud.discovery.PolarisServiceInstance) GetOneInstanceRequest(com.tencent.polaris.api.rpc.GetOneInstanceRequest) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse) PolarisServiceInstance(cn.polarismesh.agent.core.spring.cloud.discovery.PolarisServiceInstance)

Example 4 with PolarisAgentProperties

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();
}
Also used : ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) ProcessRoutersResponse(com.tencent.polaris.router.api.rpc.ProcessRoutersResponse) PolarisAgentProperties(cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties) ProcessRoutersRequest(com.tencent.polaris.router.api.rpc.ProcessRoutersRequest) ArrayList(java.util.ArrayList)

Example 5 with PolarisAgentProperties

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);
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) GenericReactiveWebApplicationContext(org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext) PolarisContext(cn.polarismesh.agent.core.spring.cloud.context.PolarisContext) GenericReactiveWebApplicationContext(org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext) PolarisAgentProperties(cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext)

Aggregations

PolarisAgentProperties (cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties)5 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)3 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)2 PolarisContext (cn.polarismesh.agent.core.spring.cloud.context.PolarisContext)1 PolarisServiceInstance (cn.polarismesh.agent.core.spring.cloud.discovery.PolarisServiceInstance)1 Instance (com.tencent.polaris.api.pojo.Instance)1 GetAllInstancesRequest (com.tencent.polaris.api.rpc.GetAllInstancesRequest)1 GetInstancesRequest (com.tencent.polaris.api.rpc.GetInstancesRequest)1 GetOneInstanceRequest (com.tencent.polaris.api.rpc.GetOneInstanceRequest)1 ProcessRoutersRequest (com.tencent.polaris.router.api.rpc.ProcessRoutersRequest)1 ProcessRoutersResponse (com.tencent.polaris.router.api.rpc.ProcessRoutersResponse)1 ArrayList (java.util.ArrayList)1 GenericReactiveWebApplicationContext (org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext)1 ServiceInstance (org.springframework.cloud.client.ServiceInstance)1 ApplicationContext (org.springframework.context.ApplicationContext)1 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)1