SpringCloud 微服務 (十三) 服務網關 Zuul 路由

本篇延續上篇Zuul基礎學習,作一個實踐測試java

在以前學習的篇章中,一直積累學習,因此這邊已經存在註冊中心,product服務,order服務,config配置中心等等服務,每次寫demo,註冊中心和配置中心都是一直先啓動,本次學習Zuul也不例外web

 

新建一個服務 ,第一步利用IDEA建立spring

第二步,選擇紅框中的組件,通常服務我都會加上Webdocker

第三步開始配置,將application.properties文件改爲bootstrap.yml,若是忘了爲何這樣改,能夠回頭看看Spring cloud config & Spring cloud bus等內容,內容以下bootstrap

spring:
  application:
    name: gateway
  cloud:
    config:
      discovery:
        service-id: CONFIG
        enabled: true
      profile: dev

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

第四步,都是套路,在啓動類上添加開啓組件註解@EnableZuulProxy瀏覽器

package com.cloud.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableZuulProxy
public class GatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

說明下目前啓動相關服務以及端口->eureka server:8761,config:8000,product:9000,order:9001,docker 中rabbitMQ:5672,gateway:7000-->從gateway網關訪問product服務中的接口springboot

目前網關服務中未添加任何過濾器,先試試效果,下圖是啓動後註冊中心的3個服務發現cookie

在product服務中,以前寫好的簡單查詢接口: localhost:9000/product/list 返回一個列表app

如今從gateway網關去訪問: localhost:7000/product/product/list 就能夠訪問到了學習

第一個product是註冊中心的application的名字,後面product/list是接口

訪問config也是沒問題的,訪問方式:localhost:7000/config/config/product-dev.yml

可是呢,有個性的我每次都根據service-id來寫,明顯不是那麼合我口味,因而能夠添加yml配置

zuul:
  routes: 
    MyProduct: #這個能夠自定義
      path: /myProduct/**
      serviceId: product

意思是訪問服務中心serviceId是product的服務,訪問path方式是/myProduct/**, **表明全部該服務接口,能夠瀏覽器訪問試試

若是想要看到全部路由,能夠打開actuator監控,在springboot2.0之後,actuator改動比較大,在以前的筆記中有記錄相關內容,若是你使用的是2.0以前的版本,配置本身適合的方式就能夠,這裏以springboot2.0.3版本爲例,如下配置,以及監控信息展現,上面是目前的全部路由

management:
  endpoints:
    web:
      exposure:
        include: ["routes"]

還有一種簡潔的路由寫法,其中ignored是排除接口,set類型的參數,使用- /path方式配置

zuul:
  routes:
    product: /myProduct/**
  ignored-patterns: 
    - /**/product/hello

 

另外說個點,Zuul默認是不接收cookie,須要配置才能夠,貼上完整的配置

spring:
  application:
    name: gateway
  cloud:
    config:
      discovery:
        service-id: CONFIG
        enabled: true
      profile: dev

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

zuul:
  routes:
    product: /myProduct/**
  sensitiveHeaders:        #設置空便可,可用於接收cookie
  ignored-patterns:
    - /**/product/hello

management:
  endpoints:
    web:
      exposure:
        include: ["routes"]

 

關於路由,在開發的時候,咱們會加服務,加了服務,就要改動配置,都要從新啓動什麼的,相信你們都知道,用到以前學過的springcloud bus結合MQ實現配置動態刷新springcloud config

配置刷新了,咱們還要在代碼中也作到動態更新到運行中,因此能夠在啓動類下面加一段:

@SpringBootApplication
@EnableZuulProxy
public class GatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }

    @ConfigurationProperties("zuul")
    @RefreshScope
    public ZuulProperties zuulProperties(){
        return new ZuulProperties();
    }
}

 

以上是粗略的路由相關的筆記,下來繼續學習過濾器相關的內容

---------------------------------------------

相關文章
相關標籤/搜索