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上的配置文件,是可以不用注册中心的,但是随着服务的增多,如果都使用同一个配置中心,万一此配置中心挂了,那么依赖此配置中心的所有微服务系统就全部崩溃了。所以,为了避免这种情况的出现,那么就部署多个配置中心,防止某个配置中心节点故障导致的整个系统的崩溃的情况。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值