springcloud之配置中心用法

一、配置文件服务器server端

1、构建server端所需jar

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <version>1.4.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

 2、构建server端main方法所在类的注解

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
@EnableAutoConfiguration
@Configuration

3、构建server端所需配置文件及详解

server:
  port: 8001  
spring:
  application:
    name: spring-cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/Pinshuducha/config-file/ --git仓库地址
          search-paths: config-repo --将拉取到的配置文件放到哪个文件夹下
          username:   --git用户名 有时可以不加
          password:   --git密码  有时可以不加
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8000/eureka/

二、配置文件服务器client端

1、构建client端所需jar

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <version>1.4.4.RELEASE</version>
    </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>
    </dependency>
</dependencies>

2、构建client端所需配置文件及解释(注意:此时的配置文件一般命名为bootstrap.yml)

server:
  port: 8002
spring:
  application:
    name: configclient
  cloud:
    config:
      discovery:
        serviceId: spring-cloud-config-server
        enabled: true
      name: configclient
      profile: default,pre,two
      label: master
  profiles:
    active: default,pre,two
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8000/eureka/

3、构建client端所需注解

@SpringBootApplication
@EnableEurekaClient

三、配置文件库

这个是我创建配置文件库的方法

直接操作github或gitlib的操作页面创建,然后更新到本地,以后就可以修改了

server端和client端代码参考:https://github.com/Pinshuducha/spring-cloud-config.git

配置文件库参考:https://github.com/Pinshuducha/config-file.git

原文地址:https://www.cnblogs.com/lu51211314/p/10370168.html