四、SpringCloud之Config配置中心

一、SpringCloud Config介绍

Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。

客户端和服务器上的概念映射与Spring Environment和PropertySource抽象相同,因此它们与Spring应用程序非常契合,但可以与任何以任何语言运行的应用程序一起使用。随着应用程序通过从开发人员到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。

服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具。可以轻松添加替代实现,并使用Spring配置将其插入。

本教程使用的配置是本地,并非在git上。

二、SpringCloud Config搭建

首先我们需要建3个服务,Eureka server、config server、config client。

Eureka的搭建就不介绍了,详情看 SpringCloud之Eureka服务注册中心 文章。

2.1、config server搭建

新建config server项目neil-config-server

在pom.xml里面添加Eureka client和config的依赖

		<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>

配置文件application.yml

server:
  port: 8995
  servlet:
    context-path: /
spring:
  application:
    name: neil-config-server
  profiles:
    active: native    #使用本地配置,非git
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8989/eureka/ #Eureka Server地址
  instance:
    prefer-ip-address: true  #Hostname使用主机ip

然后在resources目录下创建config目录,然后新建2个配置文件用来测试

请添加图片描述

config-dev.yml

name: ZhangSan
age: 18
env: dev

config-uat.yml

name: ZhangSan
age: 18
env: uat

springboot启动类里面添加@EnableEurekaServer@EnableConfigServer注解

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

@EnableEurekaClient
@EnableConfigServer
@SpringBootApplication
public class NeilConfigServerApplication {

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

启动项目,然后查看Eureka,是否服务已注册。

请添加图片描述

2.2、config client搭建

新建项目neil-config-client

pom.xml

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<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>
		<!--springcloud2020及以后版本把Bootstrap默认禁用了,需要引入依赖-->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-bootstrap</artifactId>
		</dependency>

在resource文件下新建bootstrap.yml配置文件

因为我们使用的配置是从config server获取的,所以得用bootstrap.yml才能获取到,bootstrap.yml的优先级高于application.yml。

bootstrap.yml

server:
  port: 8996
  servlet:
    context-path: /
spring:
  application:
    name: neil-config-client
  cloud:
    config:
      profile: dev                        #指定profile,对应neil-config-server所获取的配置⽂件中的{profile}。  ${name}-${profile}.yml
      name: config                        #配置文件名,对应neil-config-server所获取的配置⽂件名,config-uat.yml、config-dev.yml。 ${name}-${profile}.yml
      discovery:
        enabled: true                    #开启服务发现
        service-id: neil-config-server   #config server注册在Eureka上的服务名
#      uri: http://192.168.75.1:8995/    在不使用Eureka的情况下,使用此配置指定config server的地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8989/eureka/ #Eureka Server地址
  instance:
    prefer-ip-address: true  #Hostname使用主机ip

新建一个controller类,用来测试从config server获取数据。env的配置存在于config server,而 config client的配置文件里面并不存在。

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigController {

    @Value("${env}")
    public String env;

    @RequestMapping("/get")
    public String test(){
        System.out.println("配置文件获取:"+env);
        return env;
    }
}

springboot启动类里面添加@EnableEurekaServer@EnableConfigServer注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class NeilConfigClientApplication {

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

启动项目,然后查看Eureka,是否服务已注册。

请添加图片描述

2.3、测试

首先我们直接从config server测试,看看能不能拿到配置文件里的信息

config取值资源信息遵循如下规则

/{application}/{profile}[/{label}] 

/{application}-{profile}.yml 

/{label}/{application}-{profile}.yml 

/{application}-{profile}.properties 

/{label}/{application}-{profile}.properties

http://localhost:8995/config-dev.yml

请添加图片描述

http://localhost:8995/config-uat.yml

请添加图片描述

config server的配置能拿到,那再来测试client服务能否正常拿到config server的值

调用neil-config-client服务的接口测试

http://localhost:8996/get

请添加图片描述

接下来我们修改profile,换成uat,再重启服务

请添加图片描述

然后重新调用接口,获取到的值就变成uat的

请添加图片描述

client里面获取到的值就是config-dev.yml、config-uat.yml这2个配置文件的值,不同profile,获取不同的配置文件。

至此,config简单教程结束

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值