spring cloud微服務快速教程之(五) ZUUL API網關中心

0-前言

  咱們一個個微服務構建好了,外部的應用如何來訪問內部各類各樣的微服務呢?在微服務架構中,後端服務每每不直接開放給調用端,而是經過一個API網關根據請求的url,路由到相應的服務。當添加API網關後,在第三方調用端和服務提供方之間就建立了一個代理層,這個代理層直接與調用方通訊進行權限控制,後將請求均衡分發給後臺服務端。git

  Zuul:就是一個API中間代理層,能夠用來執行認證、動態路由、服務前移、負載均衡、安全、動態響應處理等;好比/api/user轉發到到user服務,/api/order轉發到到shop服務。zuul默認使用Ribbon實現負載均衡;github

 

1、ZUUL的簡單使用

1.一、建立ZUUL模塊,添加依賴:spring

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

1.二、建立配置文件,增長配置後端

server:
  port: 8081
spring:
  application:
    name: zuul
# 配置Eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
# 構建路由地址
zuul:
  prefix: /api
  routes:
    # 這裏能夠自定義
    user:
      # 匹配的路由規則
      path: /user/**
      # 路由的目標服務名
      serviceId: user
    order:
      # 匹配的路由規則
      path: /order/**
      # 路由的目標服務名
      serviceId: order

1.三、啓動類增長 @EnableZuulProxy 註解api

@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class application
{
    public  static void main(String[] args)
    {
        SpringApplication.run(application.class);
    }
}

完成安全

1.四、運行測試架構

 

 

 

 能夠看到,ZUUL已經按照配置文件的路由規則進行了路由轉發,並對服務實現了負載均衡;app

ZUUL簡單使用完畢;負載均衡

 GITdemo地址:https://github.com/anson-yang/springclouddemo微服務

相關文章
相關標籤/搜索