spring-cloud开发Greenwich版:服务调用feign集成Hystrix(八)

上文我们采用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.yml

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
eureka:
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
20
package com.ralab.ralabclient.services;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(value = "nlp-service",fallback = EurekaFeignServiceFailure.class)
public interface EurekaFeignService {

@RequestMapping(value = "nlp/api")
String nlpService();
}

@Component
class EurekaFeignServiceFailure implements EurekaFeignService {
@Override
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
56
package 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;

@RestController
@RequestMapping("/nlp")
@RefreshScope
public class NlpResource {

@Autowired
private RestTemplate restTemplate;

@Autowired
private EurekaFeignService eurekaFeignService;

@Value("${example.property}")
private String exampleProperty;

@RequestMapping("/config")
public String home() {
return "exampleProperty:" + exampleProperty;
}

@GetMapping("/service")
@HystrixCommand(fallbackMethod = "getFallbackIndex")
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;
}

@GetMapping("/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

坚持原创技术分享,您的支持是我前进的动力!