博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springcloud15---zuul-fallback
阅读量:5056 次
发布时间:2019-06-12

本文共 3149 字,大约阅读时间需要 10 分钟。

package com.itmuch.cloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.zuul.EnableZuulProxy;@SpringBootApplication@EnableZuulProxypublic class ZuulApplication {  public static void main(String[] args) {    SpringApplication.run(ZuulApplication.class, args);  }}
package com.itmuch.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;//通过zuul(8040端口)访问user微服务http://localhost:8040/microservice-provider-user/simple/1//如果此时user微服务停掉,就会返回"fallbackmicroservice-provider-user",(使用的是hysitrcs的断路器功能)//feign的Fallback是针对一个类,zuul的Fallback是针对一个微服务。@Componentpublic class MyFallbackProvider implements ZuulFallbackProvider {  @Override  public String getRoute() {    return "microservice-provider-user";  }  @Override  public ClientHttpResponse fallbackResponse() {    return new ClientHttpResponse() {      @Override      public HttpStatus getStatusCode() throws IOException {        return HttpStatus.BAD_REQUEST;      }      @Override      public int getRawStatusCode() throws IOException {        return HttpStatus.BAD_REQUEST.value();      }      @Override      public String getStatusText() throws IOException {        return HttpStatus.BAD_REQUEST.getReasonPhrase();      }      @Override      public void close() {      }      @Override      public InputStream getBody() throws IOException {        return new ByteArrayInputStream(("fallback" + MyFallbackProvider.this.getRoute()).getBytes());      }      @Override      public HttpHeaders getHeaders() {        HttpHeaders headers = new HttpHeaders();        headers.setContentType(MediaType.APPLICATION_JSON);        return headers;      }    };  }}
spring:  application:    name: microservice-gateway-zuulserver:  port: 8040eureka:  client:    service-url:      defaultZone: http://user:password123@localhost:8761/eureka  instance:    prefer-ip-address: truehystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000ribbon:  ConnectTimeout: 3000  ReadTimeout: 60000
4.0.0
com.itmuch.cloud
microservice-spring-cloud
0.0.1-SNAPSHOT
microservice-gateway-zuul-fallback
jar
UTF-8
org.springframework.cloud
spring-cloud-starter-zuul
org.springframework.cloud
spring-cloud-starter-eureka

 

转载于:https://www.cnblogs.com/yaowen/p/9159472.html

你可能感兴趣的文章
iOS-字符串的连接
查看>>
(leetcode题解)Max Consecutive Ones
查看>>
LOJ#10004. 「一本通 1.1 例 5」智力大冲浪
查看>>
JS的函数节流(throttle)
查看>>
webpack简介
查看>>
[iOS]利用Appicon and Launchimage Maker生成并配置iOSApp的图标和启动页
查看>>
C#正则删除HTML标签
查看>>
1、My Scripts
查看>>
springmvc
查看>>
U盘安装Debian KDE 输入法 Manjaro Linux WPS 字体
查看>>
用户反馈:对 Rafy 开发框架的一些个人建议
查看>>
_DataStructure_C_Impl:二叉排序树的查找
查看>>
Codeforces Round #544 (Div. 3) D F1 F2
查看>>
随机数发生器
查看>>
glusterfs分布式复制扩容卷以及平衡卷
查看>>
Word 2013 多级自动编号设置
查看>>
case class inheritance
查看>>
PHP json不转义
查看>>
6.13-C3p0连接池配置,DBUtils使用
查看>>
【计算机视觉】Selective Search for Object Recognition论文阅读3
查看>>