iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 20
0
Software Development

30天從零開始 使用 Spring Boot 跟 Spring Cloud 建構完整微服務架構系列 第 20

Day 20 - Gateway Zuul

簡單介紹一下, 其實 Zuul 是來自於 Netflix 的一個 軟體 閘道器, 那你一定會問....
用 Nginx 就好啦, 有比 HAProxy 快嗎?

軟體架構不是只有比快, 彈性也很重要, Zuul 透過我之前介紹自動發現機制, 他可以無須變更軟體組態, 只要服務發現新增了服務, 他就可以做一個內外部的代理功能, 其實自己也是 Service 的一種.

早期 Netflix 拿 Zuul 做 Gateway 的時候, Zuul 裡面也可以包含很多不同程度的功能 如 A/B 測試 金思雀部屬 等等..., 所以你大概可以想像, 他的客製化程度是很強的.

所以來看一下怎麼使用

新增 Proxy 機制

建立一個 reservation-client 專案

使用到的組件如下

Web Cloud Config Cloud Discovery Cloud Routing Cloud Circuit Breaker Cloud Tracing Cloud Messaging Ops
HATEOAS Config Client Eureka Discovery Zuul Hystrix Zipkin Stream Redis Actuator

下面是我們會使用到的一些依賴
build.gradle

dependencies {
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.cloud:spring-cloud-starter-config')
    compile('org.springframework.cloud:spring-cloud-starter-eureka')
    compile('org.springframework.cloud:spring-cloud-starter-hystrix')
    compile('org.springframework.cloud:spring-cloud-starter-zuul')
    testCompile('org.springframework.boot:spring-boot-starter-test') 
}

把範例的 application.properties 刪掉並新增 bootstrap.properties

spring.application.name=reservation-client
spring.cloud.config.uri=http://localhost:8888

起動程式
ReservationClientApplication.java

package com.example;

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

@EnableZuulProxy
@EnableDiscoveryClient
@SpringBootApplication
public class ReservationClientApplication {

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

在原先的範例程式上增加 @EnableZuulProxy 跟 @EnableDiscoveryClient
起動後即可 http://localhost:8050/reservation-service/reservations 取得原本 reservation-service 上的資料如下,可以很明顯的得出來多了一層 reservation-service 代理路徑

{
  "_embedded": {
    "reservations": [
      {
        "reservationName": "Dr. rod",
        "_links": {
          "self": {
            "href": "http://localhost:8050/reservation-service/reservations/1"
          },
          "reservation": {
            "href": "http://localhost:8050/reservation-service/reservations/1"
          }
        }
      },
      {
        "reservationName": "Dr. Syer",
        "_links": {
          "self": {
            "href": "http://localhost:8050/reservation-service/reservations/2"
          },
          "reservation": {
            "href": "http://localhost:8050/reservation-service/reservations/2"
          }
        }
      },
      {
        "reservationName": "Juergen",
        "_links": {
          "self": {
            "href": "http://localhost:8050/reservation-service/reservations/3"
          },
          "reservation": {
            "href": "http://localhost:8050/reservation-service/reservations/3"
          }
        }
      },
      {
        "reservationName": "ALL THE COMMUNITY",
        "_links": {
          "self": {
            "href": "http://localhost:8050/reservation-service/reservations/4"
          },
          "reservation": {
            "href": "http://localhost:8050/reservation-service/reservations/4"
          }
        }
      },
      {
        "reservationName": "Josh",
        "_links": {
          "self": {
            "href": "http://localhost:8050/reservation-service/reservations/5"
          },
          "reservation": {
            "href": "http://localhost:8050/reservation-service/reservations/5"
          }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://localhost:8050/reservation-service/reservations"
    },
    "profile": {
      "href": "http://localhost:8050/reservation-service/profile/reservations"
    },
    "search": {
      "href": "http://localhost:8050/reservation-service/reservations/search"
    }
  },
  "page": {
    "size": 20,
    "totalElements": 5,
    "totalPages": 1,
    "number": 0
  }
}

上一篇
Day 19 - 線上服務組態變更 Cloud Bootstrap & Cloud Bus AMQP
下一篇
Day 21 - 呼叫遠端服務 Feign
系列文
30天從零開始 使用 Spring Boot 跟 Spring Cloud 建構完整微服務架構35
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言