Gateway added fuse mechanism
Join fuse mechanism in the gateway
Add Dependency
spring-cloud-gateway project POM file to the spring-cloud-starter-netflix-hystrix
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
Modify the configuration file
Application.yml modify configuration files
server:
port: 9000
spring:
cloud:
consul:
host: 127.0.0.1
port: 8500
discovery:
register: true
gateway:
routes:
- id: test_route
uri: lb://service-provider
predicates:
- Path=/service-provider/{segment}
filters:
- SetPath=/{segment}
- name: Hystrix
args:
name: service-provider-fallback
fallbackUri: forward:/service-provider-error
- name: Retry
args:
retries: 3
statuses: BAD_GATEWAY,BAD_REQUEST
default-filters:
- name: Hystrix
args:
name: fallbackcmd
fallbackUri: forward:/default-error
application:
name: PC-ApiGateWay
Join fuse mechanism in the default filter
default-filters:
- name: Hystrix
args:
name: fallbackcmd
fallbackUri: forward:/default-error
default-filters the gateway on behalf of the default filter, Hystrix achieve fusing mechanism, fallbackcmd is the name (name attribute) HystrixCommand object, fallbackUri represents a jump to the fusing mechanism triggering request url, / default-error is spring-cloud error message -gateway project implemented unified treatment Controller:
@RestController
public class ErrorHandle {
@RequestMapping("/default-error")
public String DefaultErrorHandle(){
return "这是通用错误处理返回的信息。";
}
}
Custom single road fuse mechanism to deal with the content of
gateway:
routes:
- id: test_route
uri: lb://service-provider
predicates:
- Path=/service-provider/{segment}
filters:
- SetPath=/{segment}
- name: Hystrix
args:
name: service-provider-fallback
fallbackUri: forward:/service-provider-error
The above description and the same content, the same need spring-cloud-gateway program implemented service-provider-error process.
@RestController
public class ErrorHandle {
@RequestMapping("/default-error")
public String DefaultErrorHandle(){
return "这是通用错误处理返回的信息。";
}
@RequestMapping("/service-provider-error")
public String ServiceProviderErrorHandle(){
return "这是ServiceProvider服务专属的错误处理信息。";
}
}
Automatic retry mechanism
gateway:
routes:
- id: test_route
uri: lb://service-provider
predicates:
- Path=/service-provider/{segment}
filters:
- SetPath=/{segment}
- name: Hystrix
args:
name: service-provider-fallback
fallbackUri: forward:/service-provider-error
- name: Retry
args:
retries: 3
statuses: BAD_GATEWAY,BAD_REQUEST
Statement under the filters name for the gateway Retry filter, the number of retries retries, why statuses returns an HTTP status code and try again (and also methods series Parameter) value, refer to org.springframework.http.HttpStatus, org.springframework .http.HttpMethod and org.springframework.http.HttpStatus.Series.
Start test project
Start Consul service centers and micro spring-cloud-provider service, started last spring-cloud-gateway project, under normal circumstances:
After closing the spring-cloud-provider micro-service process to refresh the page again:
Source
Github repository: https: //github.com/sunweisheng/spring-cloud-example