the circuit breaker mechanism is enabled in zuul, and it takes a long time to set it up. After the backend returns successfully, zuul enters the fallback,. Excuse me, what"s going on?
the contents of the appliaction.yaml file are as follows
spring:
application:
name: b2b-zuul
server:
port: 8086
tomcat:
basedir: .
zuul:
sensitive-headers: Access-Control-Allow-Origin
ignored-headers: Access-Control-Allow-Origin,H-APP-Id,APPToken
routes:
b2b-order:
path: /order/**
serviceId: b2b-order
eureka:
instance:
hostname: ${spring.cloud.client.ipAddress}
lease-renewal-interval-in-seconds: 3
lease-expiration-duration-in-seconds: 9
client:
serviceUrl:
defaultZone: http://192.168.60.207:9095/eureka/
-sharphystrix
hystrix:
command:
serviceId:
execution:
isolation:
thread:
timeoutInMilliseconds: 200000
-sharpribbon
ribbon:
ReadTimeout: 200000
ConnectTimeout: 200000
to put it simply, the process is as follows: the front-end access gateway is forwarded to the interface, and then the interface sleep5 seconds. At this time, the front-end page is still waiting for a response
, and then the interface hibernation ends and returns successfully, but the gateway enters the fallback. Finally, the error response data set by fallback is returned to the front end
this is my zuulfallback:
@Component
@Slf4j
public class ZuulFallback implements FallbackProvider {
@Override
public String getRoute() {
return "*";//apiidreturn "*"return null
}
@Override
public ClientHttpResponse fallbackResponse() {
return null;
}
@Override
public ClientHttpResponse fallbackResponse(Throwable cause) {
return new ClientHttpResponse() {
@Override
public HttpStatus getStatusCode(){
return HttpStatus.OK; //ok
}
@Override
public int getRawStatusCode(){
return HttpStatus.OK.value();
}
@Override
public String getStatusText(){
return HttpStatus.OK.getReasonPhrase();
}
@Override
public void close() {
}
@Override
public InputStream getBody() throws IOException {
log.error(cause.getMessage());
JSONObject json = new JSONObject();
json.put("code", "501");
json.put("message", "");
return new ByteArrayInputStream(json.toJSONString().getBytes("UTF-8")); //
}
@Override
public HttpHeaders getHeaders() {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); //
return httpHeaders;
}
};
}
}
zuul turned on circuit breaker monitoring
paste startup class code:
@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
@EnableHystrixDashboard
@EnableFeignClients(basePackageClasses = {MerchantSc.class})
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
then pom file:
<name>zuul</name>
<description>Demo zuul</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
<swagger.version>2.8.0</swagger.version>
<swagger-anno.version>1.5.14</swagger-anno.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!-- hystrix dashboard -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger-anno.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
ask the bosses, what"s going on? ask for an answer!