Spring Cloud:从配置服务器加载消息源 [英] Spring Cloud : Load Message Sources from config server
问题描述
我正在处理包含客户端,注册表和配置服务器的Spring云项目(Spring Boot + Eureka API),因此我需要从Config Server加载Message属性:
I'm working on Spring cloud project (Spring Boot + Eureka API ) that contains client , registry and a config server , so I need to load Message properties from the config Server :
我已经有一个配置服务器,其application.properties配置良好,可以从客户端服务器上获得.
I have already a config server with application.properties well configured and available from client server .
客户端微服务中我当前的MessageSource Bean:
My current MessageSource Bean in the client Micro-service:
@Configuration
public class Config {
@Bean
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:/messages/messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
}
推荐答案
如果要从配置服务中加载它们,只需指向它的配置路径即可.
If you want to load them from the config service, you just need to point the config path for it.
此类的值是从bootstrap.yml中读取的.
The values on this class are read from the bootstrap.yml.
在application.yml级别上有一个名为"locale"的文件夹,其中包含文件"messages_en_GB.properties".
I have a folder called "locale" on the application.yml level, with the file "messages_en_GB.properties" inside.
Structure:
application.yml
locale (folder)
messages_en_GB.properties
@Configuration
public class MessageConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(MessageConfig.class);
@Value("${spring.cloud.config.uri}")
private String cloudUri;
@Value("${spring.cloud.config.label}")
private String cloudLabel;
@Value("${spring.profiles.active}")
private String profile;
@Value("${spring.cloud.config.enabled:false}")
private boolean cloudEnabled;
@Bean
@RefreshScope
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource =
new ReloadableResourceBundleMessageSource();
messageSource.setBasename(buildMessageLocation());
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
private String buildMessageLocation() {
if (Strings.isNullOrEmpty(cloudUri) || Strings.isNullOrEmpty(profile) || Strings
.isNullOrEmpty(cloudLabel) || !cloudEnabled) {
LOGGER.info("The cloud configuration is disabled, using local messages properties file");
return "classpath:locale/messages";
}
return cloudUri + "/" + profile + "/" + profile + "/" + cloudLabel + "/locale/" + "messages";
}
这篇关于Spring Cloud:从配置服务器加载消息源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!