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();
}
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.");
}
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);
}
}
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);
}
}
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);
}
}
Aggregations