spring-cloud / spring-cloud-gateway

An API Gateway built on Spring Framework and Spring Boot providing routing and more.
http://cloud.spring.io
Apache License 2.0
4.5k stars 3.31k forks source link

Add support for MVC Server actuator #3233

Open imdr0id opened 7 months ago

imdr0id commented 7 months ago

We're moving away from zuul to spring cloud gateway mvc. Earlier we used to get the routes with /actuator/routes endpoint, but now with gateway mvc I'm unable to get the list of routes defined via /actuator/gateway/routes.

I'm using the latest version of spring boot 3.2.2. please help!

application.yml

management:
  endpoint:
    gateway:
      enabled: true
  endpoints:
    web:
      exposure:
        include: '*'

dependencies:


<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
        <spring-cloud.version>2023.0.0</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway-mvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
oexos commented 7 months ago

I have the same issue with the same setup as you. If I use Spring Cloud Gateway Reactive Server then I can access the routes from /actuator/gateway/routes. If I use Spring Cloud Gateway Server MVC then I cannot access the routes from /actuator/gateway/routes.

This dependency doesn't work for /actuator/gateway/routes

org.springframework.cloud spring-cloud-starter-gateway-mvc

This dependency will work for /actuator/gateway/routes

org.springframework.cloud spring-cloud-starter-gateway
imdr0id commented 7 months ago

@oexos yeah, while using the reactive server this will work, But our current setup won't allow us to use reactive server, because we're deploying this as a war to an external tomcat. That's the reason why we opted for gateway mvc instead of reactive gateway.

spencergibb commented 6 months ago

Yes, there simply wasn't time to get it in before the initial release.

spencergibb commented 6 months ago

In the meantime, they are all available via RouterFunction beans. For an example see https://github.com/spring-cloud/spring-cloud-gateway/blob/67b612d4af1ad9882a95cd7db0025ef0c7633089/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/config/GatewayMvcPropertiesBeanDefinitionRegistrarTests.java#L143-L162

Gondagar commented 6 months ago

Please add an actuator to the MVC version.

imdr0id commented 6 months ago

@spencergibb : i was able to get the routes with the RouterFunction.

`

@Autowired
List<RouterFunction>  routerImplList;
private List<String> getRoutes() {
    AtomicReference<List<String>> routesList = new AtomicReference<>();
    routerImplList.forEach(a -> {
        if (a.getClass().getSimpleName().equals("DelegatingRouterFunction")) {
            a.accept(new AbstractRouterFunctionVisitor() {
                @Override
                @SuppressWarnings("unchecked")
                public void attributes(Map<String, Object> attributes) {
                    if (attributes.containsKey("gatewayRouterFunctions")) {
                        Map<String, RouterFunction> map = (Map<String, RouterFunction>) attributes
                                .get("gatewayRouterFunctions");
                        routesList.compareAndSet(null, map);
                    }
                }
            });
        }
    });
    return routesList.get();
}

`