目前市场配置中心产品有:
本文主要使用Spring Cloud Config
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>
微服务客户端,需要删除原来配置文件application.properties
或 application.yml
。使用bootstrap.properties
或 bootstrap.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
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);
}
}
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
- 通过加入重试机制,提高应用启动的可靠性
- 重试触发条件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
pom.xml
添加依赖 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
@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;
}
}
application.properties
添加配置(客户端)management.endpoints.web.exposure.include=*
POST
http://localhost:8001/actuator/refresh
配置让每一个微服务拥有独立仓库。
配置中心修改配置
#git uri
spring.cloud.config.server.git.uri=https://gitee.com/xmlvhy/{application}
客户端中修改配置
#配置微服务名称
#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/
本文作者: [AI码真香](https://www.xmlvhy.com/about)
本文标题: [SpringCloud入门系列之配置中心](https://www.xmlvhy.com/article/detail/92.html)
本文网址: [https://www.xmlvhy.com/article/92.html](https://www.xmlvhy.com/article/detail/92.html)
版权说明: 自由转载-非商用-非衍生-保持署名 [ 署名-非商业性使用4.0 国际 (CC BY-NC 4.0) ](https://creativecommons.org/licenses/by-nc/4.0/deed.zh)
类似文章