(十六)在线教育网站搭建网关

网关 Zuul

1. 介绍

后端不直接开放调用端,通过一个API网关根据请求url,路由到相应的服务。

API Getaway是介于客户端和服务端之间的中间层。处理非业务功能,提供路由请求,监控和限流等,将请求均衡分发给后台服务器

2. Zuul - Spring Cloud Zuul

2.1. Maven依赖

1
2
3
4
5
<!-- 网关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>

2.2. 主配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.online.edu.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
//开启网关代理
@EnableZuulProxy
public class GatewayApplication {

public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class,args);
}
}

2.3. applcation.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server.port=9001

spring.application.name=online-gateway

#路径中包含eduservice -> 8001
zuul.routes.edu-service.path=/eduservice/**
zuul.routes.edu-service.url=http://localhost:8001/eduservice/

#路径中包含videoservice -> 8002
zuul.routes.video-service.path=/videoservice/**
zuul.routes.video-service.url=http://localhost:8002/videoservice/

#路径中包含statisticsservice -> 8003
zuul.routes.statistics-service.path=/statisticsservice/**
zuul.routes.statistics-service.url=http://localhost:8003/statisticsservice/

#路径中包含memberservice -> 8004
zuul.routes.member-service.path=/memberservice/**
zuul.routes.member-service.url=http://localhost:8004/memberservice/

2.4. 测试代理

http://localhost:9001/memberservice/front/ucenter-member/existPhone/15167110103

会员8004端号中的路由

3. 集群实现-将网关服务化-eureka

3.1. 将网关注册到eureka中

1
2
3
4
5
<!--注册eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

3.2. 连接eureka

1
2
3
4
5
#eureka 注册
#eureka 服务器中获取的服务器的ip地址是否为主机名
eureka.instance.prefer-ip-address=true
#指定注册中心地址
eureka.client.service-url.defaultZone=http://127.0.0.1:8010/eureka/

3.3. 注解@EnableEurekaClient

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.online.edu.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
//开启网关代理
@EnableZuulProxy
@EnableEurekaClient
public class GatewayApplication {

public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class,args);
}
}

这样网关可以通过eureka中的项目注册名进行调用

http://localhost:9001/online-edu/eduservice/edu-teacher/listTeachers

9001 - 网关端口

online-edu 注册端口为8001的项目

eduservice/edu-teacher/listTeachers 路由

3.4. 关闭某个服务

1
2
#关闭服务
zuul.ignored-services=online-teacher

3.5. 关闭某些路由

1
2
#禁止某些路由
zuul.ignored-patterns=/**/admin/**

3.6. 路由别名

1
2
#别名
#zuul.routes.edu-teacher=/edu-api/**

3.7. 网关默认不传递敏感信息

网关不会将cookie信息向下传递

cookie作为session开启的钥匙

1
2
#还原网关过滤的请求头
zuul.sensitive-headers=

源码:

1
private Set<String> sensitiveHeaders = new LinkedHashSet(Arrays.asList("Cookie", "Set-Cookie", "Authorization"));

4. Filter案例-只有拥有token的才能得到播放凭证

http://localhost:9001/online-video/videoservice/front/video/getVideoInfo/f2d0eeb22fb24b349c3a3dd26e2b5ddf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.online.edu.gateway.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.http.HttpStatus;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

@Component
public class LoginFilter extends ZuulFilter {

//过滤器类型 pre routing post error
@Override
public String filterType() {
return FilterConstants.PRE_TYPE;
}

//过滤器执行顺序 值越小越先执行
@Override
public int filterOrder() {
return 0;
}

//决定是否执行run方法
// false过滤器放行,不执行run方法
// true过滤器不放行,执行run方法
@Override
public boolean shouldFilter() {

// /videoservice/front/video/getVideoInfo/{videoId}
//获取请求路径 uri
RequestContext context = RequestContext.getCurrentContext();
HttpServletRequest request = context.getRequest();
String uri =request.getRequestURI();

String needUri = "front/video/getVideoInfo";
if (uri.contains(needUri)&&!StringUtils.isEmpty(uri)){
return true;
}
return false;
}

//过滤器具体步骤
@Override
public Object run() throws ZuulException {

//System.out.println("test");

RequestContext context = RequestContext.getCurrentContext();
HttpServletRequest request = context.getRequest();

String token = request.getParameter("token");
if(StringUtils.isEmpty(token)){

//不向后执行
context.setSendZuulResponse(false);
//响应状态码
context.setResponseStatusCode(HttpStatus.SC_FORBIDDEN);
}

return null;
}
}

http://easybug.org/

本文结束  感谢您的阅读