Spring Cloud学习【第二篇:配置中心】

配置中心

一、引入依赖

<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">
	<modelVersion>4.0.0</modelVersion>
	<artifactId>config-center</artifactId>
	<packaging>jar</packaging>

	<parent>
		<groupId>com.cloud</groupId>
		<artifactId>cloud-service</artifactId>
		<version>1.0</version>
	</parent>

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

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

二、配置bootstrap.properties

######################################
##☆☆☆☆☆    配 置 中 心    ☆☆☆☆☆##
######################################

## 中心应用名
spring.application.name=config-center
## 端口号
server.port=9098
## 指明使用配置文件环境(本地)
spring.profiles.active=native
## classpath中配置文件, {profile}由别的微服务指定的 例如用户中心配置spring.cloud.config.profile=dev注入
spring.cloud.config.server.native.searchLocations=classpath:/configs/{profile}
## spring.cloud.config.server.native.searchLocations=file:/F:/configs/{profile}
## git仓库地址
spring.cloud.config.server.git.uri=https://git****.git
## git仓库的分支
spring.cloud.config.server.git.default-label=master
## 配置为true表示如果本地副本是脏的,将使Spring Cloud Config Server强制从远程存储库拉取配置。
spring.cloud.config.server.git.force-pull=true
## git仓库搜索目录
spring.cloud.config.server.git.searchPaths='{profile}'

## 注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:9099/eureka/
## 服务注册中心也会将自己作为客户端来尝试注册自己,为true(默认)时自动生效
eureka.client.register-with-eureka=true
## 检索服务选项,当设置为True(默认值)时,会进行服务检索
eureka.client.fetch-registry=true
## 表示eureka client间隔多久去拉取服务注册信息
eureka.client.registry-fetch-interval-seconds=5
## 表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该instance。
eureka.instance.lease-expiration-duration-in-seconds=15
## 表示eureka client发送心跳给server端的频率。如果在leaseExpirationDurationInSeconds后,server端没有收到client的心跳,则将摘除该instance。
## 除此之外,如果该instance实现了HealthCheckCallback,并决定让自己unavailable的话,则该instance也不会接收到流量。
eureka.instance.lease-renewal-interval-in-seconds=5
## 将IP注册到Eureka Server上,而如果不配置就是机器的主机名。
eureka.instance.prefer-ip-address=true
## Eureke上显示的实例名:config-center:9098
eureka.instance.instance-id=${spring.application.name}:${server.port}
## 加载所有的端点/默认只加载了 info / health
management.endpoints.web.exposure.include="*"
## 配置是否查看详细的应用健康信息
management.endpoint.health.show-details=always
## 日志级别及目录配置
logging.level.root=info
logging.file=logs/${spring.application.name}.log

三、启动类

package com.cloud.configcenter;

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;

/**
 * 配置中心 <br>
 * 〈功能详细描述〉
 *
 * @see [相关类/方法](可选)
 * @since [产品/模块版本](可选)
 */
@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigCenterApplication {

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

}

四、配置文件结构
在这里插入图片描述
例如:认证中心配置文件 oauth-center.properties:

## 日志级别及目录配置
logging.level.root=info
logging.file=logs/oauth-center.log

## redis配置
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379

## 数据源
spring.datasource.master.jdbc-url = jdbc:mysql://127.0.0.1:3306/TEST1
spring.datasource.master.username = admin
spring.datasource.master.password = admin
spring.datasource.master.driverClassName = com.mysql.jdbc.Driver

spring.datasource.slave.jdbc-url = jdbc:mysql://127.0.0.1:3306/TEST2
spring.datasource.slave.username = admin
spring.datasource.slave.password = admin
spring.datasource.slave.driverClassName = com.mysql.jdbc.Driver

五、运行
必须在启动注册中心后运行配置项目
在这里插入图片描述

发布了40 篇原创文章 · 获赞 31 · 访问量 62万+

猜你喜欢

转载自blog.csdn.net/weixin_38422258/article/details/95227104