
In this blog, we are going to see the dynamic configuration of Spring Cloud Gateway routing. The configuration of Spring Cloud Gateway can be control by a series of “RouteDefinitionLocator” interfaces, which are as follows:
public interface RouteDefinitionLocator {
Flux<RouteDefinition> getRouteDefinitions();
}
By default, through the @ ConfigurationProperties mechanism of SB, Spring Cloud Gateway uses PropertiesRouteDefinitionLocator to load the configuration information of routes from the configuration file. In the previous routing configuration, we have seen that a quick configuration method can be use. Specific parameter names can also be specified. For example, the following two configuration methods have the same effect:
spring:
cloud:
gateway:
routes:
- id: setstatus_route
uri: http://example.org
filters:
- name: SetStatus
args:
status: 401
- id: setstatusshortcut_route
uri: http://example.org
filters:
- SetStatus=401
In a few application scenarios of gateways, you’ll utilize property configuration. But in a few generation situations, a few steering arrangement data may come from other places, such as databases. Within the future form of Spring Cloud Portal, a few database RouteDefinitionLocator execution classes can be included, such as Redis and MongoDB
Fluent Java Routes API (use Java route api code to add route configuration):
Now let us look toward the code implementation of fluent java routes API:
// static imports from GatewayFilters and RoutePredicates
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder, AddRequestHeaderGatewayFilterFactory throttle) {
NameValueConfig requestHeader = new NameValueConfig();
requestHeader.setName("routes-a");
requestHeader.setValue("yes");
return builder.routes()
.route(r -> r.host("localhost:8080").and().path("/routes-api/a/**")
.filters(f ->{
f.addResponseHeader("X-TestHeader", "foobar");
return f.redirect(HttpStatus.MOVED_PERMANENTLY.value(), "http://www.xinyues.com");
})
.uri("http://localhost:8080").id("custom-1")
)
.route(r -> r.path("/routes-api/b/**")
.filters(f ->
f.addResponseHeader("X-AnotherHeader", "baz"))
.uri("http://localhost:8080").id("custom-2")
)
.route(r -> r.order(0)
.host("localhost:8080").and().path("/routes-api/c/**")
.filters(f -> f.filter(throttle.apply(requestHeader)))
.uri("http://localhost:8080").id("custom-3")
)
.build
In this form, you can also add some custom Predicate judgments. The Predicates defined in route definition locator can use logical and. With Fluent Java API, you can use and(), or(), and gate () to manipulate the Predicate class. For example, the first route in the above code uses and() to add two Predicates.
DiscoveryClient Route Definition Locator uses service discovery client to define route information:
It can use the service discovery client interface DiscoveryClient to obtain service registration information from the service attention center and then configure the corresponding routes. Note that you need to add the following configuration in the configuration to enable this function:
spring.cloud.gateway.discovery.locator.enabled=true
Configuring Predicates and Filters For DiscoveryClient Routeshttps://github.com/shivamm31/Dynamic-Configuration-of-Spring-Cloud-Gateway-Routing:
If you want to add a custom Predicate and filters, you can configure them as follows: spring.cloud.gateway.discovery.locator.predictions [x] and spring.cloud.gateway.discovery.location Or. Filters [y], when this configuration mode is used, the original default Predicate and Filter will not be retained. If you need the original configuration, you need to add it to the configuration manually, as shown below: application.properties
spring.cloud.gateway.discovery.locator.predicates[0].name: Path
spring.cloud.gateway.discovery.locator.predicates[0].args[pattern]: "'/'+serviceId+'/**'"
spring.cloud.gateway.discovery.locator.predicates[1].name: Host
spring.cloud.gateway.discovery.locator.predicates[1].args[pattern]: "'**.foo.com'"
spring.cloud.gateway.discovery.locator.filters[0].name: Hystrix
spring.cloud.gateway.discovery.locator.filters[0].args[name]: serviceId
spring.cloud.gateway.discovery.locator.filters[1].name: RewritePath
spring.cloud.gateway.discovery.locator.filters[1].args[regexp]: "'/' + serviceId + '/(?<remaining>.*)'"
spring.cloud.gateway.discovery.locator.filters[1].args[replacement]: "'/${remaining}'"
application.yml
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
predicates:
- name: Path
args:
pattern: "'/'+serviceId+'/test'"
filters:
- name: RedirectTo
args:
status: 301https://github.com/shivamm31/Dynamic-Configuration-of-Spring-Cloud-Gateway-Routing
url: "'http://www.xinyues.com'"
Note that in these configurations, it parses the values of all configurations by Spel. So if it is a string, you need a quotation. For example, in application.yml, the configuration is as follows. It will report an error.
application.yml: this configuration keeps the original default Predicate and Filter.
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
predicates:
- name: Path
args:
pattern: "'/'+serviceId+'/**'"
filters:
- name: AddRequestHeader
args:
name: "'foo'"
value: "'bar'"
- name: RewritePath
args:
regexp: "'/' + serviceId + '/(?<remaining>.*)'"
replacement: "'/${remaining}'"
source code implementation:
First, you have to download a project from here: https://github.com/shivamm31/Dynamic-Configuration-of-Spring-Cloud-Gateway-Routing
Modify application.yml to activate the discovery client configuration file
spring:
profiles:
active:
- discoveryclient
After that, you have to launch the Gateway project.



You can see that there is a default filter and predicate. We have seen that we configure routing information in the configuration file. The routing cohttps://github.com/shivamm31/Dynamic-Configuration-of-Spring-Cloud-Gateway-Routingnfiguration information added in the java code is all loaded into the gateway. This indicates that we can use the three ways to configure the routing information together.
Conclusion:
In the conclusion, in this blog we have leSo in conclusion, in this blog, we have learned how we can use the dynamic configuration of Gateway routing and also implemented with the help of java code. Thanks for reading this blog. I will cover more topics in the further blogs. Happy learning 🙂
For more, you can refer to the documentation: https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.1.0.RELEASE/single/spring-cloud-gateway.html
For more, you can refer to the documentation: https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.1.0.RELEASE/single/spring-cloud-gateway.html