网关 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
zuul.routes.edu-service.path=/eduservice/** zuul.routes.edu-service.url=http://localhost:8001/eduservice/
zuul.routes.video-service.path=/videoservice/** zuul.routes.video-service.url=http://localhost:8002/videoservice/
zuul.routes.statistics-service.path=/statisticsservice/** zuul.routes.statistics-service.url=http://localhost:8003/statisticsservice/
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
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
|
3.2. 连接eureka
1 2 3 4 5
|
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. 路由别名
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 {
@Override public String filterType() { return FilterConstants.PRE_TYPE; }
@Override public int filterOrder() { return 0; }
@Override public boolean shouldFilter() {
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 {
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/