spring cloud config

Posted rigidwang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring cloud config相关的知识,希望对你有一定的参考价值。

1.设置git

https://git.oschina.net/rigid/hr.git

上传如下文件:

hr_config/my-client.yml

hr_config/my-client-uat.yml

2.增加config server

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class SpringCloudServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudServerApplication.class, args);
    }
}

启动之后访问:
http://localhost:8888/my-client/master

3.增加config client

package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope 
public class MySampleRestController {
     @Value("${my-config.appName}")
    private String appName;
    
    @RequestMapping("/app-name")
    public String getAppName() {
        return appName;
    }
    
}
package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class SpringCloudClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudClientApplication.class, args);
    }
    
    @Autowired
    void setEnvironment(Environment env) {
        System.out.println("my-config.appName from env: " + env.getProperty("my-config.appName"));
    }
}

#本地访问
http://localhost:8080/app-name

#刷新
curl -X POST http://localhost:8080/refresh

  




以上是关于spring cloud config的主要内容,如果未能解决你的问题,请参考以下文章

无法通过 spring.cloud.config.enabled:false 禁用 Spring Cloud Config

0701-spring cloud config-简介Config Server开发Config Client开发

问题 spring-cloud-config 和 spring-cloud-bus

spring-cloud-config——Quick Start

spring cloud config 8888端口可以变么

spring cloud 学习 - config server