5_API网关服务
前言
Github:https://github.com/HealerJean
1、网关服务_4001
1.1、pom依赖
<!--zuul网关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<!--eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.healerjean.proj</groupId>
<artifactId>hlj-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>hlj-api-gateway-4001</artifactId>
<version>${project.healerjean.version}</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--zuul网关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<!--eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.2、启动类
@EnableZuulProxy //开启zuul 的API网关服务功能
@SpringCloudApplication
public class ApiGateway_4001_Application {
public static void main(String[] args) {
SpringApplication.run(ApiGateway_4001_Application.class, args);
}
}
1.3、传统路由
1.3.1、application.properties
配置
spring.application.name=hlj-api-gateway
server.port=4001
# 传统的路由
# 也就是说当我们访问 http://localhost:4001/zuulProvider/api/provider/connect
# 会转发到 http://localhost:2001/api/provider/connect 提供的微服务接口上,
zuul.routes.provider.path=/zuulProvider/**
zuul.routes.provider.serviceId=http://localhost:2001/
zuul.routes.consumer.path=/zuulConsumer/**
zuul.routes.consumer.url=http://localhost:3001/
1.3.2、启动访问
http://127.0.0.1:4001/zuulProvider/api/provider/connect
接口返回:
{
"host": "localhost",
"port": 2001,
"uri": "http://localhost:2001",
"metadata": {},
"serviceId": "hlj-server-provider",
"secure": false
}
1.4、面向服务的路由
1.4.1、配置文件
spring.application.name=hlj-api-gateway
server.port=4001
## 1、传统的路由
## 浏览器访问 http://localhost:4001/zuulProvider/api/provider/connect
## 实际上访问的是 http://localhost:2001/api/provider/connect 提供的微服务接口上,
#zuul.routes.provider.path=/zuulProvider/**
#zuul.routes.provider.url=http://localhost:2001/
#zuul.routes.consumer.path=/zuulConsumer/**
#zuul.routes.consumer.url=http://localhost:3001/
# 2、面向服务的路由
# 浏览器访问 : http://127.0.0.1:4001/zuulConsumer/api/consumer/feign/invokeInter
# 实际上访问的是 http://{HLJ-SERVER-CONSUMER}/api/consumer/feign/invokeInter
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/,http://localhost:1112/eureka/
zuul.routes.provider.path=/zuulProvider/**
zuul.routes.provider.serviceId=HLJ-SERVER-PROVIDER
zuul.routes.consumer.path=/zuulConsumer/**
zuul.routes.consumer.serviceId=HLJ-SERVER-CONSUMER
1.4.2、启动访问
http://127.0.0.1:4001/zuulProvider/api/provider/connect
接口返回:
{
"host": "localhost",
"port": 2001,
"uri": "http://localhost:2001",
"metadata": {},
"serviceId": "hlj-server-provider",
"secure": false
}
2、网关Zuul
过滤器
实现了请求路由功能以后,我们的微服务应用提供的接口就可以通过统一的API网关入口被客户访问到了。但是每个客户请求微服务时,他们的访问权限一般与一定的限制,系统不会对所有的功能都开放。所以这里需要一个zuul过滤器
2.1、配置过滤器
2.1.1、TokenFilter
package com.healerjean.config.filter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;
/**
* @author HealerJean
* @ClassName TokenFilter
* @date 2020/4/14 19:40.
* @Description
*/
@Slf4j
public class TokenFilter extends ZuulFilter {
/**
* 1、过滤器类型 这里定义为pre 意味着在请求路由之前执行
*/
@Override
public String filterType() {
return "pre";
}
/**
* 2、过滤器执行顺序,当请求中存在多个过滤器时,需要根据这个返回值来依次执行
*
* @return
*/
@Override
public int filterOrder() {
return 0;
}
/**
* 3、判断过滤器是否需要被执行,这里返回,true 因此这个过滤器对所有的请求都会生效,
* 实际操作中可以利用这里指定过滤器的有效范围
*/
@Override
public boolean shouldFilter() {
return true;
}
/**
* 4、过滤器的具体逻辑
*
* @return
*/
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
log.info("send {} request to {}", request.getMethod(), request.getRequestURL().toString());
Object accessToken = request.getParameter("token");
if (accessToken == null) {
log.warn("access token is empty");
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
ctx.setResponseBody("Authorization token is empty");
return null;
}
log.info("access token ok");
return null;
}
}
2.1.2、FilterConfig
配置过滤器
@Configuration
public class FilterConfig {
@Bean
public TokenFilter accessFilter() {
return new TokenFilter();
}
}
2.2、启动测试
2.2.1、拦截访问
http://127.0.0.1:4001/zuulProvider/api/provider/feign/reequestParam
浏览器:
Authorization token is empty
2.2.2、token访问
http://127.0.0.1:4001/zuulProvider/api/provider/feign/reequestParam?name=666&token=1
接口返回:
声明式服务调用Controller--------reequestParam 参数接收--------成功 :666