In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to implement Gateway routing gateway in spring cloud 2.x. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Spring Cloud Gateway is a new project of Spring Cloud, which is a gateway developed based on technologies such as Spring5.0,Sprint Boot2.0 and Project Reactor. Its purpose is to provide a simple and effective unified api route management method in micro-service architecture. The goal of Spring Cloud Gateway is to replace Netflix Zuul, which not only provides a unified route management, but also provides a set of gateway functions based on Fliter chain, such as current limit, burying point, security monitoring and so on.
Name terminology
Route (routing): the basic module of a gateway, which consists of an id, a destination uri, a set of assertions, and a set of filters. If the assertion is true, the route matches.
Predicate (assertion): is a Predicate of java8. The input type is a ServerWebExchange. You can use it to match the content from the HTTP request.
Filter (filter): is an instance of org.springframework.cloud.gateway.filter.GatewayFilter that you can use to modify requests and responses.
Process flow
The client sends a request to the Spring Cloud Gateway. If the Gateway Handler map determines that the request matches the route, it is sent to Gateway Web Handler. Gateway Web Handler sends the request through a request-specific filter chain. The reason filters are separated by dotted lines is that filters can execute logic before or after sending proxy requests. Execute all "pre" filtering logic, and then issue a proxy request. After the proxy request is made, the "POST" filtering logic is executed.
Create Zuul Project 1.1 create sping boot Project: spring-gateway1.2 add pom.xml related dependencies org.springframework.boot spring-boot-starter-webflux org.springframework.cloud spring-cloud-starter-gateway org.springframework.cloud spring-cloud-starter-netflix-eureka-client1.3 application.yml add configuration information server: port: 8100spring: application: name: spring-gateway cloud: gateway: discovery: Locator: enabled: trueeureka: instance: hostname: eureka1.server.com lease-renewal-interval-in-seconds: 5 lease-expiration-duration-in-seconds: 10 client: service-url: defaultZone: http://eureka1.server.com:8701/eureka/, Http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
Spring.cloud.gateway.discovery.locator.enabled:true here simply uses the default function of creating routing rules and automatically creating routes according to serviceid.
1.4 description of the startup class SpringGatewayApplication
In previous articles, @ EnableEurekaClient or @ EnableDiscoveryClient annotations have been added to the corresponding startup classes. Today, when I read the document, I suddenly found that it can be automatically added to the registry without adding @ Enable* annotations, because after the Spring Cloud Edgware version, as long as the relevant dependencies are added and the corresponding configuration is made, the service can be automatically registered with the service discovery component.
1.5 start related services
Start the eureka-server, eureka-client, eureka-ribbon, spring-gateway services sequentially. Open the browser and go to the eureka-server service center to see if the service starts normally, as shown below:
The red box in the screenshot indicates that all services have been started normally. Then call the browser and enter http://localhost:8100/SPRING-GATEWAY/EUREKA-RIBBON/sayHello, which is displayed as follows:
Because we use automatic routing, all the service names on the url should be capitalized, which is consistent with the service registry. Here I can take a look at the startup log of spring-gate. Automatic routing names are all uppercase. Personally, I don't think the design here is very good.
2019-10-12 16 INFO 40 INFO 35.870 INFO 97725-[ctor-http-nio-2] c.netflix.config.ChainedDynamicProperty: Flipping property: SPRING-GATEWAY.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 21474836472019-10-12 16 40 Swiss 35.890 INFO 97725-[ctor-http-nio-2] c.n.u.concurrent.ShutdownEnabledTimer: Shutdown hook installed for: NFLoadBalancer-PingTimer-SPRING-GATEWAY2019-10-12 16:40:35. INFO 97725-[ctor-http-nio-2] c.netflix.loadbalancer.BaseLoadBalancer: Client: SPRING-GATEWAY instantiated a LoadBalancer: DynamicServerListLoadBalancer: {NFLoadBalancer:name=SPRING-GATEWAY Current list of Servers= [], Load balancer stats=Zone stats: {} Server stats: []} ServerList:null2019-10-12 16 ctor-http-nio-2 40 INFO 35.895 INFO 97725-[ctor-http-nio-2] c.n.l.DynamicServerListLoadBalancer: Using serverListUpdater PollingServerListUpdater2019-10-12 16 V 40 ctor-http-nio-2 35.910 INFO 97725-[ctor-http-nio-2] c.netflix.config.ChainedDynamicProperty: SPRING-GATEWAY.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 21474836472019-10-12 16 40 V 35.911 INFO 97725-[ctor-http-nio-2] c.n.l.DynamicServerListLoadBalancer: DynamicServerListLoadBalancer for client SPRING-GATEWAY initialized: DynamicServerListLoadBalancer: {NFLoadBalancer:name=SPRING-GATEWAY Current list of Servers= [eureka1.server.com:8100], Load balancer stats=Zone stats: {defaultzone= [Zone:defaultzone Instance count:1; Active connections count: 0; Circuit breaker tripped count: 0; Active connections per server: 0;]}, Server stats: [Server:eureka1.server.com:8100; Zone:defaultZone; Total Requests:0; Successive connection failure:0; Total blackout seconds:0; Last connection made:Thu Jan 01 08:00:00 CST 1970; First connection made:Thu Jan 01 08:00:00 CST 1970; Active Connections:0 Total failure count in last (1000) msecs:0; average resp time:0.0; 90 percentile resp time:0.0; 95 percentile resp time:0.0; min resp time:0.0; max resp time:0.0 Stddev resp time:0.0]} ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@3b80e99c2019-10-12 1640 tor-http-nio-10 c.n.u 36.008 INFO 97725-[tor-http-nio-10] c.netflix.config.ChainedDynamicProperty: Flipping property: EUREKA-RIBBON.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 21474836472019-10-12 161474836472019-[tor-http-nio-10] c.n.u. Concurrent.ShutdownEnabledTimer: Shutdown hook installed for: NFLoadBalancer-PingTimer-EUREKA-RIBBON2019-10-12 1640 INFO 36.010 INFO 97725-[tor-http-nio-10] c.netflix.loadbalancer.BaseLoadBalancer: Client: EUREKA-RIBBON instantiated a LoadBalancer: DynamicServerListLoadBalancer: {NFLoadBalancer:name=EUREKA-RIBBON Current list of Servers= [], Load balancer stats=Zone stats: {} Server stats: []} ServerList:null2019-10-12 16 tor-http-nio-10 40 INFO 36.011 INFO 97725-[tor-http-nio-10] c.n.l.DynamicServerListLoadBalancer: Using serverListUpdater PollingServerListUpdater2019-10-12 16 V 40 tor-http-nio-10 36.012 INFO 97725-[tor-http-nio-10] c.netflix.config.ChainedDynamicProperty: EUREKA-RIBBON.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 21474836472019-10-12 16 40 V 36.012 INFO 97725-[tor-http-nio-10] c.n.l.DynamicServerListLoadBalancer: DynamicServerListLoadBalancer for client EUREKA-RIBBON initialized: DynamicServerListLoadBalancer: {NFLoadBalancer:name=EUREKA-RIBBON Current list of Servers= [eureka1.server.com:8901], Load balancer stats=Zone stats: {defaultzone= [Zone:defaultzone Instance count:1; Active connections count: 0; Circuit breaker tripped count: 0; Active connections per server: 0;]}, Server stats: [Server:eureka1.server.com:8901; Zone:defaultZone; Total Requests:0; Successive connection failure:0; Total blackout seconds:0; Last connection made:Thu Jan 01 08:00:00 CST 1970; First connection made:Thu Jan 01 08:00:00 CST 1970; Active Connections:0 Total failure count in last (1000) msecs:0; average resp time:0.0; 90 percentile resp time:0.0; 95 percentile resp time:0.0; min resp time:0.0; max resp time:0.0; stddev resp time:0.0]} ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@19148ab
I can request eureka-feign in the same way, and the result is as follows:
At this point, the default routing rules based on service discovery are built.
1.6 Custom routing rules 1.6.1 modify application.yml configuration server: port: 8100spring: application: name: spring-gateway cloud: gateway:# discovery:# locator:# enabled: true # enable the function of automatically creating routes based on serviceId through the service center routes:-id: ribbon-route uri: lb: / / EUREKA-RIBBON order: 0 predicates:-Path=/ribbon/** filters:-StripPrefix=1 # remove the prefix For more information, please see StripPrefixGatewayFilterFactory-AddResponseHeader=X-Response-Default-Foo, Default-Bar-id: feign-route uri: lb://EUREKA-FEIGN order: 0 predicates:-Path=/feign/** filters:-StripPrefix=1-AddResponseHeader=X-Response-Default-Foo Default-Bareureka: instance: hostname: eureka1.server.com lease-renewal-interval-in-seconds: 5 lease-expiration-duration-in-seconds: 10 client: service-url: defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
StripPrefix: accepts a non-negative integer, and the corresponding implementation is StripPrefixGatewayFilterFactory, which is used to remove the prefix and the integer corresponds to the number of layers. For the / ribbon/sayHello accessed in this example, the gateway service removes the request received by / ribbon,eureka-ribbon when it forwards the request backward: / sayHello. Eureka-feign is the same.
1.6.2 start the service
Visit http://localhost:8100/ribbon/sayHello and http://localhost:8100/feign/feign/sayHello, as shown in the following figure:
Ribbon:
Feign:
1.6.3 Custom Configuration
Spring Cloud Gateway also supports the routing definition of java's streaming api, which can be used with application.yml.
Package spring.cloud.demo.spring.gateway.config;import org.springframework.cloud.gateway.route.RouteLocator;import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration @ Configurationpublic class RoutesConfig {@ Bean public RouteLocator routeLocator (RouteLocatorBuilder routeLocatorBuilder) {return routeLocatorBuilder.routes () .route (r-> r.path ("/ ribbon/**") .fi lters (f-> f.stripPrefix (1) .addRequestHeader ("X-Response-Default-Foo") "Default-Bar") .uri ("lb://EUREKA-RIBBON") .order (0) .id ("ribbon-route")) .build () }} the above content is how to implement Gateway routing gateway in spring cloud 2.x. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.