Springcloud 之 服务配置中心

SpringCloud Config简介

Spring Cloud Config 是 Spring Cloud 团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分。其中服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息、加密 / 解密信息等访问接口;而客户端则是微服务架构中的各个微服务应用或基础设施,它们通过指定的配置中心来管理应用资源与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。Spring Cloud Config 实现了对服务端和客户端中环境变量和属性配置的抽象映射,所以它除了适用于 Spring 构建的应用程序之外,也可以在任何其他语言运行的应用程序中使用。由于 Spring Cloud Config 实现的配置中心默认采用 Git 来存储配置信息,所以使用 Spring Cloud Config 构建的配置服务器,天然就支持对微服务应用配置信息的版本管理,并且可以通过 Git 客户端工具来方便的管理和访问配置内容。当然它也提供了对其他存储方式的支持,比如:GIT仓库、SVN 仓库、本地化文件系统。
在这里插入图片描述
Config Server端主要和Git/SVN服务器

通俗点,就是统一管理配置,包括方便切换环境配置,以及修改配置无需动代码,省心省力;
如果用上SpringCloud Bus,能实现无需重启,自动感知配置变化以及应用新配置;
在这里插入图片描述

Config Server基本使用

根据前面SpringCloud架构图,首先第一步,要搞个 configServer来联通远程GIT仓库,来读取远程配置;

这里GIT仓库,我们一般选用GitHub https://github.com/,或者码云 https://gitee.com/
我们这里用GitHub演示

建个仓库 microservice-config 然后 Git下载本地;

上传一个配置文件上到git仓库,application.yml 记住要utf-8编码,否则乱码,解析各种问题;

profile: hello

在这里插入图片描述
随便搞,目前只要能读取到配置即可;

新建module:microservice-config-server-4001
pom依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.wsy</groupId>
        <artifactId>springcloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>microservice-config-server-4001</artifactId>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.yml
server:
  port: 4001

spring:
  application:
    name:  microservice-config
  cloud:
    config:
      server:
        git:
          uri: git@github.com:wangshaoyangzsm/t224-microservice-config-.git

启动类MicroserviceConfigServer4001Application
package com.wsy.microserviceconfigserver4001;

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

@SpringBootApplication
@EnableConfigServer
public class MicroserviceConfigServer4001Application {

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

}

本地Hosts加个本地域名映射:
在这里插入图片描述
运行可得
在这里插入图片描述

Config Client基本使用

在本地仓库添加两个配置文件并上传到Github

crm-dev.yml
port:
  777
crm-test.yml
port:
  888
application.yml:
---
spring:
  profiles:
    active: dev
---
spring:
  profiles: dev
port: 111
---
spring:
  profiles: test
port: 222
新建一个module microservice-config-client-5001
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

项目启动的时候,就要调用server config端,获取配置信息,所以这里要bootstrap.yml配置文件,优先级最高,会比application先调用:

bootstrap.yml
spring:
  application:
    name: application-dev
  cloud:
    config:
      name: crm
      uri: http://configserver.wsy.com:4001
      profile: dev
      label: master
      fail-fast: true

application.yml
server:
  port: 5001
  context-path: /
ConfigClientControlle
package com.wsy.microserviceconfigclient5001.controller;

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

@RestController
public class ConfigClientController {
 
    @Value("${port}")
    private String port;
 
    @GetMapping("/getPort")
    public String getPort() {
        return "测试你访问的yml文件的端口是:【"+port+"】";
    }
}

测试 启动项目,输入:http://localhost:5001/getPort
在这里插入图片描述

Config整合Eureka

eureka整合config以及服务器提供者整合config
我们先上传配置文件到Github

eureka_config.yml
spring:
  profiles:
    active:
    - dev
---
server:
  port: 2004
  context-path: /
spring:
  profiles: dev
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
---
server:
  port: 2005
  context-path: /
spring:
  profiles: test
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

新建module microservice-eureka-server-config

配置两个配置文件
application.yml

spring:
  application:
    name: microservice-eureka-server-config
bootstrap.yml
spring:
  application:
    name: microservice-eureka-server-config
  cloud:
    config:
      name: eureka_config
      uri: http://localhost:4001  # 配置configserver地址
      profile: dev  # 级别
      label: master  # 分支 git中 默认master

