App下載

SpringCloud微服務(wù)架構(gòu)中Zuul網(wǎng)關(guān)的基本介紹和使用

猿友 2021-08-04 14:39:47 瀏覽數(shù) (1733)
反饋

簡介

  • Zuul是Spring Cloud全家桶中的微服務(wù)API網(wǎng)關(guān)。 所有從設(shè)備或網(wǎng)站來的請求都會經(jīng)過Zuul到達(dá)后端的Netflix應(yīng)用程序
  • Zuul 主要提供路由(請求轉(zhuǎn)發(fā))和過濾
  • Zuul 最終會注入Eureka

提供: 代理,過濾和路由三大功能

使用

導(dǎo)入依賴

    <!--zuul組件、zuul需要注冊至eureka中-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zuul</artifactId>
        <version>1.4.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>

配置文件

server:
  port: 9527
spring:
  application:
    name: springcloud-zuul
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:7001/eureka/,http://eureka2:7002/eureka/,http://eureka3:7003/eureka/

  instance:
    instance-id: zull9527.com #別名

開啟支持

@SpringBootApplication
@EnableZuulProxy//開啟zuul支持,默認(rèn)注冊到Eureka
public class Zuul9527Application {
    public static void main(String[] args) {
        SpringApplication.run(Zuul9527Application.class,args);
    }
}

此時我們可以通過 地址:端口號/服務(wù)名稱/服務(wù) 來訪問了

注意:此處需在host文件添加 127.0.01 www,zuultest.com

202141695526308

為了不使我們的服務(wù)名稱暴露我們可以在配置文件中添加

zuul:
  routes:
    xxx.serviceId: provider-name # xxx代表任意名稱
    xxx.path: /mydept/** 
  ignored-services: provider-name # 不再通過這個路徑訪問即不允許通過服務(wù)名直接訪問
  prefix: /lin # 訪問路徑必須加上前綴/lin

此時只有通過 地址:端口號/lin/mydept/服務(wù) 來訪問

202141695613259

補(bǔ)充: 為什么在配置文件中是使用serviceId和path 在zull中route是以鍵值對的形式存放的

public void setRoutes(Map<String, ZuulRoute> routes) {
		this.routes = routes;
	}

而在ZuulProperties中

/**
 * Represents a Zuul route.
 */
public static class ZuulRoute {
        /**
         * The path (pattern) for the route, e.g. /foo/**.
         */
        private String path;
        /**
	* The service ID (if any) to map to this route. You can specify a physical URL or
        * a service, but not both.
	*/
         private String serviceId;
      }

以上就是 SpringCloud 微服務(wù)架構(gòu)中的 Zuul 網(wǎng)關(guān)的基本介紹和使用的詳細(xì)內(nèi)容,想要了解更多關(guān)于SpringCloud Zuul的使用的資料,請關(guān)注W3Cschool其它相關(guān)文章!


0 人點(diǎn)贊