SpringCloud config本地Native配置

SpringCloud config配置中心支持Git, SVN, native等方式存放配置文件, 本文主要讲解采用native的配置方式, 采用native配置方式主要是简单, 新项目上线不需要搭建git或svn, 直接将配置文件丢到服务器就可以了, 不使用集群的话可以考虑此方式,如果还有其他优点,欢迎评论区补充。

1 创建配置中心server: config-server

1.1 添加config-server的pom.xml相关依赖

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

1.2 添加config-server配置

server:
  port: 8102

spring:
  application:
    name: config-server
	# security 登录,因为存放在本地目录,可以通过url直接获取配置信息,存在安全隐患
  security:
    user:
      name: config
      password: vIcUSNsTlKRoMjgw
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
				#服务器地址
         search-locations: /home/test/native-config/{label} 
        bootstrap: true
eureka:
  instance:
    prefer-ip-address: true
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8101/eureka/

1.3 启动类ConfigServerApplication增加注解

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

注:启动时添加参数:-Dspring.profiles.active=native

2 创建配置中心client:config-client

2.1 添加config-client的pom相关依赖

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2.2 添加config-client配置bootstrap.properties

server.port=8104

spring.application.name=user
spring.profiles.active=dev
spring.main.allow-bean-definition-overriding=true

# config 配置
spring.cloud.config.profile=${spring.profiles.active}
spring.cloud.config.label=${spring.profiles.active}
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=config-server
spring.cloud.config.username=config
spring.cloud.config.password=vIcUSNsTlKRoMjgw

3 动态刷新config-server配置

3.1 引入依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

3.2 配置文件添加

management.endpoints.web.exposure.include=refresh,health,info

3.3 测试使用

在需要刷新的类添加注解: @RefreshScope

@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${info:error}")
    private String info;

    @RequestMapping("/config/info")
    public String info() {
        return info;
    }

}

3.4 执行

curl -X POST http://localhost:8084/actuator/refresh。注意域名和ip是客户端的。

参考文档:
https://www.springcloud.cc/spring-cloud-config.html
https://blog.csdn.net/acmman/article/details/106585866
https://blog.csdn.net/acmman/article/details/106440320

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值