尚学堂110天总结+zuul网关

2019-12-11  本文已影响0人  人间草木为伴

103天
行百里者半九十,想要在一个行业里成为顶尖人才,一定满足一万小时定律,要想学好JAVA,需要持之以恒不断地努力,每天都要勤思考+善于询问+解决问题!

知识温故而知新>>>>>>

SpringCloud微服务的流程分析和各组件的作用

image.png

今天学到的知识>>>>>>

网关解决了什么问题?

image

URL指定路由是什么规则?

URL 匹配关键字,如果包含关键字就跳转到指定的 URL 中

image

3.6修改配置文件配置URL路由规则。

采用服务名称指定路由方式

使用服务名称指定路由的规则是什么?


image

路由的排除方法

修改配置文件,将路由规则修改为添加前缀法。

image

过滤器类型讲解

ZuulFilter的作用是什么?


image

习题的情况总结>>>>>>

采用网关过滤器实现权限验证

需求:在网关过滤器中通过 Token 判断用户是否登录
4.2修改 pom 文件添 zuul 坐标

<?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.bjsxt</groupId> 
<artifactId>zuul-gateway-example</artifactId> 
<version>0.0.1-SNAPSHOT</version> 
<packaging>jar</packaging> 
<name>springcloud-eureka-consumer</name> 
<description>Demo project for Spring Boot</description> <parent> 
<groupId>org.springframework.boot</groupId> 
<artifactId>spring-boot-starter-parent</artifactId> <version>1.5.13.RELEASE</version> 
<relativePath /> <!-- lookup parent from repository --> </parent> 
<properties> 
<project.build.sourceEncoding>UTF-8</project.build.sourceEn coding> 
<project.reporting.outputEncoding>UTF-8</project.reporting. outputEncoding> 
<java.version>1.8</java.version> 
</properties> 
<dependencyManagement> 
<dependencies> 
<dependency> 
<groupId>org.springframework.cloud</groupId> 
<artifactId>spring-cloud-dependencies</artifactId> 
<version>Dalston.SR5</version> 
<type>pom</type> 
<scope>import</scope> 
</dependency> 
</dependencies> 
</dependencyManagement> 
<dependencies> 
<dependency> 
<groupId>org.springframework.boot</groupId> 
<artifactId>spring-boot-starter-web</artifactId> </dependency> 
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> 
<build> 
<plugins> 
<plugin> 
<groupId>org.springframework.boot</groupId> 
<artifactId>spring-boot-maven-plugin</artifactId> 
</plugin> 
</plugins> 
</build> 
</project>

4.3修改配置文件

spring.application.name=zuul-gateway-example 
server.port=9020 
#设置服务注册中心地址,指向另一个注册中心 
eureka.client.serviceUrl.defaultZone=http://user:123456@eur eka1:8761/eureka/,http://user:123456@eureka2:8761/eureka/ 

4.4创建 AccessFilter

/** 
* 登录权限验证 
* @author    Administrator 
* 
*/ 
@Component 
public    class    AccessFilter extends    ZuulFilter { 
private    static    final    Logger logger    = 
LoggerFactory.getLogger(AccessFilter.class); 
/**
* 过滤内容:在  run 方法编写过滤逻辑 
*/ 
@Override 
public    Object run() { 
//获取请求上下文 
RequestContext rc = 
RequestContext.getCurrentContext(); 
HttpServletRequest request = rc.getRequest(); 
logger.info("------------------------pre1------------------ -----------"); 
//获取表单中的  token 
String token = request.getParameter("token"); 
//对  token 做判断 
if(token == null){ 
logger.warn("token is null............"); 
rc.setSendZuulResponse(false);//代表请求结束。不在继 
续向下请求 
rc.setResponseStatusCode(401);//添加一个响应的状态码 rc.setResponseBody("{\"result\":\"token is 
null\"}");//响应内容 
rc.getResponse().setContentType("text/html;charset=utf-8"); //响应类型 
}else{ 
//访问  redis 服务    进行验证 
logger.info("token is OK"); 
} 
return    null; 
} 
/** 
* 是否开启过滤器:默认为  false 不开启 
*/ 
@Override 
public    boolean    shouldFilter() { 
// TODO    Auto-generated method stub 
return    true; 
} 
/** 
* 过滤器的执行顺序:通过整数表示顺序,数值越小,优先级越高 */ 
@Override
public    int    filterOrder() { 
// TODO    Auto-generated method stub 
return    0; 
} 
/** 
* 过滤器类型:通过过滤器类型决定了过滤器执行的时间 */ 
@Override 
public    String filterType() { 
return    "pre"; 
} 
}

zuul 性能调优:网关的 2 层超时调优
修改 pom 文件添加 zuul 依赖

<?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.bjsxt</groupId> 
<artifactId>zuul-gateway-timeout</artifactId> 
<version>0.0.1-SNAPSHOT</version> 
<packaging>jar</packaging> 
<name>springcloud-eureka-consumer</name> 
<description>Demo project for Spring Boot</description> <parent> 
<groupId>org.springframework.boot</groupId> 
<artifactId>spring-boot-starter-parent</artifactId> <version>1.5.13.RELEASE</version> 
<relativePath /> <!-- lookup parent from repository --> </parent> 
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEn coding> 
<project.reporting.outputEncoding>UTF-8</project.reporting. outputEncoding> 
<java.version>1.8</java.version> 
</properties> 
<dependencyManagement> 
<dependencies> 
<dependency> 
<groupId>org.springframework.cloud</groupId> 
<artifactId>spring-cloud-dependencies</artifactId> 
<version>Dalston.SR5</version> 
<type>pom</type> 
<scope>import</scope> 
</dependency> 
</dependencies> 
</dependencyManagement> 
<dependencies> 
<dependency> 
<groupId>org.springframework.boot</groupId> 
<artifactId>spring-boot-starter-web</artifactId> </dependency> 
<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> 
<build> 
<plugins> 
<plugin> 
<groupId>org.springframework.boot</groupId> 
<artifactId>spring-boot-maven-plugin</artifactId> 
</plugin> 
</plugins> 
</build> 
</project> 

修改配置文件

spring.application.name=zuul-gateway-timeout 
server.port=9020 
#设置服务注册中心地址,指向另一个注册中心 
eureka.client.serviceUrl.defaultZone=http://user:123456@eur eka1:8761/eureka/,http://user:123456@eureka2:8761/eureka/ 
#第一层  hystrix 超时时间设置 
#默认情况下是线程池隔离,超时时间  1000ms 
hystrix.command.default.execution.isolation.thread.timeoutI nMilliseconds=8000 
#第二层  ribbon超时时间设置:设置比第一层小 
# 请求连接的超时时间: 默认  5s 
ribbon.ConnectTimeout=5000 
# 请求处理的超时时间: 默认  5s 
ribbon.ReadTimeout=5000
上一篇下一篇

猜你喜欢

热点阅读