文章目錄
-
-
- Zuul路由網關簡介及基本使用
-
- 路由配置
- Zuul路由映射配置
- Zuul請求過濾配置
-
Zuul路由網關簡介及基本使用
簡介
Zuul API路由網關服務簡介
在圖中這裡的API 路由網關服務 由Zuul實作,主要就是對外提供服務接口的時候,起到了請求的路由和過濾作用,也是以能夠隐藏内部服務的接口細節,從來有利于保護系統的安全性;
路由配置
我們修改下Hosts,專門為zuul搞個本地域名映射
hosts檔案 加下:
127.0.0.1 zuul.swx.com
我們建立一個module microservice-zuul-3001
pom依賴:
<?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.swx</groupId>
<artifactId>microservice</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>microservice-zuul-3001</artifactId>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.swx</groupId>
<artifactId>microservice-common</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- actuator監控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- hystrix容錯 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--zuul網關-->
<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>
application.yml
server:
port: 3001
context-path: /
spring:
application:
name: microservice-zuul
eureka:
instance:
instance-id: microservice-zuul:3001
prefer-ip-address: true
client:
service-url:
defaultZone: http://eureka2001.swx.com:2001/eureka/,http://eureka2002.swx.com:2002/eureka/,http://eureka2003.swx.com:2003/eureka/
info:
groupId: com.swx.testSpringcloud
artifactId: microservice-zuul-3001
version: 1.0-SNAPSHOT
userName: http://swx.com
phone: 130************
啟動類:
加上@EnableZuulProxy注解
package com.swx.microservicezuul3001;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@EnableZuulProxy
public class MicroserviceZuul3001Application {
public static void main(String[] args) {
SpringApplication.run(MicroserviceZuul3001Application.class, args);
}
}
測試一下:
為了友善測試這裡就啟動一個注冊中心、1005服務端、3001zuul
通路測試位址:http://localhost:1005/book/list 拿到資料
我們用 http://zuul.swx.com:3001/microservice-book/book/list 域名+端口+服務名稱+請求位址 也能請求到資料;
測試成功說明我們的路由基本配置OK
Zuul路由映射配置
上面是zuul的簡單使用,從接口位址很輕易的就暴露了服務提供者的唯一辨別名microservice-book;有安全風險,我們需要将其隐藏;
ignored-services的作用是将原來的服務提供者唯一辨別名禁用;
Prefix的作用是給服務加字首
yml檔案中添加以下配置:
zuul:
routes:
studentServer.serviceId: microservice-book
studentServer.path: /bookService/**
ignored-services: "*"
prefix: /swx
當我加上這些然後重新啟動3001端口時,我們繼續用上面的位址就無法進行通路了
隻有當我們換成配置裡的新路徑時才能夠進行通路
http://zuul.swx.com:3001/swx/bookService/book/list
這樣我們的安全性就得到了大大的提升!
Zuul請求過濾配置
比如我們登入某個系統 需要身份驗證,使用者名密碼啥的;
我們請求服務,也可以來設定身份驗證,也就是過濾非法請求;Zuul通過ZuulFilter過濾器實作;
一般具體實作的話 每次經過Zuul服務網關 我們都對帶來的token進行有效性驗證;
我們先定義一個 AccessFilter類:
package com.swx.microservicezuul3001.filter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.log4j.Logger;
import javax.servlet.http.HttpServletRequest;
public class AccessFilter extends ZuulFilter {
Logger logger=Logger.getLogger(AccessFilter.class);
/**
* 判斷該過濾器是否要被執行
*/
@Override
public boolean shouldFilter() {
return true;
}
/**
* 過濾器的具體執行邏輯
*/
@Override
public Object run() throws ZuulException {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
String parameter = request.getParameter("accessToken");
logger.info(request.getRequestURL().toString()+" 請求通路");
if(parameter==null){
logger.error("accessToken為空!");
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
ctx.setResponseBody("{\"result\":\"accessToken is empty!\"}");
return null;
}
// token判斷邏輯
logger.info(request.getRequestURL().toString()+" 請求成功");
return null;
}
/**
* 過濾器的類型 這裡用pre,代表會再請求被路由之前執行
*/
@Override
public String filterType() {
return "pre";
}
/**
* 過濾器的執行順序
*/
@Override
public int filterOrder() {
return 0;
}
}
然後再開啟下 Filter配置:
package com.swx.microservicezuul3001.config;
import com.swx.microservicezuul3001.filter.AccessFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ZuulConfig {
@Bean
public AccessFilter accessFilter(){
return new AccessFilter();
}
}
重新啟動3001端口進行測試:
測試位址:http://zuul.swx.com:3001/swx/bookService/book/list
因為過濾器中的accessToken沒有值,是以直接拒絕了通路,下面我們就帶上參數通路看下
http://zuul.swx.com:3001/swx/bookService/book/list?accessToken=come