Spring Cloud学习笔记21——微服务的集中化配置

微服务为什么需要集中化配置

一般应用中都会有配置文件,即便是号称“零配置”的Spring Boot应用,也无法完全做到不使用配置文件,因为配置文件是为了迎合软件的一些个性化需求,所以说应用程序是无法避免这种配置的,特别是在微服务系统架构中,应用程序往往会部署在多个实例上,每个实例都有各自的不同的配置,如果配置发生了更改,每个服务实例都需要进行配置的变更,既然配置的变更无法避免,每个微服务自己的配置文件散落在自己的应用中,必然会对应用的配置和升级带来挑战,故而需要引入集中化配置,来解决微服务中关于配置的问题。

  • 微服务数量多,配置多
  • 手工管理配置繁琐

配置分类

  • 按配置的来源划分:主要有源代码、文件、数据库连接、远程调用等
  • 按配置的环境划分:开发环境、测试环境、预发布环境、生产环境等
  • 按配置的集成阶段划分:编译时、打包时和运行时
  • 按配置的加载方式划分:启动加载和动态加载

配置中心的要求

  • 面向可配置的编码:在创建符合要求、易于使用的配置中心时,在编码过程中,应及早考虑后期可能会经常变更的一些数据,设置为可以配置的配置项,可避免在代码中硬编码
  • 隔离性:不同的部署环境,应用之间的配置是相互隔离的
  • 一致性:相同的部署的服务器的应用配置应该是一致的,在相同部署环境下,同一个应用的所有实例使用同一份配置
  • 集中化配置:在分布式环境下,应用的配置应该具备可管理性,即提供远程管理的能力

配置中心的文件命名规则

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{lable}/{application}-{profile}.yml
/{application}-{profile}.properties
/{lable}/{application}-{profile}.properties

Spring Cloud Config

分布式外部化配置

  • Config Server:管理应用程序的外部属性,底层实现基于Git
  • Config Client

我的GitHub

在这里插入图片描述
在这里插入图片描述

使用Config实现配置中心Server端

开发环境

  • JDK8+
  • Gradle4+
  • Spring Boot 2.0.0.M3
  • Spring Cloud Starter Netflix Eureka Client Finchley.M2
  • Spring Cloud Config Server Finchley.M2

创建项目

以之前的micro-weather-eureka-client为蓝本,创建micro-weather-config-server项目
在这里插入图片描述

修改源码

修改build.gradle配置,添加Spring Cloud Config Server依赖:

//buildscript代码块中脚本优先执行
buildscript {

    //ext用于定义动态属性
	ext {
		springBootVersion = '2.0.0.M3'
	}

    //使用了Maven的中央仓库及Spring自己的仓库(也可以指定其他仓库)
	repositories {
		//mavenCentral()
        maven{ url "https://repo.spring.io/snapshot" }
        maven{ url "https://repo.spring.io/milestone" }
        maven{ url "http://maven.aliyun.com/nexus/content/groups/public/" }
	}

    //依赖关系
	dependencies {
        //classpath声明了在执行其余的脚本时,ClassLoader可以使用这些依赖项
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
	}
}

//使用插件
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

//指定了生成的编译文件的版本,默认是打成了jar包
group = 'com.study.spring.cloud'
version = '1.0.0'

//指定编译.java文件的JDK版本
sourceCompatibility = 1.8

//使用了Maven的中央仓库及Spring自己的仓库(也可以指定其他仓库)
repositories {
    //mavenCentral()
    maven{ url "https://repo.spring.io/snapshot" }
    maven{ url "https://repo.spring.io/milestone" }
    maven{ url "http://maven.aliyun.com/nexus/content/groups/public/" }
}

ext {
    springCloudVersion = 'Finchley.M2'
}

//依赖关系
dependencies {

    //Eureka Client
    compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')

    //Spring Cloud Config Server
    compile('org.springframework.cloud:spring-cloud-config-server')

    //该依赖用于测试阶段
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

//Spring Cloud依赖管理
dependencyManagement{
    imports{
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

修改com.study.spring.cloud.weather包下的Application类,加入@EnableConfigServer注解:

package com.study.spring.cloud.weather;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

/*
 * @SpringBootApplication注解声明Spring Boot应用
 * 作用等同于@Configuration, @EnableAutoConfiguration, @ComponentScan,
 * 简化Spring配置
*/
@SpringBootApplication
//启用可发现的客户端
@EnableDiscoveryClient
//启用Config Server
@EnableConfigServer
//Application类一定要处于整个工程的根目录下,这样它才能根据配置去扫描子节点下的Spring的Bean
public class Application {

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

修改application.properties配置文件:

#应用名称
spring.application.name=micro-weather-config-server

#指定端口
server.port=8888

#注册服务器的URL
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

#GitHub仓库
spring.cloud.config.server.git.uri=https://github.com/yehongliu/config-server-study.git

#下级目录
spring.cloud.config.server.git.search-paths=config-repo

先运行micro-weather-eureka-server,再运行micro-weather-config-server,运行结果如下:
在这里插入图片描述
访问http://localhost:8888/auther/dev页面:
在这里插入图片描述

使用Config实现配置中心Client端

开发环境

  • JDK8+
  • Gradle4+
  • Spring Boot 2.0.0.M3
  • Spring Cloud Starter Netflix Eureka Client Finchley.M2
  • Spring Cloud Config Client Finchley.M2

创建项目

以之前的micro-weather-eureka-client为蓝本,创建micro-weather-config-client项目
在这里插入图片描述

修改源码

修改build.gradle配置,添加Spring Cloud Config Client依赖:

//依赖关系
dependencies {

    //Eureka Client
    compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')

    //Spring Cloud Config Client
    compile('org.springframework.cloud:spring-cloud-config-client')

    //该依赖用于测试阶段
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

修改application.properties配置文件:

#应用名称
spring.application.name=micro-weather-config-client

#注册服务器的URL
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

#指定环境
spring.cloud.config.profile=dev

#指向Config Server
spring.cloud.config.uri=http://localhost:8888/

运行测试

修改test下的com.study.spring.cloud.weather包下的ApplicationTests类:

package com.study.spring.cloud.weather;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.Assert.assertEquals;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {

	@Value("${auther}")
	private String auther;

	@Test
	public void contextLoads() {
		assertEquals("willow51",auther);
	}

}

先运行micro-weather-eureka-servermicro-weather-config-server,再运行ApplicationTests类的contextLoads测试方法,运行结果如下:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43415405/article/details/84069251