springBoot-简单的分布式配置中心

Spring Cloud Config 配置中心(高可用)

git仓库的结构

在这里插入图片描述

要实现高可用效果,那就要先创建一个注册中心项目

导入dependency

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</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>

配置文件

server.port=8762
spring.application.name=eureka-server
eureka.client.service-url.defaultZone=http://localhost:8762/eureka

入口类

@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {

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

}

创建一个配置中心的项目 config server

导入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-web</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>

配置文件

# 项目端口
server.port=8881
# 项目名称
spring.application.name=config-server
# configserver git配置
# 仓库的地址,如果私有仓库 那么需要配置账号和密码
spring.cloud.config.server.git.uri=https://github.com/VULCAN2019/SpringCloudConfig.git
# 仓库的账号和密码(仓库为私有的情况下才需要这两条配置)
#spring.cloud.config.server.git.username=
#spring.cloud.config.server.git.password=
# 存放配置文件的目录
spring.cloud.config.server.git.search-paths=resp
# 使用的git分支
spring.cloud.config.label=master

# 指定注册中心地址
eureka.client.service-url.defaultZone=http://localhost:8762/eureka

启动类

@EnableEurekaClient // 注册到注册中心
@EnableConfigServer // 启动配置中心
@SpringBootApplication
public class ConfigserverApplication {
    
	public static void main(String[] args) {
		SpringApplication.run(ConfigserverApplication.class, args);
	}
    
}

构建一个config client

导入dependency

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-config</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-web</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>

配置文件 bootstrap.properties

# 配置文件名称 必须是 bootstrap.properties/yml
spring.application.name=config-client
server.port=8882
# 引用配置中心的内容
spring.cloud.config.label=master
spring.cloud.config.profile=dev
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
# 表示从注册中心中获取内容
spring.cloud.config.discovery.enabled=true
# 表示注册中心中配置中心的服务名称
spring.cloud.config.discovery.service-id=config-server

启动类

@EnableEurekaClient //注册到注册中心
@RestController
@SpringBootApplication
public class SpringcloudserverApplication {

    // 用来测试能否取到git仓库里配置文件的值
	@Value("${name}")
	private String name;

    // 查看值的接口
	@GetMapping("/test")
	public String test(){
		return name;
	}

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

}

总结:如果只是单纯的让某个配置中心从GIT上获取配置文件,然后有一个客户端Client使用GIT上的配置文件,是可以不用注册中心的,但是随着服务的增多,如果都使用同一个配置中心,万一此配置中心挂了,那么依赖此配置中心的所有微服务系统就全部崩溃了。所以,为了避免这种情况的出现,那么就部署多个配置中心,防止某个配置中心节点故障导致的整个系统的崩溃的情况。

发布了30 篇原创文章 · 获赞 7 · 访问量 1584

猜你喜欢

转载自blog.csdn.net/qq_41946543/article/details/103374533