在微服務(wù)架構(gòu)中宏娄,需要幾個關(guān)鍵的組件,服務(wù)注冊與發(fā)現(xiàn)逮壁、服務(wù)消費孵坚、負載均衡、斷路器窥淆、智能路由卖宠、配置管理等,由這幾個組件可以組建一個簡單的微服務(wù)架構(gòu)忧饭,如下圖:
注意:A服務(wù)和B服務(wù)是可以相互調(diào)用的扛伍,作圖的時候忘記了。并且配置服務(wù)也是注冊到服務(wù)注冊中心的词裤。
客戶端的請求首先經(jīng)過負載均衡(zuul刺洒、Ngnix),再到達服務(wù)網(wǎng)關(guān)(zuul集群)吼砂,然后再到具體的服務(wù)逆航,服務(wù)統(tǒng)一注冊到高可用的服務(wù)注冊中心集群,服務(wù)的所有的配置文件由配置服務(wù)管理(下一篇文章講述)渔肩,配置服務(wù)的配置文件放在git倉庫因俐,方便開發(fā)人員隨時改配置。
一周偎、Zuul簡介
Zuul的主要功能是路由和過濾器抹剩。路由功能是微服務(wù)的一部分,比如/api/user映射到user服務(wù)蓉坎,/api/shop映射到shop服務(wù)吧兔。zuul實現(xiàn)了負載均衡。
zuul有以下功能:
- Authentication
- Insights
- Stress Testing
- Canary Testing
- Dynamic Routing
- Service Migration
- Load Shedding
- Security
- Static Response handling
- Active/Active traffic management
二袍嬉、準(zhǔn)備工作
繼續(xù)使用上一節(jié)的工程境蔼。在原有的工程上灶平,創(chuàng)建一個新的工程。
三箍土、創(chuàng)建service-zuul工程
其pom.xml文件如下:
<?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.forezp</groupId>
<artifactId>service-zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-zuul</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<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>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
在其入口applicaton類加上注解@EnableZuulProxy逢享,開啟zuul:
@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class ServiceZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceZuulApplication.class, args);
}
}
加上配置文件:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8769
spring:
application:
name: service-zuul
zuul:
routes:
api-a:
path: /api-a/**
serviceId: service-ribbon
api-b:
path: /api-b/**
serviceId: service-feign
首先向eureka注冊自己,端口為8769吴藻,服務(wù)名為service-zuul瞒爬;以/api-a/ 開頭的請求都指向service-ribbon;以/api-b/開頭的請求都指向service-feign沟堡;
依次運行這五個工程;打開瀏覽器訪問:http://localhost:8769/api-a/hi?name=forezp ;瀏覽器顯示:
hi forezp,i am from port:8762
打開瀏覽器訪問:http://localhost:8769/api-b/hi?name=forezp ;瀏覽器顯示:
hi forezp,i am from port:8762
這說明zuul起到了路由的作用侧但;
四、服務(wù)過濾
zuul不僅只是路由航罗,并且還能過濾禀横,做一些安全驗證。繼續(xù)改造工程粥血;
@Component
public class MyFilter extends ZuulFilter{
private static Logger log = LoggerFactory.getLogger(MyFilter.class);
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 0;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));
Object accessToken = request.getParameter("token");
if(accessToken == null) {
log.warn("token is empty");
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
try {
ctx.getResponse().getWriter().write("token is empty");
}catch (Exception e){}
return null;
}
log.info("ok");
return null;
}
}
- filterType:返回一個字符串代表過濾器的類型柏锄,在zuul中定義了四種不同生命周期的過濾器類型,具體如下:
- pre:路由之前
- routing:路由之時
- post: 路由之后
- error:發(fā)送錯誤調(diào)用
- filterOrder:過濾的順序
- shouldFilter:這里可以寫邏輯判斷复亏,是否要過濾趾娃,本文true,永遠過濾。
- run:過濾器的具體邏輯缔御√疲可用很復(fù)雜,包括查sql耕突,nosql去判斷該請求到底有沒有權(quán)限訪問笤成。
這時訪問:http://localhost:8769/api-a/hi?name=forezp ;網(wǎng)頁顯示:
token is empty
訪問 http://localhost:8769/api-a/hi?name=forezp&token=22 有勾;
網(wǎng)頁顯示:
hi forezp,i am from port:8762
本文源碼下載:
https://github.com/forezp/SpringCloudLearning/tree/master/chapter5