zuul超时重试简单使用
网关部分重要的依赖
org.springframework.retry spring-retry
@EnableRetry开启重试注解
#hystrix超时设置 hystrix.command.default.execution.isolation.thread.timeoutInMillisceconds=20000 ribbon.ConnectTimeout=5000 ribbon.ReadTimeout=5000 #重试次数 ribbon.MaxAutoRetries=1 #寻找一个服务重试 ribbon.MaxAutoRetriesNextServer=1 ribbon.OkToRetryOnAllOperations=true
调用http://127.0.0.1:1005/product-service/product/testFirst/777服务
这里的网关没有自定义超时,所以使用了默认配置
这里的超时可以如图设置
@RestController @RequestMapping("/product") @Slf4j public class ProductController { @GetMapping("/getProduct/{id}") public String selectProduct(@PathVariable String id){ log.info("【调用服务者入参】:{}",id); return "查询到的主键返回"+id; } @GetMapping("/testFirst/{id}") public String testFirst(@PathVariable Integer id){ log.info("【调用服务者入参】:{}",id); try { Thread.sleep(4000L); } catch (InterruptedException e) { e.printStackTrace(); } StringBuilder stringBuilder=new StringBuilder(); stringBuilder.append("qwe"); stringBuilder.append("-"); stringBuilder.append(id); return stringBuilder.toString(); } }
zuul网关配置超时设置后,请求的数据不是很快返回回来,会稍微等会
本次的测试中需要一个注册中心,一个zuul网关配置,一个服务提供者
父类pom
<?xml version="1.0" encoding="UTF-8"?>4.0.0 org.example zuul-aprent pom 1.0-SNAPSHOT eureka-service prodoct-service order-service zuul-service zuul-one zuul-two zuul-three org.springframework.boot spring-boot-starter-parent 2.2.5.RELEASE org.springframework.cloud spring-cloud-dependencies Hoxton.SR3 pom import com.alibaba.cloud spring-cloud-alibaba-dependencies 2.1.0.RELEASE pom import org.apache.maven.plugins maven-compiler-plugin 1.8 1.8 utf-8 org.springframework.boot spring-boot-maven-plugin
eureka完整项目
<?xml version="1.0" encoding="UTF-8"?>server.port=8761 spring.application.name=eureka-service eureka.instance.hostname=localhost eureka.client.service-url.defaultZone=http://localhost:8761/eureka eureka.client.register-with-eureka=false eureka.client.fetch-registry=false logging.level.com.eureka=info logging.level.web=info spring.devtools.add-properties=false package com.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * @author yourheart * @Description * @create 2022-04-20 21:17 */ @SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class,args); } } zuul-aprent org.example 1.0-SNAPSHOT 4.0.0 com.eureka eureka-service org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-maven-plugin eureka
zuul网关完整项目
<?xml version="1.0" encoding="UTF-8"?>server.port=1005 spring.application.name=zuul-three #注册到eureka注册中心,如果是注册到集群就用逗号连接多个,单实例写上一个就好 eureka.client.service-url.defaultZone=http://localhost:8761/eureka logging.level.com.zuul=debug logging.level.web=debug spring.devtools.add-properties=false #hystrix超时设置 hystrix.command.default.execution.isolation.thread.timeoutInMillisceconds=20000 ribbon.ConnectTimeout=5000 ribbon.ReadTimeout=5000 #重试次数 ribbon.MaxAutoRetries=1 #寻找一个服务重试 ribbon.MaxAutoRetriesNextServer=1 ribbon.OkToRetryOnAllOperations=true package com.zuul.failback; import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.client.ClientHttpResponse; import org.springframework.stereotype.Component; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; /** * @author yourheart * @Description * @create 2022-05-26 23:36 */ @Component public class ProductProviderFallback implements FallbackProvider { @Override public String getRoute() { return "order-service"; } @Override public ClientHttpResponse fallbackResponse(String route, Throwable cause) { ClientHttpResponse response = new ClientHttpResponse() { @Override public HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); MediaType mediaType = new MediaType("application", "json", Charset.forName("utf-8")); headers.setContentType(mediaType); return headers; } @Override public InputStream getBody() throws IOException { ByteArrayInputStream inputStream = new ByteArrayInputStream("订单服务不可用".getBytes()); return inputStream; } @Override public HttpStatus getStatusCode() throws IOException { return HttpStatus.INTERNAL_SERVER_ERROR; } @Override public int getRawStatusCode() throws IOException { return this.getStatusCode().value(); } @Override public String getStatusText() throws IOException { return this.getStatusCode().getReasonPhrase(); } @Override public void close() { } }; return response; } } package com.zuul; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.retry.annotation.EnableRetry; /** * @author yourheart * @Description * @create 2022-05-26 23:15 */ @SpringBootApplication //开启zuul注解 @EnableZuulProxy //开启重试注解 @EnableRetry public class ZuulThreeApplication { public static void main(String[] args) { SpringApplication.run(ZuulThreeApplication.class,args); } } zuul-aprent org.example 1.0-SNAPSHOT 4.0.0 com.zuul zuul-three org.springframework.cloud spring-cloud-starter-netflix-zuul org.projectlombok lombok 1.18.16 org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-netflix-hystrix org.springframework.retry spring-retry
商品服务完整项目
<?xml version="1.0" encoding="UTF-8"?>zuul-aprent org.example 1.0-SNAPSHOT 4.0.0 com.product prodoct-service org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web com.alibaba fastjson 1.2.7 commons-lang commons-lang 2.6 org.projectlombok lombok 1.18.16 org.springframework.boot spring-boot-maven-plugin product
server.port=1000 spring.application.name=product-service #注册到eureka注册中心,如果是注册到集群就用逗号连接多个,单实例写上一个就好 eureka.client.service-url.defaultZone=http://localhost:8761/eureka logging.level.com.product=debug logging.level.web=debug spring.devtools.add-properties=false
package com.product.controller.front; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author yourheart * @Description * @create 2022-04-20 21:30 */ @RestController @RequestMapping("/product") @Slf4j public class ProductController { @GetMapping("/getProduct/{id}") public String selectProduct(@PathVariable String id){ log.info("【调用服务者入参】:{}",id); return "查询到的主键返回"+id; } @GetMapping("/testFirst/{id}") public String testFirst(@PathVariable Integer id){ log.info("【调用服务者入参】:{}",id); try { Thread.sleep(4000L); } catch (InterruptedException e) { e.printStackTrace(); } StringBuilder stringBuilder=new StringBuilder(); stringBuilder.append("qwe"); stringBuilder.append("-"); stringBuilder.append(id); return stringBuilder.toString(); } }
package com.product; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /** * @author yourheart * @Description * @create 2022-04-20 21:28 */ @SpringBootApplication @EnableDiscoveryClient public class ProductApplication { public static void main(String[] args) { SpringApplication.run(ProductApplication.class,args); } }
而当有第三方服务调用商品服务会出现超时问题
这个因为订单服务默认的超时不够用了
需要在订单服务中配置如下参数
server.port=1001 spring.application.name=order-service #注册到eureka注册中心,如果是注册到集群就用逗号连接多个,单实例写上一个就好 eureka.client.service-url.defaultZone=http://localhost:8761/eureka logging.level.com.order=debug logging.level.web=debug spring.devtools.add-properties=false hystrix.command.default.execution.isolation.thread.timeoutInMillisceconds=20000 ribbon.ConnectTimeout=5000 ribbon.ReadTimeout=5000 ribbon.MaxAutoRetries=1 ribbon.MaxAutoRetriesNextServer=1 ribbon.OkToRetryOnAllOperations=true
就是自定义设置后,默认的超时时间变长了