springcloud教程第4篇:Zuul---網(wǎng)關(guān)

獲取更多內(nèi)容

獲取更多內(nèi)容請(qǐng)?jiān)L問(wèn): https://juntech.top/
在微服務(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)如下圖:

Azure (1).png

在Spring Cloud微服務(wù)系統(tǒng)中,一種常見(jiàn)的負(fù)載均衡方式是,客戶端的請(qǐng)求首先經(jīng)過(guò)負(fù)載均衡(zuul、Ngnix),再到達(dá)服務(wù)網(wǎng)關(guān)(zuul集群),然后再到具體的服務(wù),服務(wù)統(tǒng)一注冊(cè)到高可用的服務(wù)注冊(cè)中心集群,服務(wù)的所有的配置文件由配置中心管理,配置服務(wù)的配置文件放在git倉(cāng)庫(kù)或本地,方便開(kāi)發(fā)人員隨時(shí)改配置。

1、什么是Zuul

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

2、準(zhǔn)備工作

繼續(xù)使用上一節(jié)的工程

3、創(chuàng)建zuul工程

pom.xml:需要引入zuul組件

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-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>

main程序:加入@EnableZuulProxy注解,開(kāi)啟zuul功能

@EnableZuulProxy
@EnableDiscoveryClient
@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:
  #將service-id為service-ribbon轉(zhuǎn)發(fā)到api-a接口
    api-a:
      path: /api-a/**
      serviceId: service-ribbon
#將service-id為service-feign轉(zhuǎn)發(fā)到api-b接口
    api-b:
      path: /api-b/**
      serviceId: service-feign

依次運(yùn)行這五個(gè)工程;打開(kāi)瀏覽器訪問(wèn):http://localhost:8769/api-a/hi?name=jd ;瀏覽器顯示:

hi jd,i am from port:8762

打開(kāi)瀏覽器訪問(wèn):http://localhost:8769/api-b/hi?name=jd ;瀏覽器顯示:

hi jd,i am from port:8762

這說(shuō)明zuul起到了路由轉(zhuǎn)發(fā)的作用

4、服務(wù)過(guò)濾

zuul不僅只是路由,并且還能過(guò)濾,做一些安全驗(yàn)證。繼續(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:返回一個(gè)字符串代表過(guò)濾器的類型,在zuul中定義了四種不同生命周期的過(guò)濾器類型,具體如下:
    • 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=jd ;網(wǎng)頁(yè)顯示:

token is empty

訪問(wèn) http://localhost:8769/api-a/hi?name=jd&token=22 ; 網(wǎng)頁(yè)顯示:

hi jd,i am from port:8762

5、參考資料

  • zuul

    ?## 更多詳情
    設(shè)置為vip可見(jiàn)的都可訪問(wèn)下面鏈接地址,即可觀看原文
    更多詳情請(qǐng)?jiān)L問(wèn): juntech

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容