This article through the config server connection git repository to achieve the configuration center, in addition to Git can also use SVN or system local directory.

Introduction of depend on

<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>
    </dependency>
</dependencies>
Copy the code

Spring-cloud-config-server this is the configuration center server dependency.

The configuration center itself needs to register its own instances with the registry to make it highly available, so the requirement references the spring-cloud-starter-Eureka dependency.

Add startup class to enable the Config Server function

@EnableDiscoveryClient @EnableConfigServer @SpringBootApplication public class ConfigApplication { public static void main(String[] args) { SpringApplication.run(ConfigApplication.class, args); }}Copy the code

@enableconFigServer: enables the server configuration function.

@enablediscoveryclient: enable the automatic registration client. By default, the ServiceRegistry implementation automatically registers running services. If the registry uses Eureka, the @enableEurekaclient annotation can also be used.

Adding the Config Configuration

spring: 
  application:
    name: config-center
  profiles:
    active: config-center1
  cloud: 
    config:
      server:
        git:
          uri: ${git.uri}
          searchPaths: ${git.searchPaths}
          username: ${git.username}
          password: ${git.password}
          basedir: ${git.basedir}
          clone-on-start: true
          force-pull: true

eureka:
  instance: 
    prefer-ip-address: true  
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}
    lease-expiration-duration-in-seconds: ${lease-expiration-duration-in-seconds}
    lease-renewal-interval-in-seconds: ${lease-renewal-interval-in-seconds}
  client:
    serviceUrl:
      defaultZone: ${register-center.urls}

---
spring:
  profiles: config-center1

server: 
  port: ${config-center1.server.port}

---
spring: 
  profiles: config-center2

server: 
  port: ${config-center2.server.port}
Copy the code

There are two Config Servers configured and registered with both registries.

Maven filter configuration

#git git.uri=http://gitlab.example.com/test/config.git git.username=root git.password=root git.searchPaths=config-center  git.basedir=f:/config/config-center/gitCopy the code

Spring Cloud Git Git

Spring. Cloud. Config. Server. Git. Uri: git warehouse address.

Spring. Cloud. Config. Server. Git. SearchPaths: git repository search directory.

Spring. Cloud. Config. Server. Git. Username: connect the git user name.

Spring. Cloud. Config. Server. Git. Password: connect the git user name password.

Spring. Cloud. Config. Server. Git. Basedir: configuration center in the local cache configuration directory.

Spring. Cloud. Config. Server. Git. Clone – on – start: startup configuration to true says clone configuration to a local cache.

Spring. Cloud. Config. Server. Git. Force – pull: configuration to true said if a local copy is dirty, will make the spring cloud config server forced Mr Khoja from a remote configuration.

Start the Configuration Center

Start the following configuration centers separately, specifying ports using different profiles.

spring-boot:run -Drun.profiles=config-center1 -P dev
spring-boot:run -Drun.profiles=config-center2 -P dev
Copy the code

Recommended reading

Dry goods: 2TB architect four-stage video tutorial

Interview: the most complete Java multithreaded interview questions and answers

Interview: the most comprehensive ali advanced Java interview questions in history

Interview: The most complete Spring interview questions in history

Tutorial: The most complete Spring Boot complete video tutorial

Books: 15 must-read books for advanced Java architects

Tools: Recommended an online creation flow chart, mind mapping software

Share Java dry goods, high concurrency programming, hot technology tutorials, microservices and distributed technology, architecture design, blockchain technology, artificial intelligence, big data, Java interview questions, and cutting-edge hot news.