Spring Cloud Config 的简单配置应用

一、Config Server配置

1. 添加配置中心pom文件

<dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-config-server</artifactId>
</dependency>

2. 在启动类上添加配置中心服务端注解

@SpringBootApplication
@EnableConfigServer
public class Application {

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

3. 创建配置文件项目

创建文件夹并新建各个环境所需要的配置文件

该项目是需要推送到远程仓库的。推送教程

4. Config Server 的yml配置:

# 端口号
server:
  port: 9999
# 服务名称
spring:
  application:
    name: configServer
  cloud:
    config:
      server:
        git:
# 服务的git仓库地址
          uri: https://gitee.com/CSAT/SpringCloudConfig.git
# 配置文件所在的目录
          search-paths: common-redis
# 是否强制拉取
#          force-pull: true
# 指定文件缓存目录
#          basedir: config-repo/config-repo
# 配置文件所在的分支
      label: master
# git仓库的用户名
      username: 
# git仓库的密码
      password: 
eureka:
  client:
# 注册地址
    eureka-server-u-r-l-context: http://peer1:8671/eureka/

(我在自己弄的时候,用的是码云的仓库,仓库是私有的时候配置到yml里,应用启动会报错。)

错误:

Authentication is required but no CredentialsProvider has been registered

把码云的库开源后就没这个错了。

5. 验证Config Server是否配置成功

http://localhost:9999/application-dev.yml

验证方式:http://IP地址:端口号/文件名

如果根据上边的访问显示出配置中心文件里的数据,则表示Config Server配置成功。

二、Config Client配置

1. Config Client pom配置

<dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
</dependency>
<dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2. 启动类配置

@SpringBootApplication
public class Application {

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

3. application.yml文件和bootstrap.yml文件配置

application.yml:

spring:
  application:
# 应用名称
    name: application01-client
# Eureka
# 应用默认端口
server:
  port: 8762
eureka:
  client:
# 是否对自己进行注册(默认会对自己注册)
#    register-with-eureka: false
# 客户端是否获取eureka服务器注册表上的注册信息,默认为true
#    fetch-registry: false
# 默认注册地址
    eureka-server-u-r-l-context: http://peer1:8761/eureka/

bootstrap.yml:

# 服务名称
spring:
  cloud:
    config:
# 配置文件所在的分支
      label: master
# 配置的文件服务环境
      profile: prod
      name: configServer
# 开启配置服务发现
#      discovery:
#        enabled: true
#        service-id: configServer
      uri: http://localhost:9999/

Config Client读取不到配置中心配置的原因:

(1)Config Server和Config Client应用名称不一样导致的,通过配置相同的应用名称即可解决。

(2)Config Client 的yml文件里没有配置 Config Server应用名称,可以通过 spring.cloud.config.name 属性来进行配置。

通过以上两种方式都可以解决 Config Client读取不到配置中心配置。

4. 编写controller类,验证是否能够读取到配置中心文件的属性值

@RestController
public class VersionController {

	@Value("${version}")
	private String version = "开发环境";

	@RequestMapping("/test")
	public String test() {
		return "当前环境:" + version;
	}
}

通过浏览器访问显示出配置中心文件里的属性值即说明成功。

注意我在写的时候Config Server和Config Client都通过Eureka进行了注册,方便使用,所以大家写的时候如果要进行注册,记得在启动类上加上Eureka客户端的注解@EnableEurekaClient

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值