springcloud13---zuul
Zuul:API ?GATEWAY (服務(wù)網(wǎng)關(guān)):
http://blog.daocloud.io/microservices-2/
一個(gè)客戶端不同的功能請(qǐng)求不同的微服務(wù),那么客戶端要知道所有微服務(wù)的ip和端口,如果有的微服務(wù)不是rest協(xié)議是用的別的協(xié)議,有時(shí)候有可能幾個(gè)微服務(wù)要合并,上面都是問題。
所以前面要加一個(gè)服務(wù)網(wǎng)關(guān),客戶端只需要知道網(wǎng)關(guān)的ip和端口就可以了,網(wǎng)關(guān)知道后面微服務(wù)的ip和端口,缺點(diǎn)是要考慮網(wǎng)關(guān)的高可用。
?
Ribbon是客戶端的負(fù)載均衡器,Zuul是服務(wù)端的負(fù)載均衡器。
使用http://192.168.88.1:7901/simple/1直接訪問user微服務(wù)(7901是user的微服務(wù)地址),
http://192.168.88.1:8040/microservice-provider-user/simple/1(8040是zuul的端口,microservice-provider-user是user微服務(wù)的application:name)。通過zuul就可以訪問user服務(wù)了。
?
給user微服務(wù)指定別名。默認(rèn)代理所有注冊(cè)到eureka上的微服務(wù)。
一個(gè)工程只有src和pom文件,加入.project文件然后更新工程就會(huì)出現(xiàn).classpath文件和其他的文件就可以跑起來了。
經(jīng)過zuul的請(qǐng)求都會(huì)通過hysitrcs包裹,所以zuul會(huì)有斷路器功能。zuul還使用了ribbon做負(fù)載均衡。
?
Zuul過濾器:
Zuul有4中過濾器,PRE,ROUTING,POST,ERROR。Pre先執(zhí)行然后routing,然后post然后error.
pre是zuul請(qǐng)求別的微服務(wù)之前,routing是請(qǐng)求過程中的,post是請(qǐng)求到微服務(wù)之后可以添加一些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 //使用這一個(gè)注解就可以注冊(cè)到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微服務(wù)serviceId: microservice-provider-user # 注冊(cè)到eureka的服務(wù)的application名字# http://localhost:8040/routes : 查看zuul代理的微服務(wù) # {"/user-path/**":"microservice-provider-user","/microservice-provider-user/**":"microservice-provider-user"} # 就可以使用zuul來代理user微服務(wù):http://localhost:8040/microservice-provider-user/simple/1 # zuul的請(qǐng)求都用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: #注冊(cè)到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#經(jīng)過zuul的請(qǐng)求都會(huì)通過hysitrcs包裹,配置hysitrcs的超時(shí)時(shí)間 hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000#zuul還使用了ribbon做負(fù)載均衡,要設(shè)備ribbon的超時(shí)時(shí)間 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 前綴加服務(wù)的名稱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: #只針對(duì)abc路由path: /user-url/**service-id: microservice-provider-user ribbon:eureka:enabled: false # http://192.168.88.1:8040/microservice-provider-user/simple/1 會(huì)從7901和7902這2個(gè)節(jié)點(diǎn)來請(qǐng)求 microservice-provider-user: # 這邊是ribbon要請(qǐng)求的微服務(wù)的serviceId,7901和7902是2個(gè)user微服務(wù),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微服務(wù)routes:microservice-provider-user: /user/** #user微服務(wù)的別名# 現(xiàn)在http://192.168.88.1:8040/user/simple/1訪問user微服務(wù),原來http://192.168.88.1:8040/microservice-provider-user/simple/1#默認(rèn)zuul會(huì)反向代理所有注冊(cè)到eureka的微服務(wù) <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>?
總結(jié)
以上是生活随笔為你收集整理的springcloud13---zuul的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP 数组函数分类和整理
- 下一篇: Java服务框架分析