当前位置: 首页 > 知识库问答 >
问题:

无法基于活动Spring配置文件生成不同的spring-cloud-gateway路由

瞿健
2023-03-14

我有两个简介,dev和Prod。我的application.yml文件如下所示

spring:
  profiles:
    default: prod
management:
  endpoint:
    health:
      enabled: true
  endpoints:
    web:
      exposure:
        include: hystrix.stream, gateway
---
spring
  profiles: prod
  cloud:
    gateway:
      routes:
        - id: test_route
          uri: http://foo.mycompany.com
          predicates:
           - Path=/status
           - Method=GET
---
spring
  profiles: dev
  cloud:
    gateway:
      routes:
        - id: test_route
          uri: http://localhost:8080
          predicates:
           - Path=/status
           - Method=GET

我的理解是:

  • Spring.profiles.default属性告诉Spring,如果没有指定配置文件,则使用prod配置文件
  • Spring将----视为“文件分隔符”,如果spring.profiles参数的计算结果为true,则重新计算每组属性并覆盖以前的值

鉴于这种理解,我希望Spring首先解析“默认”属性,了解默认激活的配置文件应该是prod。然后它将解析prod属性。由于“prod”是一个活动配置文件(唯一的活动配置文件,是缺省配置文件),它应该解析并应用prod路由。然后,它将解析dev路由,但识别dev不是活动配置文件,并且不覆盖这些值。这是我从阅读关于如何根据环境更改配置的文档中得到的理解。

但是,当我加载它,并点击actuatorendpoint时--/actuator/gateway/routes--我会回到[],在这里我希望看到prod路由。我在日志中确实看到prod概要文件被激活了,但似乎在顶部的“default”部分中没有属性会导致解析器从概要文件部分读取这些属性时无法应用它们。

我尝试的另一件事是将“dev”属性作为默认值,然后尝试使用“prod”配置文件属性覆盖URI。类似的问题也发生了--我击中了执行器endpoint并返回了路由,但它们只是默认情况下的dev路由。

  • spring-cloud-gateway 2.0.1.build-snapshot(为了解决此bug的问题,可能与此无关)
    • Spring-Cloud-starter-gateway
    • spring-boot-starter-webflux
    • Spring-启动-起动器-执行器

    (出于政治原因,我不能使用Spring Cloud Config。我公司的首席架构师有严重的此处未发明综合症。)

共有1个答案

申屠瀚海
2023-03-14

不能在属性文件中使用spring.profiles.default。现在设置这样的值就太晚了。因此可以使用程序参数(或系统属性)设置它。例如。

java -jar --spring.profiles.default=dev your-app.jar

也可以在代码中对默认配置文件进行硬编码:

@SpringBootApplication
public class DemoApplication {
  public static void main(String[] args) {
    SpringApplication application = new SpringApplication(DemoApplication.class);
    ConfigurableEnvironment environment = new StandardEnvironment();
    environment.setDefaultProfiles("dev");
    application.setEnvironment(environment);
    application.run(args);
  }
}

通过阅读所有评论,您可以在这里找到一些相关信息:https://github.com/spring-projects/spring-boot/issues/1219

 类似资料:
  • 以下是配置: 当我们公开actuator refreshendpoint并运行actuator/refresh时,路由配置将被重新加载,不会出现任何问题。这里 当我只点击/gateway/refreshendpoint时,它似乎没有做任何事情,但当我点击/actuator/refreshendpoint时,路由会从Spring Cloud Config重新加载到Spring Cloud Gatew

  • 我有Spring启动应用程序。我想为SecurityConfig创建两个配置文件:dev,prod。第一次尝试是从WebSecurityConfigurerAdapter扩展两个类,但是我在SecurityConfig类中创建了两个bean。我的配置如下所示: 也为运行应用程序类: BeanCreationException:在org.springframework.security.config

  • 我正在使用开发一个网关项目。我可以看到重试过滤器已经存在于项目的源代码中。 但是没有关于如何通过yml Congigurations来配置它的文档。任何一个与它一起工作或与您的项目集成。谁能提供任何线索。

  • 我找不到任何将Spring Cloud Sleuth与Spring Cloud Gateway集成的示例。想要一个成功集成的示例项目。

  • 下面是一个示例 谢谢,塞吉

  • 当我使用这样的routes statique时,我正在使用Spring cloud gateway(它工作得很好): 但是,当我想将routes Dynamicque与DiscoveryClientRoutedDefinitionLocator一起使用时,我的功能是: 这是我的申请表。属性: 当我叫这个urihttp://localhost:8888/PRODUCT-SERVICE/product