一. 前言
获取Eureka实例
|
1
2
3
4
|
public String serviceUrl() {
InstanceInfo instance = discoveryClient.getNextServerFromEureka("STORES", false);
return instance.getHomePageUrl();
}
|
步骤:
二. 导入包
pom.xml
|
1
2
3
4
5
6
7
8
|
<
dependency
>
<
groupId
>org.springframework.cloud</
groupId
>
<
artifactId
>spring-cloud-starter-eureka</
artifactId
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework.cloud</
groupId
>
<
artifactId
>spring-cloud-starter-hystrix</
artifactId
>
</
dependency
>
|
三. 修改启动Application
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@EnableCircuitBreaker
@EnableDiscoveryClient
public class HellloMain {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(HelloMain.class, args);
}
}
|
四. 业务使用
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
private int xxxx(String body) {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json");
headers.setContentType(type);
HttpEntity<
String
> formEntity = new HttpEntity<
String
>(body, headers);
String result = restTemplate.postForObject(getEurkaClient("hello-module"),
formEntity,
String.class);
return 0;
}
|
|
1
2
3
4
|
public String getEurkaClient(String end) {
InstanceInfo instance = discoveryClient.getNextServerFromEureka("smarthome-phihome", false);
return instance.getHomePageUrl() + end;
}
|
这样就可以在应用程序之间互相调用
本文转自rongwei84n 51CTO博客,原文链接:http://blog.51cto.com/483181/1954798,如需转载请自行联系原作者