【问题标题】:Config with Spring Cloud Config使用 Spring Cloud Config 进行配置
【发布时间】:2015-05-04 14:37:28
【问题描述】:

我想问两个关于Spring Cloud Config的问题。

1) 是否可以实现 Spring Cloud Config Server 来恢复基础 mongodb 而不是 git?

2)当您在 Spring Cloud Config ServerSpring Cloud Config Client 设置会自动更新/strong>?

谢谢!!!

【问题讨论】:

    标签: java spring spring-cloud


    【解决方案1】:

    Spring Cloud Config Server MongoDB 现已在 Github 上提供。

    要启动并运行它,您只需添加 maven 配置,如下所示,将 @EnableMongoConfigServer 添加到您的 Spring Boot 应用程序配置并配置所需的 spring.data.mongodb.* 属性。

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server-mongodb</artifactId>
            <version>0.0.1.BUILD-SNAPSHOT</version>
        </dependency>
    </dependencies>
    
    <repositories>
        <repository>
            <id>ojo-snapshots</id>
            <name>OJO Snapshots</name>
            <url>https://oss.jfrog.org/artifactory/libs-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    

    然后你可以像这样向 MongoDB 添加配置文档:

    db.appname.insert({
       "label": "master",
       "profile": "prod",
       "source": {
            "user": {
                "max-connections": 1,
                "timeout-ms": 3600
            }
        }
    });
    

    并通过http://localhost:8080/master/appname-prod.properties 访问它们以获得这样的响应:

    user.max-connections: 1.0
    user.timeout-ms: 3600.0
    

    更新 我们已升级 spring-cloud-config-server-mongodb 以使用 spring-boot 1.5.7 快照。

    【讨论】:

    • 是否可以使用 couchbase 而不是 mongodb 来使用 spring cloud-config
    【解决方案2】:
    1. 是的,有可能,欢迎拉取请求
    2. 没有推送,但是可以使用spring的@Scheduled调用 RefreshEndpoint.refresh() 以间隔为基础。

    【讨论】:

    • 首先感谢您的回复,这涉及到我的另外两个疑问。 1)我如何取消bean的创建ConfigServerConfiguration$GitRepositoryConfiguration设置时出现那个错误,因为我不会像这样使用它取消。 2) 如何在我的 mongodb 案例中创建自定义 EnvironmentRepository
    • 查看这两个链接以获取帮助:github.com/spring-cloud/spring-cloud-config/blob/master/…github.com/spring-cloud/spring-cloud-config/blob/master/… 基本上,您使用不同的配置文件运行配置服务器,该配置文件创建 EnvironmentRepository 的 bean,就像其他人为 svn 所做的那样。
    【解决方案3】:

    不确定 1。 对于 2) 你有 spring-cloud-bus,当你在配置服务器中进行更改时,它可以自动向所有客户端提供推送通知。 http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html

    需要以下内容: 1.本地运行的RabbitMQ/Redis 2.在config server pom xml中添加这个依赖。使用 Brixton.M5 构建。

    <parent>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-parent</artifactId>
          <!-- <version>Brixton.BUILD-SNAPSHOT</version> -->
          <version>Brixton.M5</version>
          <relativePath /> 
        </parent>
    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-monitor</artifactId>
            <scope>test</scope>
        </dependency>
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
    

    3。除了您可能已经拥有的 spring-config-client 依赖项之外,还使用总线依赖项:

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-bus-amqp</artifactId>
            </dependency>
    

    4。 [POST]http://localhost:/monitor?path= - 这应该向客户端推送通知。或者,您可以使用 github webhook 在文件发生更改时自动发布。

    您可以参考帖子here

    【讨论】:

      【解决方案4】:

      1. 我推荐这个 git projecto 来做到这一点:https://github.com/spring-cloud-incubator/spring-cloud-config-server-mongodb

      2.首先,在进行所有更改后,如果您没有 @RefreshScope 标签,请将其添加到您的休息控制器,如下所示:

      @RefreshScope
      @RestController
      class MessageRestController {
         @Value("${message:Hello default}")
          private String message;
      
          @RequestMapping("/message")
          String getMessage() {
              return this.message;
          }
      

      接下来,您需要向客户端的刷新端点发送一个空的 HTTP POST,如下所示:

      http://localhost:8080/refresh
      

      注意:您可以在浏览器中使用 RESTclient 插件或 POSTMAN 来执行此操作。

      几秒后终于探测到新消息http://localhost:8080/message

      注意:此示例是默认配置的客户端...

      【讨论】:

        猜你喜欢
        • 2016-05-19
        • 2019-03-20
        • 2017-01-06
        • 2019-04-14
        • 2019-01-07
        • 2018-05-30
        • 2015-08-25
        • 2017-08-23
        • 2017-06-23
        相关资源
        最近更新 更多