综合编程

Microservice Configuration: Spring Cloud Config Server Tutorial

微信扫一扫,分享到朋友圈

Microservice Configuration: Spring Cloud Config Server Tutorial
0

Configuring Microservices: The Challenges

Managing application configuration in a traditional monolith is pretty straight forward. The configuration is usually externalized in a properties files on the same server as the application. If you need to update the configuration you simply amend the properties file and restart the application. Things can get a little tricker with microservices, but why is that?

Microservices are composed of many small, autonomous services, each with their own configuration. Rather than a centralised properties file (like the monolith), configuration is scattered across multiple services, running on multiple servers. In a production environment, where you likely have multiple instances of each service, configuration management can become a hefty task.

If you’re running in the cloud things don’t get any easier.  Cloud environments tend to be quite fluid with instances regularly added and removed as a result of auto-scaling activity. This fluidity can make it difficult to apply updates and ensure that each service has the correct configuration.

Introducing Centralized Configuration

Centralized configuration is a pattern where the configuration for all services is managed in a central repository rather than being scattered across individual services. Each service pulls its configuration from the central repository on startup.

Spring provides a basis for implementing this pattern via Spring Cloud Config, a subproject of Spring Cloud. With Spring Cloud Config you can create a Spring Boot application that exposes application properties via a REST API. Services can consume their application properties from the REST API rather than loading them locally from the file system or classpath. Configuration is not stored in the Cloud Config Server itself but pulled from a Git repository. This allows you to manage your application configuration with all the benefits of version control. Spring Cloud Config can be configured to use either a local git repository (useful during dev) or a remote repository. In a production environment, you’d want the Config Server to access configuration from a private Git repository.

Sample Application

In this post, I’m going to show you how to set up a Configuration Service that pulls configuration from GitHub. I’ll also show you how the configuration is consumed by another service, in this case, a simple bank account service. The diagram below describes the main components involved.

Centralized Configuration Architecture

The full source code for the sample app is available on GitHub .

Creating the Cloud Config Service

We’ll begin by creating a simple Spring Boot app for the Config Service. Inside the main application class use @EnableConfigServer to enable the config server functionality.

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

Configuring the POM

In order to make the required Spring Cloud dependencies available you’ll need to add the following dependencies to the POM.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0                         http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.briansjavablog.microservices</groupId>
   <artifactId>config-server</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>config-server</name>
   <description>Demo config server provides centralised configuration for various micro services</description>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.3.RELEASE</version>
   </parent>
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
      <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
   </properties>
   <dependencies>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-config-server</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-devtools</artifactId>
         <scope>runtime</scope>
      </dependency>
   </dependencies>
   <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
         </dependency>
      </dependencies>
   </dependencyManagement>
   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
</project>-

Configuring the Config Server

Next, you’ll need to configure the Config Service via the application.properties  file .

spring.application.name=config-server
server.port=8888

# URI of GIT repo containing properties
spring.cloud.config.server.git.uri=https://github.com/briansjavablog/micro-services-spring-cloud-config

# path to properties from root of repo 
spring.cloud.config.server.git.searchPaths: configuration

logging.level.org.springframework.web=INFO
  • Line 1 – spring.application.name   specifies the name of the application. This isn’t essential but it’s good practice to name your Boot applications. This name will appear on the actuator/info endpoint and will be displayed in Spring Boot Admin if you use it.
  • Line 2 – server.port   specifies the port that the admin app will run on.
  • Line 5 – spring.cloud.config.server.git.uri   specifies the URL of the remote repository containing the property files that will be served up by the Config Service. In this example, I am pointing at my own repository on GitHub, but this could also point to a local Git repo. A local repository is useful during development and can be specified with the file prefix as follows file:///c:/dev/micor-services/git-local-config-repo .
  • Line 8 – spring.cloud.config.server.git.searchPaths   specifies the path to the properties files from the root of the repository. So, in the example above, I want to access property files in the  configuration   directory of the repository.

Running the Cloud Config Service

Now that we have the Config Server configured, it’s time to fire it up and do a quick test. You can start the application in Eclipse or on the command line the same way you’d start any other Boot app. You should see the application start on port 8888 as follows.

Cloud Config Service Startup Port 8888

Testing the Config Service

You can test the service by calling http://localhost:8888/bank-account-service/default . This GET request contains the name of the name and profile of the properties we want to load. In this instance, we are looking for the properties belonging to the  bank-account-service   and we want the properties associated with the default   profile.

When a request is received, the Config Service uses the GIT URI from the application.properties  file to perform a git clone of the remote repository. The screenshot below shows the repository being cloned to the local temp directory and includes the following line.

Adding property source: file:/C:/Users/BRIANS~1/AppData/Local/Temp/config-repo-6545303057095204707/configuration/bank-account-service.properties

阅读原文...


微信扫一扫,分享到朋友圈

Microservice Configuration: Spring Cloud Config Server Tutorial
0

DZone

Express is rewriting the body on a curl call

上一篇

Evaluating Shallow and Deep Neural Networks for Network Intrusion Detection Systems in Cyber…

下一篇

评论已经被关闭。

插入图片

热门分类

往期推荐

Microservice Configuration: Spring Cloud Config Server Tutorial

长按储存图像,分享给朋友