首页> 博客> SpringCloud入门系列之配置中心
17 07 2020

Spring Cloud配置中心

目前市场配置中心产品有:

  • Spring Cloud Config
  • 携程 Apollo
  • 阿里巴巴 Nacos

本文主要使用Spring Cloud Config

一、配置中心使用步骤
1.1、pom.xml添加依赖
<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.15.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.codesofun</groupId>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-server</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR6</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
1.2、修改配置

微服务客户端,需要删除原来配置文件application.propertiesapplication.yml。使用bootstrap.propertiesbootstrap.yml 文件进行配置,具体如下:

#环境 book-service-dev.properties
spring.cloud.config.profile=dev
#git 库配置分支
spring.cloud.config.label=master
#配置中心地址
spring.cloud.config.uri=http://localhost:5000
#微服务名称
spring.cloud.config.name=book-service
1.3、配置中心配置
  1. 启动类中添加配置@EnableConfigServer

    package com.codesofun.config.server;
    
    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;
    
    @SpringBootApplication
    //@EnableDiscoveryClient
    @EnableEurekaClient
    @EnableConfigServer
    public class ConfigServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigServerApplication.class, args);
        }
    }
    
  2. 添加配置application.properties

    server.port=5000
    spring.application.name=config-server
    eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
    #配置代理
    spring.cloud.config.server.git.proxy.https.host=
    spring.cloud.config.server.git.proxy.https.port=
    spring.cloud.config.server.git.proxy.https.username=xxxxx
    spring.cloud.config.server.git.proxy.https.password=xxxxxxx
    #git uri
    spring.cloud.config.server.git.uri=https://gitee.com/xmlvhy/sc-config.git
    #连接超时时间
    spring.cloud.config.server.git.timeout=6000
    spring.cloud.config.server.git.basedir=E:/sc_config/git-pro
    
    #私有仓库用户名
    spring.cloud.config.server.git.username=xxx
    ##私有仓库用户密码
    spring.cloud.config.server.git.password=xxxxxxxxx
    
  3. Spring-Retry 重试机制

    Spring-Retry重试模块

    • 通过加入重试机制,提高应用启动的可靠性
    • 重试触发条件1:配置中心无法与仓库正常通信
    • 重试触发条件2:微服务无法配置中心正常通信

    pom.xml中添加依赖

            <!--重试机制-->
            <dependency>
                <groupId>org.springframework.retry</groupId>
                <artifactId>spring-retry</artifactId>
                <version>1.3.0</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>
    

    application.properties中添加配置

    #是否允许快速失败,开启重试机制必须设置为true
    spring.cloud.config.fail-fast=true
    #初始化时间间隔(毫秒)
    spring.cloud.config.retry.initial-interval=5000
    #最大尝试次数
    spring.cloud.config.retry.max-attempts=4
    #最大时间间隔
    spring.cloud.config.retry.max-interval=300000
    #重试时间间隔倍数,最大不超过最大时间间隔,超过则下一次重试时间间隔为最大时间间隔
    spring.cloud.config.retry.multiplier=2
    
  4. 运行时刷新配置参数(客户端)

    修改端口的话,自动刷新无法实现,需要重启客户端服务。

    pom.xml添加依赖

           <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
  5. 动态刷新类上增加@RefreshScope注解(客户端)

    package com.codesofun.bookservice.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @ClassName RefreshController
     * @Description
     * @Author mozhijun
     * @Date 2020/7/14 17:33
     * @Version 1.0
     **/
    @RestController
    @RefreshScope
    public class RefreshController {
        @Value("${custom.title}")
        private String title;
    
        @GetMapping("/title")
        public String title(){
            return this.title;
        }
    }
    
  6. application.properties添加配置(客户端)

    management.endpoints.web.exposure.include=*
    
  7. 调用刷新POST

    http://localhost:8001/actuator/refresh
    
1.4、Config多仓库配置以及高可用

配置让每一个微服务拥有独立仓库。

  1. 配置中心修改配置

    #git uri
    spring.cloud.config.server.git.uri=https://gitee.com/xmlvhy/{application}
    
  2. 客户端中修改配置

    #配置微服务名称
    #spring.cloud.config.name=book-service
    spring.application.name=book-service
    #配置中心地址,配置高可用的时候不能指定具体的uri
    #spring.cloud.config.uri=http://localhost:5000
    

详细代码见仓库:https://gitee.com/xmlvhy/springcloud-learn 参考链接:http://www.itlaoqi.com/

类似文章

  1. SpringCloud入门系列之服务链路追踪Sleuth&Zipkin
  2. SpringCloud入门系列之微服务之间的通信
  3. SpringCloud入门系列之Eureka注册中心
  4. Redis 客户端常用命令
  5. SpringCloud入门系列之API网关

评论区

| 0 评论

还没有评论,快来抢沙发吧!