日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Zuul简介及代码示例

發(fā)布時(shí)間:2024/4/13 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Zuul简介及代码示例 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
zuul它是一種怪獸,可能是因?yàn)殚L(zhǎng)相太可怕了,百度居然搜不到,我們打開(kāi)Spring Cloud文檔https://cloud.spring.io/spring-cloud-netflix/reference/html/#_router_and_filter_zuulRouter and Filter: Zuul路由器和過(guò)濾器Zuul在2微服務(wù)架構(gòu)里面呢,路由是微服務(wù)架構(gòu)的一部分,根路徑會(huì)映射到你的WEB引用,/api/user他可能會(huì)映射到用戶(hù)微服務(wù),這個(gè)路徑會(huì)映射到你的商店微服務(wù),Zuul是Netflix開(kāi)發(fā)的,基于JVM的,路由器和服務(wù)器端的負(fù)載均衡器,我們之前講過(guò)客戶(hù)端負(fù)載均衡機(jī)制,Ribbon,Zuul他還是服務(wù)端的負(fù)載均衡Routing is an integral part of a microservice architecture. For example, / may be mapped to your web application, /api/users is mapped to the user service and /api/shop is mapped to the shop service. Zuul is a JVM-based router and server-side load balancer from Netflix.認(rèn)證,壓力測(cè)試,動(dòng)態(tài)路由,服務(wù)的遷移,減輕各個(gè)負(fù)載,靜態(tài)的響應(yīng)處理,還有流量的管理Netflix uses Zuul for the following:AuthenticationInsightsStress TestingCanary TestingDynamic RoutingService MigrationLoad SheddingSecurityStatic Response handlingActive/Active traffic managementZuul的規(guī)則引擎呢,使用任意的JVM語(yǔ)言寫(xiě)的,內(nèi)置可以使用JAVA和Groovy,但是理論上所有的JVM語(yǔ)言都可以玩,Zuul’s rule engine lets rules and filters be written in essentially any JVM language, with built-in support for Java and Groovy.這個(gè)配置項(xiàng)被兩個(gè)配置項(xiàng)代替了The configuration property zuul.max.host.connections has been replaced by two new properties, zuul.host.maxTotalConnections and zuul.host.maxPerRouteConnections, which default to 200 and 20 respectively.隔離一個(gè)是SEMAPHORE,一個(gè)是THREAD,一個(gè)是信號(hào),一個(gè)是線(xiàn)程,他在這個(gè)里面使用的信號(hào),你也可以使用這個(gè)配置,去把它指定成ThreadThe default Hystrix isolation pattern (ExecutionIsolationStrategy) for all routes is SEMAPHORE. zuul.ribbonIsolationStrategy can be changed to THREAD if that isolation pattern is preferred.http://www.github.com/netflix/hystrix我們講hystrix的時(shí)候有講過(guò)他的隔離https://github.com/netflix/hystrix/wikihttps://github.com/Netflix/Hystrix/wiki/Configuration一個(gè)是THREAD,一個(gè)是SEMAPHORE,他說(shuō)了默認(rèn)的是ThreadHow to Include Zuul你要在Spring Cloud里面引入某個(gè)組建的話(huà),你就要加上某某starterTo include Zuul in your project, use the starter with a group ID of org.springframework.cloud and a artifact ID of spring-cloud-starter-netflix-zuul. Spring Cloud創(chuàng)建了一個(gè)嵌入式的代理,他去簡(jiǎn)化了一種非常常見(jiàn)的開(kāi)發(fā),開(kāi)發(fā)的方式,這種方式是什么呢,其中UI應(yīng)用程序想要去代理,一個(gè)或者是多個(gè)后端的服務(wù)的調(diào)用,他說(shuō)這種特性呢,他說(shuō)這種特性是比較有用的,對(duì)于用戶(hù)界面他去代理,后端的服務(wù),對(duì)于所有后端去處理跨域,和認(rèn)證的需要,CORS是Cross Orginal Resouce Sharing,其實(shí)就是跨域請(qǐng)求,可以這樣理解,如果你想要使用它的話(huà),在你的主類(lèi)加上@EnableZuulProxy,本地的請(qǐng)求會(huì)轉(zhuǎn)發(fā)到相應(yīng)的服務(wù),按照約定,假設(shè)一個(gè)服務(wù)它的ID是users,它會(huì)用這種路徑去接收請(qǐng)求,然后他把前綴省略掉了,Zuul他使用的是Ribbon,通過(guò)Ribbon查詢(xún)服務(wù)注冊(cè)的組件,Eureka轉(zhuǎn)發(fā)你要的實(shí)例,所有的請(qǐng)求都會(huì)在Hystrix Command里面去執(zhí)行,我們之前解析過(guò)了Hystrix Command是什么東西了,所有使用@HystrixCommand這個(gè)注解,這樣所有的失敗都會(huì)在Hystrix的指標(biāo)里面去顯示,因?yàn)閿嗦菲鞫即蜷_(kāi)了,不會(huì)請(qǐng)求遠(yuǎn)程的服務(wù)了,相對(duì)于后端的服務(wù)ZuulSpring Cloud has created an embedded Zuul proxy to ease the development of a common use case where a UI application wants to make proxy calls to one or more back end services. This feature is useful for a user interface to proxy to the back end services it requires, avoiding the need to manage CORS and authentication concerns independently for all the back ends.To enable it, annotate a Spring Boot main class with @EnableZuulProxy. Doing so causes local calls to be forwarded to the appropriate service. By convention, a service with an ID of users receives requests from the proxy located at /users (with the prefix stripped). The proxy uses Ribbon to locate an instance to which to forward through discovery. All requests are executed in a hystrix command, so failures appear in Hystrix metrics. Once the circuit is open, the proxy does not try to contact the service.Zuul其實(shí)并沒(méi)有發(fā)現(xiàn)服務(wù)發(fā)現(xiàn)的客戶(hù)端,他沒(méi)有包含Eureka的Client,因此如果你想通過(guò)serviceId去路由的話(huà),你得提供依賴(lài)microservice-gateway-zuul<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zuul</artifactId> </dependency>Zuul的依賴(lài)加好了,這個(gè)時(shí)候我們是不是還得加Eureka的依賴(lài)the Zuul starter does not include a discovery client, so, for routes based on service IDs, you need to provide one of those on the classpath as well (Eureka is one choice).@EnableZuulProxy @SpringBootApplication public class EurekaApplication {public static void main(String[] args) {SpringApplication.run(EurekaApplication.class, args);} }@EnableZuulProxy它是一個(gè)組合注解@EnableCircuitBreaker @EnableDiscoveryClient @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Import(ZuulProxyConfiguration.class) public @interface EnableZuulProxy { }這就是為什么,我只要一個(gè)注解就可以把它注冊(cè)到Eurekalocalhost:7900/simple/1localhost:8040/microservice-simple-provider-user/simple/1所以這個(gè)就是Eureka里面的serviceId,Mapped URL path [/microservice-simple-provider-user/**] onto handler of type [class org.springframework.cloud.netflix.zuul.web.ZuulController]他其實(shí)是有一個(gè)映射的,規(guī)則也是非常簡(jiǎn)單的,比如你在eureka上有一個(gè)微服務(wù)叫abc,abc的端口是8000,他有一個(gè)接口是/simple/1,我們使用zuul去訪(fǎng)問(wèn)的,abc是應(yīng)用的名稱(chēng),這就可以訪(fǎng)問(wèn)了,但是沒(méi)有完全達(dá)到我的要求,這么長(zhǎng)一個(gè)我不喜歡,我想要改成user,我想改成這樣的localhost:8040/user/simple/1zuul:routes:user-route: # 該配置方式中,user-route只是給路由一個(gè)名稱(chēng),可以任意起名。service-id: microservice-provider-userpath: /user/** # service-id對(duì)應(yīng)的路徑 zuul.routes.user-route.service-id=microservice-simple-provider-user zuul.routes.user-route.path=/user/** 假設(shè)我現(xiàn)在有個(gè)user微服務(wù),有個(gè)電影微服務(wù),反向代理微服務(wù),默認(rèn)所有注冊(cè)到Eureka上的微服務(wù)microservice-consumer-movie-ribbon-hystrixlocalhost:8040/microservice-consumer-movie-ribbon-hystrix/movie/1localhost:8010/movie/1如果我只想代理user微服務(wù),不想代理電影微服務(wù),第一種辦法是把所有的微服務(wù)ignore,只有你配的微服務(wù)才反向代理,https://cloud.spring.io/spring-cloud-netflix/reference/html/#_router_and_filter_zuulzuul:ignoredServices: '*'routes:users: /myusers/**第二種就是配置zuul.ignoredServices=microservice-consumer-movie-ribbon-hystrix多個(gè)我可以用逗號(hào)/*** Set of service names not to consider for proxying automatically. By default all* services in the discovery client will be proxied.*/ private Set<String> ignoredServices = new LinkedHashSet<>();他是一個(gè)Set,我們可以用逗號(hào)分隔開(kāi) <?xml version="1.0" encoding="UTF-8"?> <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><groupId>com.learn.cloud</groupId><artifactId>microservice-gateway-zuul</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>cn.learn</groupId><artifactId>microcloud02</artifactId><version>0.0.1</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties> <dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId><version>1.4.2.RELEASE</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zuul</artifactId></dependency></dependencies><!-- 這個(gè)插件,可以將應(yīng)用打包成一個(gè)可執(zhí)行的jar包 --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project> server.port=8040 spring.application.name=microservice-gateway-zuul eureka.instance.prefer-ip-address=true eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}} eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eureka eureka.instance.appname=microservice-gateway-zuul zuul.routes.user-route.service-id=microservice-simple-provider-user #zuul.ignoredServices=microservice-consumer-movie-ribbon-hystrix zuul.routes.user-route.path=/user/** package com.learn.cloud;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy;@EnableZuulProxy @SpringBootApplication public class GateWayZuulApplication {public static void main(String[] args) {SpringApplication.run(GateWayZuulApplication.class, args);} }

?

總結(jié)

以上是生活随笔為你收集整理的Zuul简介及代码示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。