在微服務(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)中饭冬,一種常見(jiàn)的負(fù)載均衡方式是使鹅,客戶端的請(qǐng)求首先經(jīng)過(guò)負(fù)載均衡(zuul,nginx)昌抠,在到達(dá)服務(wù)網(wǎng)關(guān)(zuul集群)患朱,然后再到具體的服務(wù),服務(wù)統(tǒng)一注冊(cè)到高可用的服務(wù)注冊(cè)中心集群服務(wù)的所有的配置文件由配置服務(wù)管理炊苫,配置服務(wù)的配置關(guān)鍵放在git倉(cāng)庫(kù)裁厅,方便開(kāi)發(fā)人員隨意改配置冰沙。
一、Zuul簡(jiǎn)介
Zuul的主要功能是路由轉(zhuǎn)發(fā)和過(guò)濾器执虹。路由功能是微服務(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)備工作
繼續(xù)使用上一節(jié)的工程茬故。在原有的工程上盖灸,創(chuàng)建一個(gè)新的工程
三、創(chuàng)建service-zuul工程
在其入口applicaton類(lèi)加上注解@EnableZuulProxy磺芭,開(kāi)啟zuul的功能:
@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class ServiceZuulApplication {
public static void main(String[] args) { ????SpringApplication.run(ServiceZuulApplication.class, args);
????}
}
加上配置文件application.yml加上以下的配置代碼:
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
首先指定服務(wù)注冊(cè)中心的地址為http://localhost:8761/eureka/赁炎,服務(wù)的端口為8769,服務(wù)名為service-zuul徘跪;以/api-a/ 開(kāi)頭的請(qǐng)求都轉(zhuǎn)發(fā)給service-ribbon服務(wù)甘邀;以/api-b/開(kāi)頭的請(qǐng)求都轉(zhuǎn)發(fā)給service-feign服務(wù);
依次運(yùn)行這五個(gè)工程;打開(kāi)瀏覽器訪問(wèn):http://localhost:8769/api-a/hi?name=forezp?;瀏覽器顯示:
hi forezp,i am from port:8762
打開(kāi)瀏覽器訪問(wèn):http://localhost:8769/api-b/hi?name=forezp?;瀏覽器顯示:
hi forezp,i am from port:8762
這說(shuō)明zuul起到了路由的作用
四垮庐、服務(wù)過(guò)濾
@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è)字符串代表過(guò)濾器的類(lèi)型松邪,在zuul中定義了四種不同生命周期的過(guò)濾器類(lèi)型,具體如下
pre:路由之前
routing:路由之時(shí)
post: 路由之后
error:發(fā)送錯(cuò)誤調(diào)用
filterOrder:過(guò)濾的順序
shouldFilter:這里可以寫(xiě)邏輯判斷哨查,是否要過(guò)濾逗抑,本文true,永遠(yuǎn)過(guò)濾。
run:過(guò)濾器的具體邏輯寒亥∮矢可用很復(fù)雜,包括查sql溉奕,nosql去判斷該請(qǐng)求到底有沒(méi)有權(quán)限訪問(wèn)褂傀。
這時(shí)訪問(wèn):http://localhost:8769/api-a/hi?name=forezp?;網(wǎng)頁(yè)顯示:
token is empty
訪問(wèn)?http://localhost:8769/api-a/hi?name=forezp&token=22?加勤;?
網(wǎng)頁(yè)顯示:
hi forezp,i am from port:8762
http://blog.csdn.net/forezp/article/details/69939114?
本文出自方志朋的博客