-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathHelloServerApplication.java
More file actions
40 lines (35 loc) · 1.28 KB
/
HelloServerApplication.java
File metadata and controls
40 lines (35 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package demo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Spencer Gibb
*/
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class HelloServerApplication {
@Autowired
DiscoveryClient client;
@RequestMapping("/")
public String hello() {
StringBuilder result = new StringBuilder("Hello World: ");
List<String> services = client.getServices();
for(String serviceId: services) {
List<ServiceInstance> localInstances = client.getInstances(serviceId);
for(ServiceInstance instance: localInstances) {
result.append(instance.getServiceId()+":"+instance.getHost()+":"+instance.getPort());
}
}
return result.toString();
}
public static void main(String[] args) {
SpringApplication.run(HelloServerApplication.class, args);
}
}