Spring Cloud基础之Config Server配置中心简单搭建

荀恺
• 阅读 1079

首先需要在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
点赞
收藏
评论区
推荐文章
blmius blmius
4年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Wesley13 Wesley13
4年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Easter79 Easter79
4年前
springcloud eureka.instance
1.在springcloud中服务的 InstanceID默认值是:${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance\_id:${server.port}},也就是:主机名:应用名:应用端口。如图1
Stella981 Stella981
4年前
Kerberos无约束委派的攻击和防御
 0x00前言简介当ActiveDirectory首次与Windows2000Server一起发布时,Microsoft就提供了一种简单的机制来支持用户通过Kerberos对Web服务器进行身份验证并需要授权用户更新后端数据库服务器上的记录的方案。这通常被称为Kerberosdoublehopissue(双跃点问题),
Wesley13 Wesley13
4年前
FLV文件格式
1.        FLV文件对齐方式FLV文件以大端对齐方式存放多字节整型。如存放数字无符号16位的数字300(0x012C),那么在FLV文件中存放的顺序是:|0x01|0x2C|。如果是无符号32位数字300(0x0000012C),那么在FLV文件中的存放顺序是:|0x00|0x00|0x00|0x01|0x2C。2.  
Wesley13 Wesley13
4年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
4年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n
Wesley13 Wesley13
4年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Python进阶者 Python进阶者
2年前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
荀恺
荀恺
Lv1
没心没肺的活着,是我最想要的生活。
文章
4
粉丝
0
获赞
0