简体   繁体   English

Spring Cloud:从配置服务器加载消息源

[英]Spring Cloud : Load Message Sources from config server

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 :我正在处理包含客户端、注册表和配置服务器的 Spring 云项目(Spring Boot + Eureka API),因此我需要从配置服务器加载消息属性:

I have already a config server with application.properties well configured and available from client server .我已经有一个具有 application.properties 配置良好的配置服务器,并且可以从客户端服务器获得。

My current MessageSource Bean in the client Micro-service:我当前在客户端微服务中的 MessageSource Bean:

@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.如果你想从配置服务加载它们,你只需要指向它的配置路径。

The values on this class are read from the bootstrap.yml.这个类的值是从 bootstrap.yml 中读取的。

I have a folder called "locale" on the application.yml level, with the file "messages_en_GB.properties" inside.我在 application.yml 级别有一个名为“locale”的文件夹,里面有文件“messages_en_GB.properties”。

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";
  }

I use embedding spring-cloud-config ( spring.cloud.config.server.bootstrap=true ), so I configured to download git repo to local folder near my runnable jar - build/config/backend我使用嵌入 spring-cloud-config ( spring.cloud.config.server.bootstrap=true ),所以我配置为将 git repo 下载到我的可运行 jar 附近的本地文件夹 - build/config/backend

spring:
   cloud:
       config:
           server:
               bootstrap: true
               git:
                   searchPaths: backend
                   basedir: build/config

And after I used such configuration for message resources (my resources in messages directory in files messages*.properties )在我将这样的配置用于消息资源之后(我在文件messages*.properties中的messages directory中的资源)

spring.messages.basename=file:${spring.cloud.config.server.git.basedir}/${spring.cloud.config.server.git.searchPaths}/messages/messages

and bean configuration和bean配置

    @Value("${spring.messages.basename}")
    private String basename;

    @Value("${spring.messages.cache-duration}")
    private Duration cacheDuration;

    @Value("${spring.messages.use-code-as-default-message}")
    private Boolean useCodeAsDefaultMessage;

    @Bean
    @Primary
    @RefreshScope
    public ReloadableResourceBundleMessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(basename)));
        messageSource.setDefaultEncoding(StandardCharsets.UTF_8.name());
        messageSource.setCacheMillis(cacheDuration.toMillis());
        messageSource.setUseCodeAsDefaultMessage(useCodeAsDefaultMessage);
        return messageSource;
    }

and after update configs from git repo it's also update message resources in application从 git repo 更新配置后,它也会更新应用程序中的消息资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM