Java Code Examples for org.springframework.cloud.client.ServiceInstance#getHost()
The following examples show how to use
org.springframework.cloud.client.ServiceInstance#getHost() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PageRedirectionFilter.java From api-layer with Eclipse Public License 2.0 | 6 votes |
/** * Find matched url in specified service. For each service instance, the method first checks if the hostname and port * of the service instance are the same as the hostname and port in Location url. If they are the same, then try to * find the matched url. * * @param location url in Location header * @param serviceId specified serviceId * @return return matched url if it can be found * return empty if matched url can not be found */ private Optional<String> foundMatchedUrlInService(String location, String serviceId) { List<ServiceInstance> serviceInstances = discovery.getInstances(serviceId); for (ServiceInstance instance : serviceInstances) { //Check if the host and port in location is registered in DS String host = instance.getHost() + ":" + instance.getPort(); if (location.contains(host)) { try { String transformedUrl = transformService.transformURL(ServiceType.ALL, serviceId, location, routedServicesMap.get(serviceId)); return Optional.of(transformedUrl); } catch (URLTransformationException e) { //do nothing if no matched url is found } } } return Optional.empty(); }
Example 2
Source File: GreeterServiceConsumer.java From microservice-skeleton with MIT License | 6 votes |
public void greet(String name, String message) { if (discoveryClient == null) { logger.info("Discovery client is null"); } else { logger.info("Discovery client is not null"); try { List<ServiceInstance> servers = discoveryClient.getInstances("service-account"); for (ServiceInstance server : servers) { String hostName = server.getHost(); int gRpcPort = Integer.parseInt(server.getMetadata().get("grpc.port")); logger.info("=====>> " + hostName + " ---- " + gRpcPort); final ManagedChannel channel = ManagedChannelBuilder.forAddress(hostName, gRpcPort) .usePlaintext(true) .build(); final GreetingGrpc.GreetingFutureStub stub = GreetingGrpc.newFutureStub(channel); stub.sayHi(HelloRequest.newBuilder().setName(name).setMessage(message).build()); } } catch (Exception e) { e.printStackTrace(); } } }
Example 3
Source File: CustomerClient.java From microservice with Apache License 2.0 | 6 votes |
private String customerURL() { if (useRibbon) { ServiceInstance instance = loadBalancer.choose("CUSTOMER"); return "http://" + instance.getHost() + ":" + instance.getPort() + "/customer/"; } else { return "http://" + customerServiceHost + ":" + customerServicePort + "/customer/"; } }
Example 4
Source File: DefaultServiceLoadBalancer.java From camel-spring-boot with Apache License 2.0 | 5 votes |
protected ServiceDefinition convertServiceInstanceToServiceDefinition(ServiceInstance instance) { return new DefaultServiceDefinition( instance.getServiceId(), instance.getHost(), instance.getPort(), instance.getMetadata() ); }
Example 5
Source File: SampleLatticeApplication.java From spring-cloud-lattice with Apache License 2.0 | 5 votes |
@RequestMapping("/hi") public String hi() { ServiceInstance instance = discoveryClient.getLocalServiceInstance(); String msg = instance.getServiceId() + ":" + instance.getHost() + ":" + instance.getPort(); log.info("/hi called: " + msg); return msg; }
Example 6
Source File: LoadBalancerUriTools.java From spring-cloud-commons with Apache License 2.0 | 5 votes |
private static URI doReconstructURI(ServiceInstance serviceInstance, URI original) { String host = serviceInstance.getHost(); String scheme = Optional.ofNullable(serviceInstance.getScheme()) .orElse(computeScheme(original, serviceInstance)); int port = computePort(serviceInstance.getPort(), scheme); if (Objects.equals(host, original.getHost()) && port == original.getPort() && Objects.equals(scheme, original.getScheme())) { return original; } boolean encoded = containsEncodedParts(original); return UriComponentsBuilder.fromUri(original).scheme(scheme).host(host).port(port) .build(encoded).toUri(); }
Example 7
Source File: JobCenterAutoConfiguration.java From summerframework with Apache License 2.0 | 5 votes |
private String discoveryAdminAddress() { String serviceId = jobCenterProperties.getServiceId(); List<String> adminAddresList = new ArrayList<>(); List<ServiceInstance> instanceList = discoveryClient.getInstances(serviceId); for (ServiceInstance serviceInstance : instanceList) { try { String host = serviceInstance.getHost(); int port = serviceInstance.getPort(); LOGGER.info("Connecting {} ({}:{})", serviceId, host, port); String httpAddress = "http://" + host + ":" + port + "/"; if (isAdminReachable(httpAddress)) { adminAddresList.add(httpAddress); } else { LOGGER.error("Skip unreachable node {}", httpAddress); } } catch (Throwable e) { LOGGER.error("can not found node for admin!", e); } } if (adminAddresList.size() > 0) { adminAddresList.sort(Comparator.naturalOrder()); return String.join(",", adminAddresList); } else { LOGGER.error("Jobcenter server is down,will try after 30s"); return null; } }
Example 8
Source File: ClientDiscoveryImpl.java From BootNettyRpc with Apache License 2.0 | 5 votes |
private List<NettyClient> convertNettyClients(List<ServiceInstance> serviceInstances) { if (CollectionUtils.isEmpty( serviceInstances )) { return null; } List<NettyClient> nettyClients = new CopyOnWriteArrayList<>(); for (ServiceInstance serviceInstance : serviceInstances) { NettyClient nettyClient = new NettyClient(); Map<String, String> metadata = serviceInstance.getMetadata(); String host = metadata.get( NettyRpcConstants.NETTY_SERVER_HOST ); if (StringUtils.isEmpty( host )) { host = serviceInstance.getHost(); } nettyClient.setHost( host ); String name = metadata.get( NettyRpcConstants.NETTY_SERVER_NAME ); if (StringUtils.isEmpty( name )) { name = serviceInstance.getServiceId(); } nettyClient.setName( name ); String port = metadata.get( NettyRpcConstants.NETTY_SERVER_PORT ); if (StringUtils.isEmpty( port )) { throw new CommonException( "netty server " + name + " 's port connot be null" ); } nettyClient.setPort( Integer.valueOf( port ) ); nettyClients.add( nettyClient ); } return nettyClients; }
Example 9
Source File: HazelCastConfigration.java From jvue-admin with MIT License | 5 votes |
@Bean public HazelcastInstance hazelcastInstance() { log.debug("Configuring Hazelcast"); HazelcastInstance hazelCastInstance = Hazelcast.getHazelcastInstanceByName("jvue-server"); if (hazelCastInstance != null) { log.debug("Hazelcast already initialized"); return hazelCastInstance; } Config config = new Config(); config.setInstanceName("jvue-server"); config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); if (this.registration == null) { log.warn("No discovery service is set up, Hazelcast cannot create a cluster."); } else { String serviceId = registration.getServiceId(); log.debug("Configuring Hazelcast clustering for instanceId: {}", serviceId); config.getNetworkConfig().setPort(5701); config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true); for (ServiceInstance instance : discoveryClient.getInstances(serviceId)) { String clusterMember = instance.getHost() + ":5701"; log.debug("Adding Hazelcast (prod) cluster member " + clusterMember); config.getNetworkConfig().getJoin().getTcpIpConfig().addMember(clusterMember); } } config.getMapConfigs().put("default", initializeDefaultMapConfig()); config.getMapConfigs().put("net.ccfish.jvue.domain.*", initializeDefaultMapConfig()); return Hazelcast.newHazelcastInstance(config); }
Example 10
Source File: AbstractMicroserviceClient.java From jhipster-ribbon-hystrix with GNU General Public License v3.0 | 5 votes |
/** * Constructs a url for rest template * * @param path resource path on the service * @return a url String for use in RestTemplate */ protected String getUrl(String path) { String url; ServiceInstance instance = loadBalancerClient.choose(serviceName); String prefix = instance.isSecure() ? "https://" : "http://"; url = prefix + instance.getHost() + ":" + instance.getPort() + "/api/" + path; return url; }
Example 11
Source File: CustomerClient.java From microservice-consul with Apache License 2.0 | 5 votes |
private String customerURL() { if (useRibbon) { ServiceInstance instance = loadBalancer.choose("CUSTOMER"); return "http://" + instance.getHost() + ":" + instance.getPort() + "/customer/"; } else { return "http://" + customerServiceHost + ":" + customerServicePort + "/customer/"; } }
Example 12
Source File: CatalogClient.java From microservice-consul with Apache License 2.0 | 5 votes |
private String catalogURL() { if (useRibbon) { ServiceInstance instance = loadBalancer.choose("CATALOG"); return "http://" + instance.getHost() + ":" + instance.getPort() + "/catalog/"; } else { return "http://" + catalogServiceHost + ":" + catalogServicePort + "/catalog/"; } }
Example 13
Source File: SettingServiceImpl.java From sds with Apache License 2.0 | 5 votes |
private List<String> getServices(String key){ List<String> urls = new ArrayList<>(); List<ServiceInstance> serviceInstances = discoveryClient.getInstances(key); for (ServiceInstance instanceInfo : serviceInstances) { String url = instanceInfo.getUri().toString(); String address = instanceInfo.getHost(); if (isIp(address)) { urls.add(url); }else{ url = url.replace(address,"127.0.0.1"); urls.add(url); } } return urls; }
Example 14
Source File: HazelCastConfigration.java From sctalk with Apache License 2.0 | 5 votes |
@Bean public HazelcastInstance hazelcastInstance(MessageServerConfig serverConfig) { log.debug("Configuring Hazelcast"); HazelcastInstance hazelCastInstance = Hazelcast.getHazelcastInstanceByName("message-server"); if (hazelCastInstance != null) { log.debug("Hazelcast already initialized"); return hazelCastInstance; } Config config = new Config(); config.setInstanceName("message-server"); config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false); if (this.registration == null) { log.warn("No discovery service is set up, Hazelcast cannot create a cluster."); } else { // The serviceId is by default the application's name, see Spring Boot's // eureka.instance.appname property String serviceId = registration.getServiceId(); log.debug("Configuring Hazelcast clustering for instanceId: {}", serviceId); config.getNetworkConfig().setPort(5701); config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true); for (ServiceInstance instance : discoveryClient.getInstances(serviceId)) { String clusterMember = instance.getHost() + ":5701"; log.debug("Adding Hazelcast (prod) cluster member " + clusterMember); config.getNetworkConfig().getJoin().getTcpIpConfig().addMember(clusterMember); } } config.getMapConfigs().put("default", initializeDefaultMapConfig()); config.getMapConfigs().put("com.blt.talk.domain.*", initializeDomainMapConfig(serverConfig)); return Hazelcast.newHazelcastInstance(config); }
Example 15
Source File: ServiceBController.java From spring-cloud-consul-example with MIT License | 4 votes |
@RequestMapping(value = "/", method = RequestMethod.GET) public String printServiceB() { ServiceInstance serviceInstance = discoveryClient.getLocalServiceInstance(); return serviceInstance.getServiceId() + " (" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + ")" + "===>Say " + msg; }
Example 16
Source File: UserController.java From microservice-skeleton with MIT License | 4 votes |
public void greet(String name, String message) { // if (eurekaClient == null) { // log.info("Eureka client is empty"); // } else { // log.info("Eureka client is not empty"); // } if (discoveryClient == null) { log.info("Discovery client is null"); } else { log.info("Discovery client is not null"); try { List<ServiceInstance> servers = discoveryClient.getInstances("service-account"); for (ServiceInstance server : servers) { String hostName = server.getHost(); log.info(String.format("Hostname => %s", hostName)); for (String key : server.getMetadata().keySet()) { log.info(String.format("%s => %s : %s", hostName, key, server.getMetadata().get(key))); } int gRpcPort = 6565;//Integer.parseInt(server.getMetadata().get("grpc.port")); log.info("=====>> " + hostName + " ---- " + gRpcPort); final ManagedChannel channel = ManagedChannelBuilder.forAddress(hostName, gRpcPort) .usePlaintext(true) .build(); // final GreetingGrpc.GreetingFutureStub stub = GreetingGrpc.newFutureStub(channel); final GreetingGrpc.GreetingStub stub = GreetingGrpc.newStub(channel); log.info(String.format("Current time milis : %d", System.currentTimeMillis())); stub.sayHi(HelloRequest.newBuilder().setName(name).setMessage(message).build(), new StreamObserver<HelloReply>() { @Override public void onNext(HelloReply r) { log.info(String.format("Current time milis 2 : %d", System.currentTimeMillis())); log.info(String.format("Replied : %s , %s", r.getName(), r.getMessage())); } @Override public void onError(Throwable throwable) { } @Override public void onCompleted() { } }); } } catch (Exception e) { e.printStackTrace(); } } }
Example 17
Source File: ReservationClientApplication.java From bootiful-reactive-microservices with Apache License 2.0 | 4 votes |
private String uri(ServiceInstance si) { return "http://" + si.getHost() + ':' + si.getPort() + "/reservations"; }
Example 18
Source File: WebSocketProxyServerHandler.java From api-layer with Eclipse Public License 2.0 | 4 votes |
private String getTargetUrl(String serviceUrl, ServiceInstance serviceInstance, String path) { String servicePath = serviceUrl.charAt(serviceUrl.length() - 1) == '/' ? serviceUrl : serviceUrl + SEPARATOR; return (serviceInstance.isSecure() ? "wss" : "ws") + "://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + servicePath + path; }
Example 19
Source File: AServiceController.java From spring-cloud-consul-example with MIT License | 4 votes |
@RequestMapping(value = "/", method = RequestMethod.GET) public String printServiceA() { ServiceInstance serviceInstance = discoveryClient.getLocalServiceInstance(); return serviceInstance.getServiceId() + " (" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + ")" + "===>name:" + name + "<br/>" + serviceBClient.printServiceB(); }
Example 20
Source File: AbstractMicroserviceClient.java From jhipster-ribbon-hystrix with GNU General Public License v3.0 | 3 votes |
protected String getUrl(String path) { String url; ServiceInstance instance = loadBalancerClient.choose(serviceName); url = "http://" + instance.getHost() + ":" + instance.getPort() + "/api/" + path; return url; }