springcloud13---zuul
Zuul:API ?GATEWAY (服務網關):
http://blog.daocloud.io/microservices-2/
一個客戶端不同的功能請求不同的微服務,那么客戶端要知道所有微服務的ip和端口,如果有的微服務不是rest協議是用的別的協議,有時候有可能幾個微服務要合并,上面都是問題。
所以前面要加一個服務網關,客戶端只需要知道網關的ip和端口就可以了,網關知道后面微服務的ip和端口,缺點是要考慮網關的高可用。
?
Ribbon是客戶端的負載均衡器,Zuul是服務端的負載均衡器。
使用http://192.168.88.1:7901/simple/1直接訪問user微服務(7901是user的微服務地址),
http://192.168.88.1:8040/microservice-provider-user/simple/1(8040是zuul的端口,microservice-provider-user是user微服務的application:name)。通過zuul就可以訪問user服務了。
?
給user微服務指定別名。默認代理所有注冊到eureka上的微服務。
一個工程只有src和pom文件,加入.project文件然后更新工程就會出現.classpath文件和其他的文件就可以跑起來了。
經過zuul的請求都會通過hysitrcs包裹,所以zuul會有斷路器功能。zuul還使用了ribbon做負載均衡。
?
Zuul過濾器:
Zuul有4中過濾器,PRE,ROUTING,POST,ERROR。Pre先執行然后routing,然后post然后error.
pre是zuul請求別的微服務之前,routing是請求過程中的,post是請求到微服務之后可以添加一些header,error是拋異常了。
也可以自定義過濾器。
?
周立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: /simplestrip-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: 3000ReadTimeout: 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>?
總結
以上是生活随笔為你收集整理的springcloud13---zuul的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP 数组函数分类和整理
- 下一篇: Git 分支 - 变基