Search in sources :

Example 6 with ProviderAPI

use of com.tencent.polaris.api.core.ProviderAPI in project polaris-java by polarismesh.

the class Provider method main.

public static void main(String[] args) throws Exception {
    String namespace = NAMESPACE_DEFAULT;
    String service = ECHO_SERVICE_NAME;
    HttpServer server = HttpServer.create(new InetSocketAddress(LISTEN_PORT), 0);
    server.createContext("/echo", new EchoServerHandler());
    Configuration configuration = ConfigAPIFactory.defaultConfig();
    String localHost = getLocalHost(configuration);
    int localPort = server.getAddress().getPort();
    // List<ServerConnectorConfigImpl> connectorConfigList = configuration.getGlobal().getServerConnectors();
    // for (ServerConnectorConfigImpl serverConnectorConfig : connectorConfigList) {
    // if (DefaultPlugins.SERVER_CONNECTOR_CONSUL.equals(serverConnectorConfig.getProtocol())) {
    // Map<String, String> metadata = serverConnectorConfig.getMetadata();
    // metadata.put(MetadataMapKey.INSTANCE_ID_KEY, "EJ-111");
    // metadata.put(MetadataMapKey.IP_ADDRESS_KEY, "localhost");
    // metadata.put(MetadataMapKey.PREFER_IP_ADDRESS_KEY, "true");
    // }
    // }
    ProviderAPI providerAPI = DiscoveryAPIFactory.createProviderAPIByConfig(configuration);
    HEARTBEAT_EXECUTOR.schedule(new RegisterTask(namespace, service, localHost, localPort, providerAPI), 500, TimeUnit.MILLISECONDS);
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        HEARTBEAT_EXECUTOR.shutdown();
        server.stop(1);
        deregister(namespace, service, localHost, localPort, providerAPI);
        providerAPI.close();
    }));
    server.start();
}
Also used : ProviderAPI(com.tencent.polaris.api.core.ProviderAPI) Configuration(com.tencent.polaris.api.config.Configuration) InetSocketAddress(java.net.InetSocketAddress) HttpServer(com.sun.net.httpserver.HttpServer)

Example 7 with ProviderAPI

use of com.tencent.polaris.api.core.ProviderAPI in project spring-cloud-tencent by Tencent.

the class PolarisServiceRegistry method deregister.

@Override
public void deregister(Registration registration) {
    log.info("De-registering from Polaris Server now...");
    if (StringUtils.isEmpty(registration.getServiceId())) {
        log.warn("No dom to de-register for polaris client...");
        return;
    }
    InstanceDeregisterRequest deRegisterRequest = new InstanceDeregisterRequest();
    deRegisterRequest.setToken(polarisDiscoveryProperties.getToken());
    deRegisterRequest.setNamespace(polarisDiscoveryProperties.getNamespace());
    deRegisterRequest.setService(registration.getServiceId());
    deRegisterRequest.setHost(registration.getHost());
    deRegisterRequest.setPort(registration.getPort());
    try {
        ProviderAPI providerClient = polarisDiscoveryHandler.getProviderAPI();
        providerClient.deRegister(deRegisterRequest);
    } catch (Exception e) {
        log.error("ERR_POLARIS_DEREGISTER, de-register failed...{},", registration, e);
    } finally {
        if (null != heartbeatExecutor) {
            heartbeatExecutor.shutdown();
        }
    }
    log.info("De-registration finished.");
}
Also used : ProviderAPI(com.tencent.polaris.api.core.ProviderAPI) ReflectionUtils.rethrowRuntimeException(org.springframework.util.ReflectionUtils.rethrowRuntimeException) PolarisException(com.tencent.polaris.api.exception.PolarisException) InstanceDeregisterRequest(com.tencent.polaris.api.rpc.InstanceDeregisterRequest)

Example 8 with ProviderAPI

use of com.tencent.polaris.api.core.ProviderAPI in project spring-cloud-tencent by Tencent.

the class PolarisServiceRegistry method register.

@Override
public void register(Registration registration) {
    if (StringUtils.isEmpty(registration.getServiceId())) {
        log.warn("No service to register for polaris client...");
        return;
    }
    // Register instance.
    InstanceRegisterRequest instanceRegisterRequest = new InstanceRegisterRequest();
    instanceRegisterRequest.setNamespace(polarisDiscoveryProperties.getNamespace());
    instanceRegisterRequest.setService(registration.getServiceId());
    instanceRegisterRequest.setHost(registration.getHost());
    instanceRegisterRequest.setPort(registration.getPort());
    instanceRegisterRequest.setWeight(polarisDiscoveryProperties.getWeight());
    instanceRegisterRequest.setToken(polarisDiscoveryProperties.getToken());
    instanceRegisterRequest.setRegion(staticMetadataManager.getRegion());
    instanceRegisterRequest.setZone(staticMetadataManager.getZone());
    instanceRegisterRequest.setCampus(staticMetadataManager.getCampus());
    if (null != heartbeatExecutor) {
        instanceRegisterRequest.setTtl(ttl);
    }
    instanceRegisterRequest.setMetadata(registration.getMetadata());
    instanceRegisterRequest.setProtocol(polarisDiscoveryProperties.getProtocol());
    instanceRegisterRequest.setVersion(polarisDiscoveryProperties.getVersion());
    try {
        ProviderAPI providerClient = polarisDiscoveryHandler.getProviderAPI();
        providerClient.register(instanceRegisterRequest);
        log.info("polaris registry, {} {} {}:{} {} register finished", polarisDiscoveryProperties.getNamespace(), registration.getServiceId(), registration.getHost(), registration.getPort(), staticMetadataManager.getMergedStaticMetadata());
        if (null != heartbeatExecutor) {
            InstanceHeartbeatRequest heartbeatRequest = new InstanceHeartbeatRequest();
            BeanUtils.copyProperties(instanceRegisterRequest, heartbeatRequest);
            // Start the heartbeat thread after the registration is successful.
            heartbeat(heartbeatRequest);
        }
    } catch (Exception e) {
        log.error("polaris registry, {} register failed...{},", registration.getServiceId(), registration, e);
        rethrowRuntimeException(e);
    }
}
Also used : ProviderAPI(com.tencent.polaris.api.core.ProviderAPI) InstanceRegisterRequest(com.tencent.polaris.api.rpc.InstanceRegisterRequest) ReflectionUtils.rethrowRuntimeException(org.springframework.util.ReflectionUtils.rethrowRuntimeException) PolarisException(com.tencent.polaris.api.exception.PolarisException) InstanceHeartbeatRequest(com.tencent.polaris.api.rpc.InstanceHeartbeatRequest)

