preface

Spring Cloud was used in this paper as 2.1.8RELEASE, version=Greenwich.SR3

This article builds on the implementation of eureka-Server in the previous article. reference

  • eureka-server

An overview of the

In a distributed system, due to the large number of services, a distributed configuration center is required to facilitate unified management of service configuration files. Spring Cloud Config provides server and client support for externalized configuration in distributed systems.

The structure of the project involved is a Config Server standalone mode and suitable for linking Git repository. A Config Client displays configuration file data through the Server. Use two buses at the same time to simulate a configuration file refresh.

Create the config-server project

The Config – server functionality

  • HTTP for external configuration, resource-based API
  • Encrypt and decrypt properties
  • Use an application that can be embedded with Spring Boot

1.1 Creating the Configuration center server: config-server

1.2 Adding pom. XML dependencies of config-server

<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>
Copy the code

1.3 Adding the Application of config-server Add the configuration information

Load the local file configuration
spring:
  application:
    name: config-server
  profiles:
    active: native Load the local configuration
  cloud:
    config:
      server:
        native: Load local directory files
          search-locations: /Users/xxxx/config-server

server:
  port: 13081

eureka:
  instance:
    hostname: eureka1.client.com
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 10
  client:
    service-url:
      defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
Copy the code

1.4 Starting the ConfigServerApplication class added annotations

package spring.cloud.demo.configserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

At this point, a single local config-server is set up

1.5 Creating a Configuration center Client: config-client

1.6 Adding POM-related dependencies of config-client

<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>
Copy the code

1.7 Adding config-client bootstrap.yml configuration (note that this is not application.yml)

bootstrap.yml

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      profile: dev
      fail-fast: true
      uri: http://localhost:13081 Access the configuration center server by domain name
      discovery:
        enabled: true
eureka:
  instance:
    hostname: eureka1.client.com
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 10
  client:
    service-url:
      defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
Copy the code

1.8 Creating a configuration file requested from the server: config-client-dev.yml

Path rules for obtaining resource configurations from the server:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

This article uses the second rule.

Configuration contents:

spring:
  application:
    name: config-client

server:
  port: 52601

eureka:
  instance:
    hostname: eureka1.client.com
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 10
  client:
    service-url:
      defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/

info: local-config-client-dev
Copy the code

Will the new config – the client – dev. Yml in config – server configuration/Users/XXXX/config – directory server, if the config – no configuration directory server, using the resources directory by default.

1.9 ConfigClientApplication added annotations

package spring.cloud.demo.configclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication {

    public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); }}Copy the code

2.0 Create test classes

Create the test Controller in config-client: ConfigClientController

package spring.cloud.demo.configclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/** * Test whether the configuration of the controller * can be obtained@auther: maomao
 * @DateT: the 2019-09-17 * /
@RestController
public class ConfigClientController {

    @Value("${info}")
    private String info;

    @RequestMapping("/config/info")
    public String info(a) {
        returninfo; }}Copy the code

2.1 Starting related Services

Start eureka-server, config-server, and config-client respectively. Visit: http://localhost:52601/config/info, show as below

The local configuration file has taken effect.

2.2 Loading a Resource Configuration File from Remote Git

Modify the application. Yml configuration file of config-server:

## Load local file configuration
#spring:
# application:
# name: config-server
# profiles:
Load the local configuration
# cloud:
# config:
# server:
# native: # load local directory files
# search-locations: /Users/fengfujie/config-server

Load the remote Git repository resource file
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          Git repository
          uri: https://github.com/fengfujie25/sping-cloud-config
          Git repository
          username: xxxxxx
          Git repository password
          password: xxxxxx

server:
  port: 13081

eureka:
  instance:
    hostname: eureka1.client.com
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 10
  client:
    service-url:
      defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
Copy the code

Restart the config-server service. And then refresh the http://localhost:52601/config/info address shown below

The configuration information of the remote Git resource is obtained.

2.3 Accessing the config-server using the service name

All the preceding configurations are accessed using domain names. To ensure high availability of the system, the configuration service center in the production environment is configured in clusters. Therefore, all clients can access the config-server only by using service names.

Modify config-client bootstrap.yml

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      profile: dev
      fail-fast: true
      #uri: http://localhost:13081 # Access configuration center server by domain name
      discovery:
        enabled: true
        service-id: config-server Access configuration center server via service

eureka:
  instance:
    hostname: eureka1.client.com

    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 10
  client:
    service-url:
      defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
Copy the code

Spring. Cloud. Config. Discovery. Service – id: through service center server name to access configuration

Restart the config – client. And visit http://localhost:5301/config/info, shows the result with [2.2] represents the configuration information is effective, proved to be accessed through the service name config – server.

At this point, the spring Cloud integration config configuration is complete. However, there is a problem. If you change the resource configuration of a remote Git repository, the project does not refresh, so the configuration information does not take effect.

2.4 Dynamically Refreshing the Config-server Configuration

Dynamically updated the config-serve configuration mode

  • Dynamic refresh based on RabbitMQ
  • Native refresh (pseudo-dynamic refresh)

This paper uses a relatively simple native refresh method.

2.4.1 Adding related POM.xml dependencies
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Copy the code
2.4.2 Added the @refreshScope annotation to ConfigClientController
package spring.cloud.demo.configclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/ * * *@auther: maomao
 * @DateT: the 2019-09-17 * /
@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${info:error}")
    private String info;

    @RequestMapping("/config/info")
    public String info(a) {
        returninfo; }}Copy the code

In this way, you need to modify the @value annotation to @value (“${info:error}”), because the configuration information must have a default Value when refreshing, otherwise an error will be reported.

2.4.3 Restarting the config-client

Visit http://localhost:5301/config/info to see if the service can be normal access.

You can then modify the configuration information in the Git repository.

  • Via post visit: http://localhost:5301/actuator/refresh, shows as follows:

  • Refresh the http://localhost:5301/config/info, the results showed:

Verify that refresh has taken effect.

This way every time need to manually refresh the line, more troublesome. GitHub provides a Webhooks method that eliminates the need to manually refresh every time.

Payload URL: indicates the URL of the callback

Content Type: Data format, usually using JSON

Secret: a string used to encrypt the Body of the POST, using the HMAC algorithm

Events: Indicates the list of triggered Events

The event type describe
Just the push event The default event is triggered when the repository has a push
Send me everything Send me for everything
Let me select individual events Select individual events

In this way, we can use the Webhook mechanism to trigger the update of the client, but when there are more and more clients, the Webhook mechanism is not elegant enough, and it is not realistic to change the Webhook every time the client is added.

In fact, Spring Cloud gives us a better solution – Spring Cloud Bus.

Spring Cloud Bus will be updated later.

conclusion

In this paper, the simple implementation of config-server and config-client standalone and remote Git repository configuration call and simple dynamic update of configuration information.

The code address

  • Making address: config – server

  • Making address: config – client


  • Eureka Server Service Registry tutorial for Spring Cloud 2.x
  • Eureka Client Service Provider tutorial for Spring Cloud 2.x
  • Spring Cloud 2.x Ribbon Service Discovery Tutorial (with Integrated Hystrix Fuse)
  • Feign Service Discovery Tutorial with Integrated Hystrix Circuit breaker in Spring Cloud 2.x
  • Spring Cloud 2.x Zuul Routing Gateway Tutorial
  • Spring Cloud 2.x Config Distributed Configuration Center tutorial
  • Spring Cloud 2.x Hystrix Dashboard Circuit breaker tutorial

Please indicate the source of reprint,