启动类加上注解

package com.wsy.microserviceeurekaserverconfig;

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


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

}

再搭建一个生产者注册进这个注册中心:microservice-student-provider-config
一样的首先上传一个配置文件

provider_config.yml
spring:
  profiles:
    active: dev
---
server:
  port: 1007
  context-path: /

# 数据源配置
spring:
  profiles: dev
  application:
    name: microservice-student
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf8
    username: root
    password: 123
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

eureka:
  instance:
    hostname: localhost
    appname: microservice-student
    instance-id: microservice-student:1007
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:2004/eureka

info:
  groupId: com.xy.testSpringcloud
  artifactId: microservice-student-provider-config-1007
  version: 1.0-SNAPSHOT
  userName: http://wsy.com
  phone: 123456
---
server:
  port: 1008
  context-path: /

# 数据源配置
spring:
  profiles: test
  application:
    name: microservice-student
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf8
    username: root
    password: 123
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

eureka:
  instance:
    hostname: localhost
    appname: microservice-student
    instance-id: microservice-student:1008
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:2004/eureka

info:
  groupId: com.xy.testSpringcloud
  artifactId: microservice-student-provider-config-1008
  version: 1.0-SNAPSHOT
  userName: http://wsy.com
  phone: 123456
pom依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.wsy</groupId>
        <artifactId>springcloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.wsy</groupId>
    <artifactId>microservice-student-provider-config</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>microservice-student-provider-config</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.wsy</groupId>
            <artifactId>microservice-common</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>
        <!--添加注册中心Eureka相关配置-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

        <!-- actuator监控引入 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>com.wsy</groupId>
            <artifactId>microservice-common</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.yml
spring:
  application:
    name: microservice-student-provider-config
bootstrap.yml
spring:
  application:
    name: microservice-student-provider-config
  cloud:
    config:
      name: provider_config
      uri: http://localhost:4001  # 配置configserver地址
      profile: dev  # 级别
      label: master  # 分支 git中 默认master

然后启动就行了

Config配置搜索路径

前面我们所有的GIT远程端配置文件都是跟目录的,所有请求默认都是根目录,但是有时候,项目很多,配置文件需要根据子目录来划分,这时候,就需要来配置搜索路径了;比如aaa项目的配置文件放aaa目录下,bbb项目的配置文件放bbb目录下,不配置的话 是找不到的那些配置文件的,我们需要配置search-paths属性实现;

microservice-config-server-4001 configserver端 加个配置
server:
  port: 4001

spring:
  application:
    name:  microservice-config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/wsy/microservice-config.git
          search-paths: first,second
nns.yml,nns2.yml,nn3.yml,内容都一样
spring:
  profiles:
    active: dev
---
spring:
  profiles: dev
name: aaadev
---
spring:
  profiles: test
name: aaatest

在这里插入图片描述
second
在这里插入图片描述
在这里插入图片描述
over。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Cloud Nacos作为配置中心的优势在于它具备统一管理、动态刷新和分布式配置的能力。下面是使用Spring Cloud Nacos作为配置中心的基本步骤: 1. 添加依赖:在项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> ``` 2. 配置Nacos服务地址:在项目的配置文件(application.yml或application.properties)中添加以下配置: ```yaml spring: cloud: nacos: config: server-addr: ${nacos.server-addr} ``` 其中`${nacos.server-addr}`是Nacos服务的地址,如`localhost:8848`。 3. 创建配置文件:在Nacos控制台创建配置文件,例如创建一个名为`example.properties`的配置文件,并添加一些配置项。 4. 使用配置:在Spring Boot应用程序中,通过使用`@Value`注解来注入配置项,例如: ```java @Value("${example.property}") private String exampleProperty; ``` 这样,`exampleProperty`变量将被自动注入为配置文件中`example.property`对应的值。 5. 动态刷新:当配置发生变化时,可以通过添加`@RefreshScope`注解来实现动态刷新,例如: ```java @RefreshScope @RestController public class ExampleController { // ... } ``` 这样,当配置发生变化时,注入的配置项将自动更新。 以上是使用Spring Cloud Nacos作为配置中心的基本步骤,希望能帮到你!如有更多问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

听晚风续过晚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值