I am using spring cloud gateway as a client for spring cloud config server. I want to put routes specific to a microservice into a specific repo. So that each microservice can have independent git repo for routes. but doing so override one route over another when gateway fetch the configuration from config server. How can I solve this issue. I am using composite property to combine the repos.
Comment From: spencergibb
It's not possible
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: ShahzebAnsari
Hi, We have solved this problem. The idea was to add different prefix in each git repo routes to avoid overriding.
e.g. Repo1 first: routes: - id: abc uri: http://localhost:8081/ predicates: -Path= /my/api
Repo2 second: routes: - id: def uri: http://localhost:8082/ predicates: -Path= /api/data
To allow gateway use these routes we looked into GatewayProperties class and PropertiesRouteDefinitionLocator class.
We created our own Properties class same as GatewayProperties class with same content but we removed @ConfigurationProperties(prefix = "spring.cloud.gateway") annotation from it. Then we extended Properties class for different prefixes like @ConfigurationProperties(prefix = "first") and @ConfigurationProperties(prefix = "second").
Then we used MyRouteDefinitionLocator class same as PropertiesRouteDefinitionLocator class for using the routes of all extended classes.
Here are the classes: Properties.class
public class Properties { private final Log logger = LogFactory.getLog(getClass());
private List<RouteDefinition> routes = new ArrayList<>();
private List<FilterDefinition> defaultFilters = new ArrayList<>();
public List<RouteDefinition> getRoutes() {
return routes;
}
public void setRoutes(List<RouteDefinition> routes) {
System.out.println("in service properties");
this.routes = routes;
if (routes != null && routes.size() > 0 && logger.isDebugEnabled()) {
logger.debug("Routes supplied from Gateway Properties: " + routes);
}
}
public List<FilterDefinition> getDefaultFilters() {
return defaultFilters;
}
public void setDefaultFilters(List<FilterDefinition> defaultFilters) {
System.out.println("in service properties");
this.defaultFilters = defaultFilters;
}
@Override
public String toString() {
return new ToStringCreator(this).append("routes", routes)
.append("defaultFilters", defaultFilters)
.toString();
}
}
Extended Classes
@Configuration @ConfigurationProperties(prefix="first") public class FirstProperties extends Properties{
}
@Configuration @ConfigurationProperties(prefix="second") public class SecondProperties extends Properties{
}
MyRouteDefinitionLocator
@Component public class MyRouteDefinitionLocator implements RouteDefinitionLocator {
private List<Properties> propertiesList;
@Autowired
public void setPropertiesList(List<Properties> propertiesList){
this.propertiesList = propertiesList;
}
@Override
public Flux<RouteDefinition> getRouteDefinitions() {
List<RouteDefinition> routes = new ArrayList<>();
for(Properties properties: propertiesList){
routes.addAll(properties.getRoutes());
}
return Flux.fromIterable(routes);
}
}