问题
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 :
I have already a config server with application.properties well configured and available from client server .
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;
}
}
回答1:
If you want to load them from the config service, you just need to point the config path for it.
The values on this class are read from the bootstrap.yml.
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";
}
来源:https://stackoverflow.com/questions/55046062/spring-cloud-load-message-sources-from-config-server