SpringCloud學習(4)

        本篇介紹並集成ZUULweb

API getaway:
API Gateway是一個服務器,也能夠說是進入系統的惟一節點。這跟面向對象設計模式中的Facade模式很像。API Gateway封裝內部系統的架構,而且提供API給各個客戶端。它還可能有其餘功能,如受權、監控、負載均衡、緩存、請求分片和管理、靜態響應處理等。下圖展現了一個適應當前架構的API Gateway。spring

clipboard.png
API Gateway負責請求轉發、合成和協議轉換。全部來自客戶端的請求都要先通過API Gateway,而後路由這些請求到對應的微服務。API Gateway將常常經過調用多個微服務來處理一個請求以及聚合多個服務的結果。它能夠在web協議與內部使用的非Web友好型協議間進行轉換,如HTTP協議、WebSocket協議。設計模式

    Spring Cloud Zuul路由是微服務架構的不可或缺的一部分,提供動態路由,監控,彈性,安全等的邊緣服務。Zuul是Netflix出品的一個基於JVM路由和服務端的負載均衡器。api

新建Springboot項目,pom添加依賴緩存

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>

application:安全

/**
    使用@EnableZuulProxy註解激活zuul。
 * 跟進該註解能夠看到該註解整合了@EnableCircuitBreaker、@EnableDiscoveryClient,是個組合註解,目的是簡化配置。
 
 */
@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZuulApplication.class,args);
    }
}

yml:服務器

spring:
  application:
    name: microservice-api-gateway
server:
  port: 8000
eureka:
  instance:
    hostname: gateway
  client:
    serviceUrl:
      defaultZone: http://discovery:8761/eureka/
# 下面整個樹都非必須,若是不配置,將默認使用 http://GATEWAY:GATEWAY_PORT/想要訪問的Eureka服務id的小寫/** 路由到:http://想要訪問的Eureka服務id的小寫:該服務端口/**
zuul:
  routes:
    user:                                               # 能夠隨便寫,在zuul上面惟一便可;當這裏的值 = service-id時,service-id能夠不寫。
      path: /user/**                                    # 想要映射到的路徑
      service-id: userprovider         # Eureka中的serviceId

啓動後這樣就能夠 用`http://GATEWAY:GATEWAY_PORT/想要訪問的Eureka服務id的小寫/**
訪問微服務了架構

若想忽略某個微服務則能夠配置
`zuul:
ignored-services: microservice-provider-user # 須要忽視的服務(配置後將不會被路由)`app

相關文章
相關標籤/搜索