日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Zuul的回退

發(fā)布時(shí)間:2024/4/13 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Zuul的回退 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
下面我們來看一下Zuul的回退,默認(rèn)情況下,經(jīng)過Zuul的請求,他都會使用Hystrix進(jìn)行包裹,所以Zuul本身就有斷路器的功能,我們在聊Zuul的fallback之前呢,一起來做一個(gè)實(shí)驗(yàn),啟動(dòng)Eureka,啟動(dòng)用戶微服務(wù),啟動(dòng)zuul10.40.8.152:8761訪問zuul的routeslocalhost:8040/routeslocalhost:8040/microservice-simple-provider-user/simple/1經(jīng)過zuul的請求都會通過Hystrix包裹,那我們是否可以訪問hystrix.stream端點(diǎn)呢localhost:8040/hystrix.stream我們把dashboard啟一下localhost:8030/hystrix

我們發(fā)現(xiàn)Circuit closed,我們把用戶微服務(wù)停掉,然后拼命的刷http://localhost:8040/microservice-simple-provider-user/simple/1microservice-gateway-zuul-fallback@Component public class MyFallbackProvider implements ZuulFallbackProvider {@Overridepublic String getRoute() {//route 如return "microservice-simple-provider-user";}@Overridepublic ClientHttpResponse fallbackResponse() {return new ClientHttpResponse() {@Overridepublic HttpStatus getStatusCode() throws IOException {return HttpStatus.OK;}@Overridepublic int getRawStatusCode() throws IOException {return 200;}@Overridepublic String getStatusText() throws IOException {return "OK";}@Overridepublic void close() {}@Overridepublic InputStream getBody() throws IOException {return new ByteArrayInputStream(("fallback: ===========>: "+MyFallbackProvider.this.getRoute()).getBytes());}@Overridepublic HttpHeaders getHeaders() {HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);return headers;}};} }localhost:8040/routeslocalhost:8040/microservice-simple-provider-user/simple/1它會返回一個(gè)fallback給我fallback: ===========>: microservice-simple-provider-userfallback其實(shí)就是這里的@Override public InputStream getBody() throws IOException {return new ByteArrayInputStream(("fallback: ===========>: "+MyFallbackProvider.this.getRoute()).getBytes()); }首先是要返回HTTP的狀態(tài)碼,你不一定是返回OK,OK是200,你可能會返回其他的狀態(tài)碼,500,BAD_REQUEST,BAD_REQUEST.value(),HttpStatus他只一個(gè)枚舉類型org.springframework.http.HttpStatus/*** Java 5 enumeration of HTTP status codes.** <p>The HTTP status code series can be retrieved via {@link #series()}.** @author Arjen Poutsma* @author Sebastien Deleuze* @see HttpStatus.Series* @see <a href="http://www.iana.org/assignments/http-status-codes">HTTP Status Code Registry</a>* @see <a href="http://en.wikipedia.org/wiki/List_of_HTTP_status_codes">List of HTTP status codes - Wikipedia</a>*/ public enum HttpStatus {構(gòu)造方法是他private HttpStatus(int value, String reasonPhrase) {this.value = value;this.reasonPhrase = reasonPhrase; }value這個(gè)就是name,name是int值的,還有reasonPhrase,我們知道Zuul它會度Eurka里面的數(shù)據(jù),然后看他要代理哪些微服務(wù),那現(xiàn)在Eureka里面已經(jīng)沒有用戶微服務(wù)了,所以這邊會截一個(gè)404,但是你的routes里面沒有了localhost:8040/routeslocalhost:8040/hystrix.streamZuul fallback是一個(gè)微服務(wù),這力度是不一樣的,可以簡單的進(jìn)行一個(gè)對比,但是其實(shí)這個(gè)價(jià)值也不大,維度是什么樣子的,知道不知道又怎么樣呢 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn.cloud</groupId><artifactId>microservice-gateway-zuul-fallback</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>cn.learn</groupId><artifactId>microcloud02</artifactId><version>0.0.1</version></parent><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zuul</artifactId></dependency></dependencies><!-- 這個(gè)插件,可以將應(yīng)用打包成一個(gè)可執(zhí)行的jar包 --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project> server.port=8040 spring.application.name=microservice-gateway-zuul-fallback eureka.instance.prefer-ip-address=true eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}} eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eureka eureka.instance.appname=microservice-gateway-zuul-reg-exp #zuul.prefix=/api #zuul.routes.user-route.stripPrefix=false #zuul.prefix=/simple #zuul.stripPrefix=true logging.level.com.learn=trace logging.file=springboot.log logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n logging.pattern.file=%d{yyyy-MM-dd} ==== [%thread] %-5level ==== %logger{50} ==== %msg%nmanagement.security.enabled=falsehystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=60000 ribbon.ConnectTimeout=3000 ribbon.ReadTimeout=6000 package com.learn.cloud.fallback;import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream;import org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider; 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;@Component public class MyFallbackProvider implements ZuulFallbackProvider {@Overridepublic String getRoute() {//route 如return "microservice-simple-provider-user";}@Overridepublic ClientHttpResponse fallbackResponse() {return new ClientHttpResponse() {@Overridepublic HttpStatus getStatusCode() throws IOException {return HttpStatus.OK;}@Overridepublic int getRawStatusCode() throws IOException {return 200;}@Overridepublic String getStatusText() throws IOException {return "OK";}@Overridepublic void close() {}@Overridepublic InputStream getBody() throws IOException {return new ByteArrayInputStream(("fallback: ===========>: "+MyFallbackProvider.this.getRoute()).getBytes());}@Overridepublic HttpHeaders getHeaders() {HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);return headers;}};} } package com.learn.cloud;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy;@EnableZuulProxy @SpringBootApplication public class ZuulFallbackApplication {public static void main(String[] args) {SpringApplication.run(ZuulFallbackApplication.class, args);} }

?

總結(jié)

以上是生活随笔為你收集整理的Zuul的回退的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。