Java Spring - Spring Cloud Config Client

I. Giới thiệu

Java Srping cung cấp tính năng cho phép quản lý file config tập trung thông qua các hệ thống quản lý version như git.

Việc deploy và thay đổi config sẽ dễ dàng hơn khi cloud config hỗ trợ lấy config theo môi trường.

Spring Cloud Config Clientsẽ gọi api từ Cloud Config Server để lấy config theo môi trường.

II. Khởi tạo project Cloud Config Client
1. pom.xml
        
    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.5.3</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.cloudconfig</groupId>
        <artifactId>cloud-config-client</artifactId>
        <version>1.0</version>
        <name>cloud-config-client</name>
        <description>cloud config client</description>
        <properties>
            <java.version>1.8</java.version>
            <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <spring-cloud.version>2020.0.3</spring-cloud.version>
        </properties>
    
        <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>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-bootstrap</artifactId>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
            
        
      
2. bootstrap.yml
        
    spring:
        application:
            name: cloud-config-client
        profiles:
            active: dev
        cloud:
            config:
                enabled: true
                uri: http://localhost:8888 # đổi sang config server đang chạy
                username: root
                password: pass@2021
                request-read-timeout: 10000
        
      

spring.cloud.config.enabled: enable cloud config server

spring.cloud.config.uri: url đến Cloud Config Server đang có

spring.cloud.config.username: username đã config trong Cloud Config Server

spring.cloud.config.password: password đã config trong Cloud Config Server đang có

3. {application}-{profile}.yml

Bước này ta sẽ đẩy file config lên git đã config trong Cloud Config Server spring.cloud.server.git.uri

Ví dụ ta đẩy 2 file cloud-config-client-dev.yml

        
    server:
        port: 8901 # tự tùy chỉnh 
    env: dev  
    logging:
        file:
            path: /var/log/${spring.application.name}
        level:
            org.springframework: info
            root: info
        
      

Và file cloud-config-client-sit.yml

        
    server:
        port: 8901 # tự tùy chỉnh 
    env: sit  
    logging:
        file:
            path: /var/log/${spring.application.name}
        level:
            org.springframework: info
            root: info
        
      

2 file khác nhau phần env

3. logback-spring.xml
        
    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <springProperty scope="context" name="springAppName"
            source="spring.application.name" />
        <springProperty scope="context" name="LOGS"
            source="logging.file.path"></springProperty>
    
        <appender name="Console"
            class="ch.qos.logback.core.ConsoleAppender">
            <layout class="ch.qos.logback.classic.PatternLayout">
                <Pattern>
                    %black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
                </Pattern>
            </layout>
        </appender>
    
        <appender name="RollingFile"
            class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${LOGS}/${springAppName}-logger.log</file>
            <encoder
                class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
            </encoder>
    
            <rollingPolicy
                class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!-- rollover daily and when the file reaches 10 MegaBytes -->
                <fileNamePattern>${LOGS}/archived/${springAppName}-logger-%d{yyyy-MM-dd}.%i.log
                </fileNamePattern>
                <timeBasedFileNamingAndTriggeringPolicy
                    class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <maxFileSize>10MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
            </rollingPolicy>
        </appender>
    
        <!-- LOG everything at INFO level -->
        <root level="info">
            <appender-ref ref="RollingFile" />
            <appender-ref ref="Console" />
        </root>
    
        <!-- LOG at TRACE level -->
        <logger name="com.cloudconfigclient" level="trace" additivity="false">
            <appender-ref ref="RollingFile" />
            <appender-ref ref="Console" />
        </logger>
    
    </configuration>
        
      
4. ConfigClient Boot
        
    package com.cloudconfigclient;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class ConfigClient {
    
        public static void main(String[] arguments) {
            SpringApplication.run(ConfigClient.class, arguments);
        }
    
    }
            
        
      
5. Tiến hành build chạy project (chạy cả cloud config server)

Chú ý tham số cấu hình spring.profiles.active trong file bootstrap (hoặc truyền tham số profile active khi chạy app spring) chính là profile cần lấy trên cloud config theo mẫu

{application}-{profile} trong ví dụ này là cloud-config-client-dev và cloud-config-client-sit

Trong đó:

profile: active profile

Nhận xét

Bài đăng phổ biến từ blog này

IBM BPM - Date

BPM WebSphere - Create Datasource (Connect to DB via JDBC)

IBM BPM - Error Report the following identifier to your administrator for further investigation