本文整理汇总了Java中org.springframework.cloud.client.ServiceInstance.getHost方法的典型用法代码示例。如果您正苦于以下问题:Java ServiceInstance.getHost方法的具体用法?Java ServiceInstance.getHost怎么用?Java ServiceInstance.getHost使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.cloud.client.ServiceInstance
的用法示例。
在下文中一共展示了ServiceInstance.getHost方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: greet
import org.springframework.cloud.client.ServiceInstance; //导入方法依赖的package包/类
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();
}
}
}
示例2: getInstance
import org.springframework.cloud.client.ServiceInstance; //导入方法依赖的package包/类
private ServiceInstance getInstance(String ipPort) {
List<ServiceInstance> instances = discoveryClient.getInstances(DeliveryServerService.SOCKET_SERVER_KEY);
for (ServiceInstance instance : instances) {
String ip = instance.getHost();
int port = instance.getPort();
String instancesIpPort = String.format("%s:%d", ip, port);
if (instancesIpPort.equals(ipPort)) {
return instance;
}
}
return null;
}
示例3: getUrl
import org.springframework.cloud.client.ServiceInstance; //导入方法依赖的package包/类
/**
* 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;
}
示例4: hazelcastInstance
import org.springframework.cloud.client.ServiceInstance; //导入方法依赖的package包/类
@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);
}
示例5: catalogURL
import org.springframework.cloud.client.ServiceInstance; //导入方法依赖的package包/类
private String catalogURL() {
if (useRibbon) {
ServiceInstance instance = loadBalancer.choose("CATALOG");
return "http://" + instance.getHost() + ":" + instance.getPort()
+ "/catalog/";
} else {
return "http://" + catalogServiceHost + ":" + catalogServicePort
+ "/catalog/";
}
}
示例6: customerURL
import org.springframework.cloud.client.ServiceInstance; //导入方法依赖的package包/类
private String customerURL() {
if (useRibbon) {
ServiceInstance instance = loadBalancer.choose("CUSTOMER");
return "http://" + instance.getHost() + ":" + instance.getPort()
+ "/customer/";
} else {
return "http://" + customerServiceHost + ":" + customerServicePort
+ "/customer/";
}
}
示例7: greet
import org.springframework.cloud.client.ServiceInstance; //导入方法依赖的package包/类
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();
}
}
}
示例8: printServiceB
import org.springframework.cloud.client.ServiceInstance; //导入方法依赖的package包/类
@RequestMapping(value = "/", method = RequestMethod.GET)
public String printServiceB() {
ServiceInstance serviceInstance = discoveryClient.getLocalServiceInstance();
return serviceInstance.getServiceId() + " (" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + ")" + "===>Say " + msg;
}
示例9: printServiceA
import org.springframework.cloud.client.ServiceInstance; //导入方法依赖的package包/类
@RequestMapping(value = "/", method = RequestMethod.GET)
public String printServiceA() {
ServiceInstance serviceInstance = discoveryClient.getLocalServiceInstance();
return serviceInstance.getServiceId() + " (" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + ")" + "===>name:" + name + "<br/>" + serviceBClient.printServiceB();
}
示例10: getUrl
import org.springframework.cloud.client.ServiceInstance; //导入方法依赖的package包/类
protected String getUrl(String path) {
String url;
ServiceInstance instance = loadBalancerClient.choose(serviceName);
url = "http://" + instance.getHost() + ":" + instance.getPort() + "/api/" + path;
return url;
}