Example 9 with ProviderAPI

use of com.tencent.polaris.api.core.ProviderAPI in project polaris-java by polarismesh.

the class HeartbeatExample method main.

public static void main(String[] args) throws Exception {
    InitResult initResult = ExampleUtils.initProviderConfiguration(args);
    String namespace = initResult.getNamespace();
    String service = initResult.getService();
    String host = initResult.getHost();
    int port = initResult.getPort();
    String token = initResult.getToken();
    try (ProviderAPI providerAPI = ExampleUtils.createProviderAPI(initResult.getConfig())) {
        InstanceHeartbeatRequest instanceHeartbeatRequest = new InstanceHeartbeatRequest();
        instanceHeartbeatRequest.setNamespace(namespace);
        instanceHeartbeatRequest.setService(service);
        instanceHeartbeatRequest.setHost(host);
        instanceHeartbeatRequest.setPort(port);
        instanceHeartbeatRequest.setToken(token);
        providerAPI.heartbeat(instanceHeartbeatRequest);
        System.out.println("heartbeat for service successfully: " + service);
    }
}
Also used : ProviderAPI(com.tencent.polaris.api.core.ProviderAPI) InitResult(com.tencent.polaris.discovery.example.utils.ExampleUtils.InitResult) InstanceHeartbeatRequest(com.tencent.polaris.api.rpc.InstanceHeartbeatRequest)

Example 10 with ProviderAPI

use of com.tencent.polaris.api.core.ProviderAPI in project polaris-java by polarismesh.

the class RegisterExample method main.

public static void main(String[] args) throws Exception {
    InitResult initResult = ExampleUtils.initProviderConfiguration(args);
    String namespace = initResult.getNamespace();
    String service = initResult.getService();
    String host = initResult.getHost();
    int port = initResult.getPort();
    String token = initResult.getToken();
    try (ProviderAPI providerAPI = ExampleUtils.createProviderAPI(initResult.getConfig())) {
        InstanceRegisterRequest instanceRegisterRequest = new InstanceRegisterRequest();
        instanceRegisterRequest.setNamespace(namespace);
        instanceRegisterRequest.setService(service);
        instanceRegisterRequest.setHost(host);
        instanceRegisterRequest.setPort(port);
        instanceRegisterRequest.setToken(token);
        if (initResult.getTtl() > 0) {
            instanceRegisterRequest.setTtl(initResult.getTtl());
        }
        InstanceRegisterResponse instanceRegisterResponse = providerAPI.register(instanceRegisterRequest);
        System.out.println("response after register is " + instanceRegisterResponse);
    }
}
Also used : ProviderAPI(com.tencent.polaris.api.core.ProviderAPI) InitResult(com.tencent.polaris.discovery.example.utils.ExampleUtils.InitResult) InstanceRegisterRequest(com.tencent.polaris.api.rpc.InstanceRegisterRequest) InstanceRegisterResponse(com.tencent.polaris.api.rpc.InstanceRegisterResponse)

Aggregations

ProviderAPI (com.tencent.polaris.api.core.ProviderAPI)12 PolarisException (com.tencent.polaris.api.exception.PolarisException)5 InstanceHeartbeatRequest (com.tencent.polaris.api.rpc.InstanceHeartbeatRequest)5 InstanceDeregisterRequest (com.tencent.polaris.api.rpc.InstanceDeregisterRequest)4 InstanceRegisterRequest (com.tencent.polaris.api.rpc.InstanceRegisterRequest)4 ReflectionUtils.rethrowRuntimeException (org.springframework.util.ReflectionUtils.rethrowRuntimeException)4 InitResult (com.tencent.polaris.discovery.example.utils.ExampleUtils.InitResult)3 Configuration (com.tencent.polaris.api.config.Configuration)2 InstanceRegisterResponse (com.tencent.polaris.api.rpc.InstanceRegisterResponse)2 Test (org.junit.Test)2 HttpServer (com.sun.net.httpserver.HttpServer)1 ConsumerAPI (com.tencent.polaris.api.core.ConsumerAPI)1 Instance (com.tencent.polaris.api.pojo.Instance)1 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)1 ServiceCallResult (com.tencent.polaris.api.rpc.ServiceCallResult)1 InitResult (com.tencent.polaris.circuitbreak.example.CircuitBreakExampleUtils.InitResult)1 SDKContext (com.tencent.polaris.client.api.SDKContext)1 InputStream (java.io.InputStream)1 InetSocketAddress (java.net.InetSocketAddress)1