springCloud微服务系列——配置中心第一篇——简单搭建

目录

一、简介

二、服务端

三、客户端


一、简介

       这篇文章简单总结如何搭建配置中心

二、服务端

   pom配置

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

  bootstrap.yml文件配置,注意必须将以下配置配置到bootstrap.yml中,不能配置到application.yml中。bootstrap.yml在application.yml之前加载,用来加载外部资源。

spring: 
  application: 
    name: config-server
  cloud: 
    config: 
      server: 
       git: 
        uri: https://github.com/wulinfeng2/serverConfig
        username: xxx #如果企业是付费用户
        password: xxx #如果企业是付费用户
        searchPaths: aciv-cf #项目根路径
        clone-on-start: true #启动的时候拉取配置文件,而不是需要的时候,这样可能使启动变慢,第一次加载配置变快
        force-pull: true #配置文件在本地会有一个备份,设置为true,始终从服务端获取,防止脏数据

  在启动类上加上@EnableConfigServer注解

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

三、客户端

   pom配置

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
    
<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
</dependency>

   bootstrap.yml文件配置

spring: 
  application: 
    name: config-demo
  cloud: 
    config: 
      uri: http://@localhost:8868/manage/serverConfig
      name: demo #{描述}-{环境}.yml中的描述,可以配置多个
      label: master #github的label,用来做版本控制
      fail-fast: true #如果不能连接到配置中心,则无法启动
      retry: 
        max-attempts: 6 #最大重试次数
      request-read-timeout: 6000 #读取配置超时时间
  profiles: 
    active: dev

  在启动类上加上@EnableDiscoveryClient注解

@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication {
	
	public static void main(String[] args) {
		SpringApplication.run(ConfigClientApplication.class, args);
	}

}

猜你喜欢

转载自blog.csdn.net/guduyishuai/article/details/81704641