上次我講了EurekaServer注冊中心鹦肿,接下來我們就來說說Zuul網(wǎng)關(guān)
一、Zuul簡介
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)備工作
繼續(xù)使用上一節(jié)的工程肖揣。在原有的工程上民假,創(chuàng)建一個(gè)新的工程。
三龙优、創(chuàng)建service-zuul工程
其pom.xml文件如下:
在其入口applicaton類加上注解@EnableZuulProxy羊异,開啟zuul的功能:
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;
import org.springframework.context.annotation.Bean;
@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ù)過濾
zuul不僅只是路由彤断,并且還能過濾野舶,做一些安全驗(yàn)證。繼續(xù)改造工程宰衙;
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;
/**
* Created by forezp on 2017/4/8.
*/
@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去判斷該請求到底有沒有權(quán)限訪問诽嘉。
這時(shí)訪問:http://localhost:8761 ;網(wǎng)頁顯示:
zuul目錄如下:
ok,zuul網(wǎng)關(guān)搭建就完成了弟翘,下一節(jié)我們說說Config配置中心虫腋,希望有人能喜歡~~~
·