Spring 有明确的加载外部化配置的顺序。
然而,来自Spring Cloud Config 的配置似乎有明显的遗漏。有谁知道 Spring Cloud Config 在上面的位置
此问题出现在Spring-Data发行版2中.在最新版本1.13.9(及更早版本)中,它运行正常.
控制器代码:
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
@RequestMapping(value = "sorttest", method = RequestMethod.GET)
public Page<Integer> getDummy() {
return new PageImpl<>(Collections.singletonList(1), new PageRequest(0, 5, new Sort("asdf")), 1);
}
}
Run Code Online (Sandbox Code Playgroud)
Spring-Data 2风格相同:
Pageable pageable = PageRequest.of(0, 10, new Sort(Sort.Direction.ASC, "asd"));
PageImpl<Integer> page = new PageImpl<Integer>(Lists.newArrayList(1,2,3), pageable, 3);
return page;
Run Code Online (Sandbox Code Playgroud)
组态:
@SpringBootApplication
@EnableWebMvc
@EnableSpringDataWebSupport
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
还尝试了简单的Spring应用程序,没有带有Java配置的Spring Boot以及XML配置.结果是一样的:
{
"content": …Run Code Online (Sandbox Code Playgroud) 我们使用环境变量来配置 Spring Boot 应用程序中的各种属性。我发现FOO_BAR_BAZ通过@Value(${foo.bar.baz})在 Spring Boot 1.4.3.RELEASE / Spring 4.3.5.RELEASE 中开箱即用地绑定环境变量。
然而,从我在[宽松的绑定文档][1]中读到的内容来看,这似乎仅对@ConfigurationProperties.
所以我的问题是,我是否必须期望观察到的行为(如上所述)会在未来的版本中消失?
[1]: https: //docs.spring.io/spring-boot/docs/1.4.3.RELEASE/reference/htmlsingle/#boot-features-external-config-relaxed-binding,https : //docs.spring .io/spring-boot/docs/1.4.3.RELEASE/reference/htmlsingle/#boot-features-external-config-vs-value
我正在编写spring boot应用程序,它使用spring 配置,部署在关键的云代工厂上,并由Netflix Eureka作为发现服务/负载均衡器公开。
我创建了一个 bean,如下所示:
@Component
@ConfigurationProperties("config")
@RefreshScope
@Data
public class GeneralProperties {
private boolean ignoreEvent;
}
Run Code Online (Sandbox Code Playgroud)
/refresh在更改配置存储库中的实际属性后调用 Eureka 公开的应用程序路由时,@refreshScope注释的值已更改(以响应状态结束该字段),这意味着它正常工作。
当在云上运行同一应用程序的多个实例并调用/refresh.
beeing 使用的路由是 Eureka 公开的路由,它使用它的负载均衡器将调用路由到可用实例之一。
这会导致意想不到的结果,即并非所有实例都使用属性的最新更改进行更新。
任何建议如何将更改应用于所有实例?
我有一个模块化Spring Boot项目。作为数据库模式管理器,我想使用Flyway. 如前所述,该项目是模块化的。这是因为会有使用不同模块的不同配置。这意味着,我想将与模块相关的所有内容打包到其特定项目中。有了Flyway这个似乎没那么简单。
我理想中的想象:
ApplicationA
|
|_Module1
| |
| |_db/migration
| |
| |_V1__InitModule1Tables
| |_V2__MigrateSomeTable
|
|_Module2
|
|_db/migration
|
|_V1__InitModule2Tables
|_V2__MigrateSomeTable
Run Code Online (Sandbox Code Playgroud)
每个模块都独立定义自己的飞行脚本,因为无论如何它们都不知道彼此的存在。每个模块显然都需要在共享数据库中拥有自己的飞行历史表。这样,整个系统解耦和配置下一个应用程序ApplicationB使用Module1,并Module3不会是一个麻烦。
好吧,我没有找到Flyway达到此解决方案的任何配置可能性。
在每个模块中做这样的事情显然是一个坏主意,因为 bean 的初始化/执行顺序是相当随机的,导致当我需要它们用于其他配置时没有创建表。也显得凌乱。
@Configuration
public class Module1Config {
@Autowired
public void flyway(DataSource dataSource) {
Flyway flyway = new Flyway();
flyway.setBaselineOnMigrate(true);
flyway.setTable(flyway.getTable() + "-module1");
flyway.setDataSource(dataSource);
flyway.migrate();
}
}
Run Code Online (Sandbox Code Playgroud)
我不认为我是第一个试图实现这一目标的人。我怎样才能达到所需的模块化Flyway配置?
以下解决方案以重复主题所建议的方式工作,对我有用:
我在我的base …
modularity database-migration flyway spring-boot spring-config
我正在使用Spring Boot,并有两个非常相似的服务,我想在其中配置application.yml。
配置大致如下所示:
serviceA.url=abc.com
serviceA.port=80
serviceB.url=def.com
serviceB.port=8080
Run Code Online (Sandbox Code Playgroud)
是否可以创建一个注有注释的类@ConfigurationProperties并在注入点设置前缀?
例如
@Component
@ConfigurationProperties
public class ServiceProperties {
private String url;
private String port;
// Getters & Setters
}
Run Code Online (Sandbox Code Playgroud)
然后在服务本身中:
public class ServiceA {
@Autowired
@SomeFancyAnnotationToSetPrefix(prefix="serviceA")
private ServiceProperties serviceAProperties;
// ....
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我没有在文档中找到有关此功能的任何信息...非常感谢您的帮助!
我正在使用Spring Config在git服务器中共享一些Spring Boot微服务的配置。
效果很好,但是当我旅行时,有时不得不离线工作。
我已经配置了Spring Config微服务本地配置文件以从本地git(文件:)获取配置,而不是HTTP git服务器,因此我可以更改配置和测试,而无需访问主git服务器。
问题是由于我无法执行“ git push”将更改推送到主存储库,Spring Confgig记录了该内容并显示以下消息:
The local repository is dirty or ahead of origin. Resetting it to origin/master.
Run Code Online (Sandbox Code Playgroud)
并重置它,以删除我的最近一次本地提交以及最后一次配置更改。
我如何才能使Spring Config仅仅在本地git中获得最后提交的配置,而忽略是否将其推送到主服务器?
我当时正在做一个关于 Spring Boot 1.5 的项目。它具有以下类型的配置类:
@Configurtion
public class Foo{
@Autowired
private DependencyA dependencyA;
@Bean
public DependencyA getDependency(){
return new DependencyAImpl();
}
}
Run Code Online (Sandbox Code Playgroud)
这在 Spring Boot 1.5 中工作得很好,但是当我升级到 Spring Boot 2 时,这不再工作,应用程序将无法启动,并出现异常“找不到 DependencyA 类型的 bean”。我认为这可能是因为 Spring 尝试注入依赖项时未创建 bean,因此,作为“黑客”,将 @Lazy 添加到 dependencyA 注入中。这有效。
鉴于此,配置类的执行顺序是怎样的?是:A)首先创建bean,然后注入依赖项B)尝试创建具有所有依赖项的实例,然后在配置中创建任何bean。
我的应用程序在没有 spring boot 的情况下使用 spring web mvc 框架运行。现在我想使用 spring session JDBC 将会话存储到应用程序使用的数据库中。我在网上找到的所有例子都是使用spring boot的,如果不使用spring boot,他们使用的数据源配置是EmbeddedDatabase这样的:
@Bean
public EmbeddedDatabase dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("org/springframework/session/jdbc/schema-h2.sql").build();
}
Run Code Online (Sandbox Code Playgroud)
我有使用 HikariCP 的数据源配置,我希望 spring 会话使用此数据源配置。
@Bean
public DataSource dataSource() {
HikariConfig config = new HikariConfig();
config.setDriverClassName(env.getRequiredProperty("jdbc.driver"));
config.setJdbcUrl(env.getRequiredProperty("jdbc.url"));
config.setUsername(env.getRequiredProperty("jdbc.username"));
config.setPassword(env.getRequiredProperty("jdbc.password"));
config.setMinimumIdle(env.getRequiredProperty("jdbc.pool.minimumIdle", Integer.class));
config.setMaximumPoolSize(env.getRequiredProperty("jdbc.pool.maximumPoolSize", Integer.class));
config.addDataSourceProperty("cachePrepStmts", env.getRequiredProperty("jdbc.prop.cachePrepStmts"));
config.addDataSourceProperty("prepStmtCacheSize", env.getRequiredProperty("jdbc.prop.prepStmtCacheSize"));
config.addDataSourceProperty("prepStmtCacheSqlLimit", env.getRequiredProperty("jdbc.prop.prepStmtCacheSqlLimit"));
HikariDataSource ds = new HikariDataSource(config);
return ds;
}
Run Code Online (Sandbox Code Playgroud)
如何使用我当前的配置与 spring 会话集成?
我有这个配置:
@Configuration
public class KafkaTopicConfig {
private final TopicProperties topics;
public KafkaTopicConfig(TopicProperties topics) {
this.topics = topics;
}
@Bean
public NewTopic newTopicImportCharge() {
TopicProperties.Topic topic = topics.getTopicNameByType(MessageType.IMPORT_CHARGES.name());
return new NewTopic(topic.getTopicName(), topic.getNumPartitions(), topic.getReplicationFactor());
}
@Bean
public NewTopic newTopicImportPayment() {
TopicProperties.Topic topic = topics.getTopicNameByType(MessageType.IMPORT_PAYMENTS.name());
return new NewTopic(topic.getTopicName(), topic.getNumPartitions(), topic.getReplicationFactor());
}
@Bean
public NewTopic newTopicImportCatalog() {
TopicProperties.Topic topic = topics.getTopicNameByType(MessageType.IMPORT_CATALOGS.name());
return new NewTopic(topic.getTopicName(), topic.getNumPartitions(), topic.getReplicationFactor());
}
}
Run Code Online (Sandbox Code Playgroud)
我可以将 10 个不同的主题添加到 TopicProperties. 而且我不想手动创建每个类似的 bean。是否存在某种方法可以在spring-kafka或仅spring 中创建所有主题?
spring-config ×10
spring-boot ×8
spring ×7
java ×3
apache-kafka ×1
flyway ×1
git ×1
modularity ×1
spring-data ×1
spring-kafka ×1