前言
在微服務(wù)架構(gòu)中耗拓,需要幾個(gè)基礎(chǔ)的服務(wù)治理組件,包括服務(wù)注冊(cè)與發(fā)現(xiàn)灿椅、服務(wù)消費(fèi)恨憎、負(fù)載均衡、斷路器验夯、智能路由猖吴、配置管理等,由這幾個(gè)基礎(chǔ)組件相互協(xié)作挥转,共同組建了一個(gè)簡(jiǎn)單的微服務(wù)系統(tǒng)海蔽。一個(gè)簡(jiǎn)答的微服務(wù)系統(tǒng)如下圖:
在Spring Cloud微服務(wù)系統(tǒng)中,一種常見的負(fù)載均衡方式是绑谣,客戶端的請(qǐng)求首先經(jīng)過負(fù)載均衡(zuul党窜、Ngnix),再到達(dá)服務(wù)網(wǎng)關(guān)(zuul集群)借宵,然后再到具體的服幌衣。,服務(wù)統(tǒng)一注冊(cè)到高可用的服務(wù)注冊(cè)中心集群壤玫,服務(wù)的所有的配置文件由配置服務(wù)管理(下一篇文章講述)豁护,配置服務(wù)的配置文件放在git倉庫,方便開發(fā)人員隨時(shí)改配置欲间。
一择镇、Zuul簡(jiǎn)介
Zuul的主要功能是路由轉(zhuǎn)發(fā)和過濾器。路由功能是微服務(wù)的一部分括改,比如/api/user轉(zhuǎn)發(fā)到到user服務(wù)腻豌,/api/shop轉(zhuǎn)發(fā)到到shop服務(wù)家坎。zuul默認(rèn)和Ribbon結(jié)合實(shí)現(xiàn)了負(fù)載均衡的功能。
zuul有以下功能:
- Authentication
- Insights
- Stress Testing
- Canary Testing
- Dynamic Routing
- Service Migration
- Load Shedding
- Security
- Static Response handling
- Active/Active traffic management
二吝梅、準(zhǔn)備工作
在原有的工程上新建一個(gè)工程
三虱疏、創(chuàng)建service-zuul工程
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>server-zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>server-zuul</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.example</groupId>
<artifactId>spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
在其入口applicaton類加上注解@EnableZuulProxy,開啟zuul的功能:
package com.example.serverzuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@EnableDiscoveryClient
@EnableEurekaClient
@EnableZuulProxy
@SpringBootApplication
public class ServerZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ServerZuulApplication.class, args);
}
}
加上配置文件application.yml加上以下的配置代碼:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8766
spring:
application:
name: service-zuul
zuul:
routes:
api-a:
path: /api-a/**
serviceId: service-ribbon
api-b:
path: /api-b/**
serviceId: service-feign
首先指定服務(wù)注冊(cè)中心的地址為http://localhost:8761/eureka/苏携,服務(wù)的端口為8766做瞪,服務(wù)名為service-zuul;以/api-a/ 開頭的請(qǐng)求都轉(zhuǎn)發(fā)給service-ribbon服務(wù)右冻;以/api-b/開頭的請(qǐng)求都轉(zhuǎn)發(fā)給service-feign服務(wù)装蓬;
坑來了
依次運(yùn)行這五個(gè)工程;打開瀏覽器訪問:http://localhost:8766/api-a/hi?name=lisi ;瀏覽器顯示:
Description:
The bean 'proxyRequestHelper', defined in class path resource [org/springframework/cloud/netflix/zuul/ZuulProxyAutoConfiguration$NoActuatorConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/cloud/netflix/zuul/ZuulProxyAutoConfiguration$EndpointConfiguration.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
Process finished with exit code 1
進(jìn)過排查之后發(fā)現(xiàn)是springboot版本高,我的是2.1.1版本纱扭,springcloud版本是F版本牍帚,只要把springboot版本調(diào)到2.0.6就行了(在父model里改)
再次訪問http://localhost:8766/api-a/hi?name=lisi
訪問http://localhost:8766/api-b/hi?name=lisi
證明了zuul服務(wù)有路由作用
四、服務(wù)過濾
zuul也可以有過濾作用乳蛾,可以用來當(dāng)安全驗(yàn)證
在ServerZuulApplication所在目錄下新建一個(gè)MyFilter類
package com.example.serverzuul;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
@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:返回一個(gè)字符串代表過濾器的類型暗赶,在zuul中定義了四種不同生命周期的過濾器類型,具體如下:
- pre:路由之前
- routing:路由之時(shí)
- post: 路由之后
- error:發(fā)送錯(cuò)誤調(diào)用 - filterOrder:過濾的順序
- shouldFilter:這里可以寫邏輯判斷肃叶,是否要過濾蹂随,本文true,永遠(yuǎn)過濾。
- run:過濾器的具體邏輯因惭≡浪可用很復(fù)雜,包括查sql蹦魔,nosql去判斷該請(qǐng)求到底有沒有權(quán)限訪問激率。
訪問:http://localhost:8766/api-a/hi?name=lisi
控制臺(tái)
訪問:http://localhost:8766/api-a/hi?name=lisi
控制臺(tái)