Microservices connect to the configuration center to read the external configuration.

Introduction of depend on

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.retry</groupId>
        <artifactId>spring-retry</artifactId>
    </dependency>
</dependencies>
Copy the code

Spring-cloud-starter-config: configures the dependency of the central client.

Spring-boot-starter-aop,spring-retry: These are the dependencies needed to connect to the configuration center for quick failures and retries.

Add startup classes

@EnableDiscoveryClient
@SpringBootApplication
public class ServiceApplication {

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

}
Copy the code

Add the configuration

Add the following configuration to bootstrap.yml, which must be bootstrap, not application.

spring: 
  application: 
    name: config-client
  cloud:
    config:
      #username: 
      #password: 
      name: ${git.application}
      profile: ${git.profile}
      label: ${git.label}
      fail-fast: true
      retry:
        initial-interval: 2000
        max-attempts: 5
      discovery: 
        enabled: true
        service-id: config-center 

eureka:
  client: 
    serviceUrl:
      defaultZone: ${register-center.urls}
Copy the code

As you can see, the configuration is relatively simple and will not be detailed below.

The application. Yml configuration file is as follows:

spring: 
  profiles: 
    active: config-client1

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}

---
spring: 
  profiles: config-client1

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

---
spring: 
  profiles: config-client2

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

Maven filter configuration

. #git git.application=application git.profile=dev git.label=master ...Copy the code

Reading configuration

@RestController public class TestController { @Value("${username}") private String username; .Copy the code

You can use Value to read the configuration in the configuration center, but you can also get the configuration from SpringCloud in other ways, as mentioned in the previous SpringBoot articles.

Start the service

By specifying a Profile to start two microservices, they can read the content of the configuration center.

spring-boot:run -Drun.profiles=config-client1 -P dev
spring-boot:run -Drun.profiles=config-client2 -P dev
Copy the code

Recommended reading

Dry goods: Free 2TB architect four-stage video tutorial

Interview: the most complete Java multithreaded interview questions and answers

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.