springcloud13---zuul

ZuulAPI  GATEWAY (服務網關):spring

http://blog.daocloud.io/microservices-2/apache

一個客戶端不一樣的功能請求不一樣的微服務,那麼客戶端要知道全部微服務的ip和端口,若是有的微服務不是rest協議是用的別的協議,有時候有可能幾個微服務要合併,上面都是問題。api

因此前面要加一個服務網關,客戶端只須要知道網關的ip和端口就能夠了,網關知道後面微服務的ip和端口,缺點是要考慮網關的高可用。app

 

Ribbon是客戶端的負載均衡器,Zuul是服務端的負載均衡器。負載均衡

使用http://192.168.88.1:7901/simple/1直接訪問user微服務(7901user的微服務地址),maven

http://192.168.88.1:8040/microservice-provider-user/simple/18040zuul的端口,microservice-provider-useruser微服務的application:name)。經過zuul就能夠訪問user服務了。ide

 

user微服務指定別名。默認代理全部註冊到eureka上的微服務。微服務

一個工程只有srcpom文件,加入.project文件而後更新工程就會出現.classpath文件和其餘的文件就能夠跑起來了。post

通過zuul的請求都會經過hysitrcs包裹,因此zuul會有斷路器功能。zuul還使用了ribbon作負載均衡。ui

 

Zuul過濾器:

Zuul4中過濾器,PREROUTINGPOSTERRORPre先執行而後routing,而後post而後error.

prezuul請求別的微服務以前,routing是請求過程當中的,post是請求到微服務以後能夠添加一些headererror是拋異常了。

也能夠自定義過濾器。

 

周立springclud : http://www.itmuch.com/advertisment/my-spring-book/

package com.itmuch.cloud;

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

@SpringBootApplication
@EnableZuulProxy     //使用這一個註解就能夠註冊到eureka,
public class ZuulApplication {
  public static void main(String[] args) {
    SpringApplication.run(ZuulApplication.class, args);
  }
}
spring:
  application:
    name: microservice-gateway-zuul
server:
  port: 8040
eureka:
  client:
    service-url:
      defaultZone: http://user:password123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
zuul:
  routes:
    abc:                            # abc隨意,只要惟一就能夠
      path: /user-path/**               # 使用user-path訪問user微服務
      serviceId: microservice-provider-user        # 註冊到eureka的服務的application名字
    
# http://localhost:8040/routes : 查看zuul代理的微服務
# {"/user-path/**":"microservice-provider-user","/microservice-provider-user/**":"microservice-provider-user"}
# 就可使用zuul來代理user微服務:http://localhost:8040/microservice-provider-user/simple/1
# zuul的請求都用hystrix包裹:http://localhost:8040/hystrix.stream
spring:
  application:
    name: microservice-gateway-zuul
server:
  port: 8040
eureka:
  client:
    service-url:
      defaultZone: http://user:password123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
zuul:
  prefix: /simple
  strip-prefix: false
logging:
  level:
    com.netflix: debug
spring:
  application:
    name: microservice-gateway-zuul
server:
  port: 8040
eureka: #註冊到eureka server上面去
  client:
    service-url:
      defaultZone: http://user:password123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
    #prefer-ip-address: false # localhost:microservice-gateway-zuul:8040
    
#通過zuul的請求都會經過hysitrcs包裹,配置hysitrcs的超時時間
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000

#zuul還使用了ribbon作負載均衡,要設備ribbon的超時時間
ribbon:
  ConnectTimeout: 3000
  ReadTimeout: 60000
spring:
  application:
    name: microservice-gateway-zuul
server:
  port: 8040
eureka:
  client:
    service-url:
      defaultZone: http://user:password123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
zuul:
  prefix: /api   # http://192.168.88.1:8040/api/microservice-provider-user/simple/1 前綴加服務的名稱
  strip-prefix: true   # 爲false就不能經過加前綴/api來訪問了,
logging:
  level:
    com.netflix: DEBUG
spring:
  application:
    name: microservice-gateway-zuul
server:
  port: 8040
eureka:
  client:
    service-url:
      defaultZone: http://user:password123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
zuul:
  routes:
    abc:
      path: /user-url/**
      url: http://192.168.85.1:7900/
    
spring:
  application:
    name: microservice-gateway-zuul
server:
  port: 8040
eureka:
  client:
    service-url:
      defaultZone: http://user:password123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
zuul:
  routes:
    abc:  #只針對abc路由
      path: /user-url/**
      service-id: microservice-provider-user
ribbon:
  eureka:
    enabled: false
# http://192.168.88.1:8040/microservice-provider-user/simple/1 會從7901和7902這2個節點來請求
microservice-provider-user:     # 這邊是ribbon要請求的微服務的serviceId,7901和7902是2個user微服務,
  ribbon:
    listOfServers: http://localhost:7901,http://localhost:7902
spring:
  application:
    name: microservice-gateway-zuul
server:
  port: 8040
eureka:
  client:
    service-url:
      defaultZone: http://user:password123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
zuul:
  ignoredServices: microservice-consumer-movie-ribbon-with-hystrix  #不想反向代理microservice-consumer-movie-ribbon-with-hystrix微服務
  routes:
    microservice-provider-user: /user/**  #user微服務的別名
    # 如今http://192.168.88.1:8040/user/simple/1訪問user微服務,原來http://192.168.88.1:8040/microservice-provider-user/simple/1
    #默認zuul會反向代理全部註冊到eureka的微服務
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.itmuch.cloud</groupId>
        <artifactId>microservice-spring-cloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>microservice-gateway-zuul</artifactId>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <!-- 依賴,還要加eureka client的依賴 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
    </dependencies>

</project>
本站公眾號
   歡迎關注本站公眾號,獲取更多信息