In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to achieve spring cloud getway routing configuration". In daily operation, I believe many people have doubts about how to achieve spring cloud getway routing configuration. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to achieve spring cloud getway routing configuration"! Next, please follow the editor to study!
1. Create a project getway-server
two。 Add pom dependencies:
Org.springframework.cloud spring-cloud-starter-gateway
3. Add Startup Class
4. Add a profile:
# Port server: port: 808 service name spring: application:name: getway-service cloud:gateway: routes:-id: product-service uri: lb://product-service # pull the service request path predicates:-Path=/product/** from the registry based on the service name
Id: our custom routing ID, which remains unique
Uri: destination service address
Predicates: routing condition. Predicate accepts an input parameter and returns a Boolean result. The interface contains multiple defaults
Identify ways to combine Predicate into other complex logic (such as and, or, not)
5. The error when starting the getway service is as follows:
This error is due to the fact that the getway is internally implemented through netty + webflux, and the webflux implementation conflicts with springmvc
Solution: add the following dependencies of the parent project configuration to the required project:
Org.springframework.boot spring-boot-starter-web
Correspondingly, add dependencies to the product and order services, and start the getway service again.
Visit http://localhost:8080/product/1
Rewrite forwarding path
In SpringCloud Gateway, route forwarding is directly after the matching route path is spliced directly to the mapped path (URI), then
It is often not so convenient in micro-service development. Here you can rewrite the path through the RewritePath mechanism.
(1) case transformation
Modify application.yml to change the matching path to / product-service/**
Restart the gateway and we will visit http://127.0.0.1:8080/product-service/product/1 in the browser and throw 404. this
It is because the routing forwarding rules are forwarded to the commodity micro-service (http://127.0.0.1:9002/productservice/product/1) path by default, and the commodity micro-service does not have the mapping configuration corresponding to product-service.
(2) add RewritePath rewrite forwarding path
Modify application.yml and add rewrite rules
# Service name spring: application:name: getway-service cloud:gateway: routes:-id: product-service# uri: http://127.0.0.1:9001 uri: lb://product-service # pull the service request path predicates:#-Path=/product/**-Path=/product-service/**filters from the registry based on the service name :-RewritePath=/product-service/ (? *) / $\ {segment} # configure route forwarding based on service name discovery: locator:enabled: true # enable automatic forwarding of service name lower-case-service-id: true # name in lowercase
Rewrite the forwarded url via RewritePath, rewrite / product-service/ (?. *) as {segment}, and then forward it to the order
Micro services. For example, if a http://localhost:8080/product-service/product is requested on a web page, the request will be forwarded to htt.
P://127.0.0.1:9002/product/1
Gateway current limit
The 1.pom file adds the following dependencies:
Org.springframework.boot spring-boot-starter-data-redis-reactive org.springframework.boot spring-boot-starter-actuator 2. Modify the configuration file server: port: 8080 # Port spring: application:name: gateway-service # Service name redis:host: localhostpool: 6379database: 0 cloud: # configure SpringCloudGateway routing gateway: routes:-id: order-serviceuri: lb://order-servicepredicates:-Path=/order-service/**filters:-RewritePath=/order-service/ (?. *) / $\ {segment}-id: product-serviceuri: lb://product-servicepredicates:-Path=/product-service/**filters:-name: RequestRateLimiter args:# # use SpEL to get the object from the container key-resolver:'# {@ pathKeyResolver}'# # token bucket filling average rate redis- per second Rate-limiter.replenishRate: upper limit of token bucket redis-rate-limiter.burstCapacity: 3-RewritePath=/product-service/ (?. *) / $\ {segment}
3. Create a new KeyResolverConfiguration class
@ Configurationpublic class KeyResolverConfiguration {/ * request path current limit rule * @ return * / @ Bean public KeyResolver pathKeyResolver () {return new KeyResolver () {public Mono resolve (ServerWebExchange exchange) {return Mono.just (exchange.getRequest (). GetPath (). ToString ());}};}}
Visit http://localhost:8080/product-service/product/1
If a continuous refresh request is made, the following interface appears
Through the MONITOR of reids, you can monitor the execution of redis. At this time, there will be corresponding data in Redis.
In curly braces is our current-limiting Key, here is IP, and the local one is localhost
Timestamp: stores the number of seconds in the current time, that is, System.currentTimeMillis () / 1000 or
Instant.now () .getEpochSecond ()
Tokens: stores the corresponding number of tokens available in the current second
Current limit according to parameters
1. Add the following code to the KeyResolverConfiguration class
/ * * request parameter current limit * @ return * / @ Beanpublic KeyResolver userKeyResolver () {return exchange-> Mono.just (exchange.getRequest (). GetQueryParams (). GetFirst ("userId") / / exchange.getRequest (). GetHeaders (). GetFirst ("X-Forwarded-For") based on the current limit of the request ip);}
two。 Modify the configuration file:
Access is the same as above, if the access frequency is too high, it will have the same effect.
Access address: http://localhost:8080/product-service/product/1?userId=1
You need to bring parameters.
At this point, the study on "how to implement spring cloud getway routing configuration" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.