目录

  • 一、应用场景
  • 二、Config配置中心介绍
  • 三、使用实例
  • (一)构建Config Server统⼀配置中⼼
  • 1、pom
  • 2、启动类
  • 2、配置文件
  • 4、测试
  • (二)构建Client客户端
  • 1、pom
  • 2、配置文件
  • 3、添加一个controller测试是否可以配置中心获取值
  • 4、测试
  • 5、注意
  • ①client server jar引用
  • ②client client jar引用
  • ③以上两个jar包,版本要对应


一、应用场景

往往,我们使⽤配置⽂件管理⼀些配置信息,⽐如application.yml

单体应⽤架构,配置信息的管理、维护并不会显得特别麻烦,⼿动操作就可以,因
为就⼀个⼯程;

微服务架构,因为我们的分布式集群环境中可能有很多个微服务,我们不可能⼀个⼀个去修改配置然后重启⽣效,在⼀定场景下我们还需要在运⾏期间动态调整配置信息,⽐如:根据各个微服务的负载情况,动态调整数据源连接池⼤⼩,我们希望配置内容发⽣变化的时候,微服务可以⾃动更新。

场景总结如下:

  1. 集中配置管理,⼀个微服务架构中可能有成百上千个微服务,所以集中配置管理是很重要的(⼀次修改、到处⽣效)
  2. 不同环境不同配置,⽐如数据源配置在不同环境(开发dev,测试test,⽣产prod)中是不同的
  3. 运⾏期间可动态调整。例如,可根据各个微服务的负载情况,动态调整数据源连接池⼤⼩等配置修改后可⾃动更新
  4. 如配置内容发⽣变化,微服务可以⾃动更新配置那么,我们就需要对配置⽂件进⾏集中式管理,这也是分布式配置中⼼的作⽤。

二、Config配置中心介绍

Spring Cloud Config是⼀个分布式配置管理⽅案,包含了Server端和 Client端两个部分。

spring cloud gcp配置文件 spring cloud config配置中心_spring

  1. Server 端:提供配置⽂件的存储、以接⼝的形式将配置⽂件的内容提供出去,通过使⽤@EnableConfigServer注解在 Spring boot 应⽤中⾮常简单的嵌⼊
  2. Client 端:通过接⼝获取配置数据并初始化⾃⼰的应⽤

总结:

  1. 单独创建 Config Server 工程,专门提供配置信息
  2. 微服务从Config Server 获取配置信息
  3. Config Server 从git上获取配置信息

三、使用实例

说明
Config Server是集中式的配置服务,⽤于集中管理应⽤程序各个环境下的配置。 默认使⽤Git存储配置⽂件内容,也可以SVN。

新建项目lagou-cloud-configserver-9006

(一)构建Config Server统⼀配置中⼼

1、pom

git上的文件命令规则

  • {application}-{profile}.yml 或者 {application}-{profile}.properties
  • 其中,application为应⽤名称,profile指的是环境(⽤于区分开发环境,测试环
    境、⽣产环境等)
  • 示例:lagou-service-resume-dev.yml、lagou-service-resume-test.yml、lagou-
    service-resume-prod.yml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>lagou-parent</artifactId>
        <groupId>com.lagou.edu</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>lagou-cloud-configserver-9006</artifactId>
    <dependencies>
        <!--eureka client 客户端依赖引入-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--config配置中心服务端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>
</project>

2、启动类

package com.lagou.edu;

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
@EnableDiscoveryClient
@EnableConfigServer // 开启配置服务器功能
public class ConfigServerApplication9006 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication9006.class,args);
    }
}

2、配置文件

server:
  port: 9006
# 添加注册到eureka的配置
eureka:
  client:
    service-url:
      defaultZone: http://LagouCloudEurekaServerA:8762/eureka,http://LagouCloudEurekaServerB:8763/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}@project.version@
spring:
  application:
    name: lagou-cloud-configserver
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/XXXX/spring-cloud-test.git
          username: XXXXXXXXXXXXXXXXX # git网站用户名
          password: XXXXXXXXXXXXXXXXX # git网站用户密码
          search-paths:
            - spring-cloud-test # git上的服务名称
      label: master # git上项目的分支名称
  #针对的被调⽤⽅微服务名称,不加就是全局⽣效
  #lagou-service-resume:
  # ribbon:
  # NFLoadBalancerRuleClassName:
  # com.netflix.loadbalancer.RoundRobinRule #负载策略调整
management:
  endpoints:
    web:
      exposure:
        include: "*"
  # springboot中暴露健康检查等断点接⼝
  endpoint:
    health:
      show-details: always

4、测试

启动并访问地址
http://localhost:9006/master/lagou-service-resume-dev.ymlspring cloud gcp配置文件 spring cloud config配置中心_微服务_02

(二)构建Client客户端

改造原有的项目lagou-service-resume-8080

1、pom

添加依赖

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

2、配置文件

application.yml需要改名为bootstrap.yml

spring cloud gcp配置文件 spring cloud config配置中心_config_03


配置如下:

server:
  port: 8080
spring:
  application:
    name: lagou-service-resume
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/lagou-sc?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
    username: root
    password: root
  cloud:
    # config客户端配置,和ConfigServer通信,并告知ConfigServer希望获取的配置信息在哪个文件中
    config:
      name: lagou-service-resume  #配置文件名称
      profile: dev  #后缀名称
      label: master #分支名称
      uri: http://localhost:9006    #ConfigServer配置中心地址
mybatis-plus:
  mapper-locations: classpath:mapper/*.xml
  configuration: # 打印sql
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true
# 添加注册到eureka的配置
eureka:
  client:
    service-url:
      defaultZone: http://LagouCloudEurekaServerA:8761/eureka/,http://LagouCloudEurekaServerB:8762/eureka/
  instance:
    prefer-ip-address: true #服务实例中显示ip,而不是显示主机名(兼容老的eureka版本)
    # 实例名称: 192.168.1.103:lagou-service-resume:8080,我们可以自定义它
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}@project.version@

3、添加一个controller测试是否可以配置中心获取值

package com.lagou.edu.web;

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

/**
 *
 * 该类用于模拟,我们要使用配置中心的公共配置做一些事情
 */
@RestController
@RequestMapping("/config")
public class ConfigController {

    /**
     * 配置中心的配置的属性
     * lagou:
     *   message: hello lagou,100
     * mysql:
     *   url: dev-http://localhost:3306/db100
     */

    // 和取本地配置信息一样
    @Value("${lagou.message}")
    private String lagouMessage;
    @Value("${mysql.url}")
    private String mysqlUrl;


// http://localhost:8080/config/viewconfig
    @GetMapping("/viewconfig")
    public String viewconfig() {
        return "lagouMessage==>" + lagouMessage + " mysqlUrl=>" + mysqlUrl;
    }
}

4、测试

spring cloud gcp配置文件 spring cloud config配置中心_git_04

5、注意

如果启动项目报错【spring cloud config Could not resolve placeholder lagou.message………………${lagou.message}

需要注意

①client server jar引用
<!--config配置中心服务端-->
<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-server</artifactId>
</dependency>
②client client jar引用
<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-config-client</artifactId>
</dependency>
③以上两个jar包,版本要对应
最好是使用同样的版本。
   我自己测试的时候,就是因为在父工程中单独定义了
   spring-cloud-config-server版本,
   spring-cloud-config-client版本使用默认的,
   导致配置正确启动报错。去掉parent中的
   spring-cloud-config-server版本定义,就不报错了