关于springcloud:Spring-Cloud基础之Config-Server配置中心简单搭建

121次阅读

共计 4360 个字符,预计需要花费 11 分钟才能阅读完成。

首先须要在 Discovery Service 章节中的创立 Discovery Server 服务
创立 Config Server 服务

// 导入包,理论是通过 Spring Initializr 导入的,Eureka Discovery,Config Server,Actuactor
spring-cloud-config-server
spring-cloud-starter-netflix-eureka-server
spring-boot-starter-actuator
// 配置启动类注解 EnableConfigServer
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args);
    }
}
// 配置文件
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/bu6030/scf-config-repository.git
spring.application.name=config-server
eureka.client.service-url.defaultZone=http://localhost:8761/eureka

启动时须要默认的 SpringCloudserver 曾经启动
通过 url 能够找到对应的配置内容

// 配置的 Endpoint 规定几种形式
GET /{application}/{profile}[/{label}]
/myap/dev/master,/myapp/prod/v2,/myapp/default
GET /{application}-{profile}.(yml | properties)
/myapp-dev.yml,/myapp-prod.properties,/myapp-default.properties
GET /{label}-{application}-{profile}.(yml | properties)
/master/myapp-dev.yml,/v2/myapp-prod.properties,/master/myapp-default.properties

//http://localhost:8888/config-client-app.yml 返回内容:some:
  other:
    property: global
  property: app specific overridden value
//http://localhost:8888/config-client-app.properties 返回内容
some.other.property: global
some.property: app specific overridden value
//http://localhost:8888/config-client-app/prod 返回内容
{
    "name": "config-client-app",
    "profiles": ["prod"],
    "label": null,
    "version": "1cb37c9ba1fa4c17d2ae7489c2e592eba1688547",
    "state": null,
    "propertySources": [
        {
            "name": "https://github.com/bu6030/scf-config-repository.git/config-client-app-prod.properties",
            "source": {
                "some.property": "profile specific value",
                "some.other.property": "profile specific value"
            }
        },
        {
            "name": "https://github.com/bu6030/scf-config-repository.git/config-client-app.properties",
            "source": {"some.property": "app specific overridden value"}
        },
        {
            "name": "https://github.com/bu6030/scf-config-repository.git/application.properties",
            "source": {
                "some.property": "global",
                "some.other.property": "global"
            }
        }
    ]
}
//http://localhost:8888/config-client-app/default 返回内容
{
    "name": "config-client-app",
    "profiles": ["default"],
    "label": null,
    "version": "1cb37c9ba1fa4c17d2ae7489c2e592eba1688547",
    "state": null,
    "propertySources": [
        {
            "name": "https://github.com/bu6030/scf-config-repository.git/config-client-app.properties",
            "source": {"some.property": "app specific overridden value"}
        },
        {
            "name": "https://github.com/bu6030/scf-config-repository.git/application.properties",
            "source": {
                "some.property": "global",
                "some.other.property": "global"
            }
        }
    ]
}

配置 config client

// 导入包,理论是通过 Spring Initializr 导入的,Eureka Discovery,Config Client,Actuactor
sspring-cloud-starter-config
spring-cloud-starter-netflix-eureka-client
spring-boot-starter-actuator
// 配置启动类注解 EnableConfigServer
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ConfigClientAppApplication {
    @Autowired
    private ConfigClientAppConfiguration properties;
    @Value("${some.other.property}")
    private String someOtherPorperty;
    public static void main(String[] args) {SpringApplication.run(ConfigClientAppApplication.class, args);
    }
    @RequestMapping("/")
    public String printConfig(){StringBuffer sb = new StringBuffer();
        sb.append(properties.getProperty());
        sb.append("||");
        sb.append(someOtherPorperty);
        return sb.toString();}
}
@Component
@ConfigurationProperties(prefix="some")
public class ConfigClientAppConfiguration {
    private String property;
    public String getProperty() {return property;}
    public void setProperty(String property) {this.property = property;}
}
// 配置文件
server.port=8080
spring.application.name=config-client-app
spring.cloud.config.discovery.enabled=true
eureka.client.service-url.defaultZone=http://localhost:8761/eureka

批改配置文件后主动刷新通过调用 http://localhost:8080/actuato… 刷新接口
配置文件须要减少

management.endpoints.enabled-by-default=true
management.endpoints.web.exposure.include=refresh,health,bus-refresh
// 启动类减少 RefreshScope 后,@Value 也能够刷新
@SpringBootApplication
@EnableDiscoveryClient
@RestController
@RefreshScope
public class ConfigClientAppApplication {
    @Autowired
    private ConfigClientAppConfiguration properties;
    @Value("${some.other.property}")
    private String someOtherPorperty;
    public static void main(String[] args) {SpringApplication.run(ConfigClientAppApplication.class, args);
    }
    @RequestMapping("/")
    public String printConfig(){StringBuffer sb = new StringBuffer();
        sb.append(properties.getProperty());
        sb.append("||");
        sb.append(someOtherPorperty);
        return sb.toString();}
}
// 刷新服务的形式,http://localhost:8080/actuator/refresh
http://localhost:8080/actuator/bus-refresh

正文完
 0