上文我们采用feign组件与微服务’ralab-nlp’进行通信,在实际应用中,feign组件通过集成Hystrix,从而实现熔断机制
改造微服务’ralab-client’
- Add the Maven spring-cloud-starter-netflix-hystrix denpendency
- Config hystrix info to bootstrap.yml file
- Add FeignClient fallback class by Implement EurekaFeignService Interface
我们在前面的文章中已经添加了spring-cloud-starter-netflix-hystrix依赖1
2
3
4<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
bootstrap.yml1
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
31eureka:
instance:
# ip-address: 127.0.0.1
prefer-ip-address: true # 补充
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8091
management:
endpoints:
web:
exposure:
include: hystrix.stream
spring:
application:
name: nlp-client
cloud:
config:
name: licensingservice
profile: default
label: master
discovery:
enabled: true
service-id: configserver
feign:
hystrix:
enabled: true
EurekaFeignService文件1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20package com.ralab.ralabclient.services;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
"nlp-service",fallback = EurekaFeignServiceFailure.class) (value =
public interface EurekaFeignService {
"nlp/api") (value =
String nlpService();
}
class EurekaFeignServiceFailure implements EurekaFeignService {
public String nlpService() {
return "不好意思,feingn调用失败";
}
}
NlpResource文件 注释@HystrixCommand注解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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56package com.ralab.ralabclient.resources;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.ralab.ralabclient.services.EurekaFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
"/nlp") (
public class NlpResource {
private RestTemplate restTemplate;
private EurekaFeignService eurekaFeignService;
"${example.property}") (
private String exampleProperty;
"/config") (
public String home() {
return "exampleProperty:" + exampleProperty;
}
"/service") (
"getFallbackIndex") (fallbackMethod =
public String index()
{
//String result = restTemplate.getForObject("http://localhost:8090/nlp/api", String.class);
String result = restTemplate.getForObject("http://nlp-service/nlp/api", String.class);
return result;
}
"/feign") (
//@HystrixCommand(fallbackMethod = "getFallbackIndex")
public String feignservice()
{
//String result = restTemplate.getForObject("http://localhost:8090/nlp/api", String.class);
String result = eurekaFeignService.nlpService();
return result;
}
private String getFallbackIndex()
{
//String result = restTemplate.getForObject("http://localhost:8090/nlp/api", String.class);
return "不好意思,没有获取到服务端信息";
}
}
这时停止微服务’ralab-nlp’,打开http://localhost:8091/nlp/feign