SpringCloud服务配置中心

本文介绍了SpringCloud Config作为分布式配置中心的使用,包括服务端搭建、客户端配置以及与Eureka的整合。通过Config,可以集中管理服务配置,支持实时更新,避免代码变动。同时,通过Config Server与Git仓库结合,实现了配置文件的远程管理。Config Client则在启动时从Server获取配置信息。此外,文章还展示了如何将Config与Eureka服务发现组件整合,使配置更新能够自动传播到服务提供者。
摘要由CSDN通过智能技术生成

SpringCloud Config简介

Config服务端搭建(Server)

Config客户端搭建(Client)

Config整合Eureka

SpringCloud Config简介

配置中心为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件,它就是Spring Cloud Config.
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。

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

在这里插入图片描述

Config服务端搭建(Server)

application.yml

---
spring:
  profiles:
    active: dev
---
spring:
  profiles: dev
port: 111
---
spring:
  profiles: test
port: 222



新建一个项目

microservice-config-server-4001

    <parent>
        <groupId>com.liuxia</groupId>
        <artifactId>springcloud01</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    


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



配置application.yml文件,把你Github仓库的https访问路径配置好

server:
  port: 4001

spring:
  application:
    name:  microservice-config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/liuxia2020/microservice-config.git



然后给启动类添加注解


package com.liuxia.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);
    }
}


输入:http://localhost:4001/application-xx.yml
在这里插入图片描述

Config客户端搭建(Client)

在本地仓库添加两个配置文件并上传到Github
crm-dev.yml

port:
  777


crm-test.yml

port:
  888


新建项目microservice-config-client-5001
导入pom依赖,修改父工程

 <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://localhost:4001
      profile: dev
      label: master
      fail-fast: true


application.yml

server:
  port: 5001
  context-path: /


然后再配置一个我们测试用的controller
ConfigClientController

package com.liuxia.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/


新建项目: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


在启动类加上Eureka的注解:@EnableEurekaServer

然后再搭建一个生产者注册进这个注册中心: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/t226_layui?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.liuxia.testSpringcloud
  artifactId: microservice-student-provider-config-1007
  version: 1.0-SNAPSHOT
  userName: http://liuxia.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/t226_layui?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.liuxia.testSpringcloud
  artifactId: microservice-student-provider-config-1008
  version: 1.0-SNAPSHOT
  userName: http://liuxia.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.liuxia</groupId>
        <artifactId>springcloud01</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.liuxia</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>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>
         

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


然后启动项目,输入:localhost:2004
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值