标签:return etl ngnix 情况下 set 原理 img tab 能力
Zuul 作为路由网关组件,在微服务架构中有着非常重要的作用:
请求的生命周期图:
pom文件:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
spring.application.name=feign server.port=8087 eureka.client.service-url.defaultZone=http://localhost:8762/eureka/ #开启Hystrix的功能 feign.hystrix.enabled=true
在下方配置文件的使用:
spring.application.name=hystric server.port=8088 eureka.client.service-url.defaultZone=http://localhost:8762/eureka/ #开启Hystrix的功能 feign.hystrix.enabled=true
在下方配置文件的使用:
新建工程的配置类:
@EnableZuulProxy @EnableEurekaClient @SpringBootApplication public class EurekaZuulClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaZuulClientApplication.class, args); } }
eureka: client: service-url: defaultZone: http://localhost:8762/eureka/ server: port: 5000 spring: application: name: zuul zuul: routes: #自定义的 ribbonapi: #路径 path: /ribbonapi/** #引入的application(上述需要使用到的工程名) serviceid: hystric feignapi: path: /feignapi/** serviceid: feign
zuul: routes: ribbonapi: path: /ribbonapi/** serviceid: hystric url: http://localhost:8089
package com.cr.eurekazuulclient.zull; import org.slf4j.LoggerFactory; 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.util.logging.Logger; @Component public class MyFallbackProvider implements FallbackProvider { @Override public String getRoute() { // 表明是为哪个微服务提供回退,*表示为所有微服务提供回退 return "hystric"; } public ClientHttpResponse fallbackResponse(){ return new ClientHttpResponse() { @Override public HttpStatus getStatusCode() throws IOException { return HttpStatus.OK; } @Override public int getRawStatusCode() throws IOException { return 200; } @Override public String getStatusText() throws IOException { return "OK"; } @Override public void close() { } @Override public InputStream getBody() throws IOException { return new ByteArrayInputStream("The service is unavailable.".getBytes()); } @Override public HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); return headers; } }; } @Override public ClientHttpResponse fallbackResponse(String route, Throwable cause) { if (cause != null && cause.getCause() != null){ String reason = cause.getCause().getMessage(); System.out.println("exception:" + reason); } return fallbackResponse(); } }
重启hystrix服务:
过滤器的类型:
package com.cr.eurekazuulclient.zull; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.exception.ZuulException; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import java.util.logging.Logger; @Component public class MyZuulFilter extends ZuulFilter { //private static Logger log= (Logger) LoggerFactory.getLogger(MyZuulFilter.class); @Override public String filterType() { return "pre"; } @Override public int filterOrder() { return 1; } @Override public boolean shouldFilter() { return true; } @Override public Object run() throws ZuulException { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); Object accessToken = request.getParameter("token"); if (accessToken == null){ System.out.println("token is empty"); //log.warning("token is empty"); ctx.setSendZuulResponse(false); ctx.setResponseStatusCode(401); try { ctx.getResponse().getWriter().write("token is empty"); }catch (Exception e){ return null; } } return null; } }
此时请求:
此时加上参数token:
7、Spring -Cloud-路由网管Spring Cloud Zuul
标签:return etl ngnix 情况下 set 原理 img tab 能力
原文地址:https://www.cnblogs.com/Mrchengs/p/10654069.html