Centralize Configuration ด้วย Spring Cloud Config แบบง่ายๆ กันนะครับ

Chiwa Kantawong (Pea)
2 min readMar 21, 2019

--

ผมจะพยายามทำให้ง่ายที่สุดนะครับ

  1. เรามาสร้าง Configuration Service กันครับ โปรเจคส์จองเราก็เป็น maven เช่นเคยนะครับ

pom file ประมาณนี้ครับ

<?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.zengcode</groupId>
<artifactId>configuration-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/>
</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>Dalston.SR2</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-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</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>

ต่อไป ก็สร้าง ConfigurationServiceApplication.java ให้ key หลักของเราก็คือ @EnableConfigServer นะครับ

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

@SpringBootApplication
@EnableConfigServer
public class ConfigurationServiceApplication {

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

ไฟล์ bootstrap.yml

server:
port:
8888
spring:
application:
name:
configuration-service
cloud:
config:
server:
git:
uri:
/Users/chiwa.kantawong/local-git
  • *** /Users/chiwa.kantawong/local-git คือ local git repo ที่เครื่องผมนะครับ

ซึ่งผมจะมีไฟล์ชื่อ customer-service.yml ซึ่งเป็น customer-service คือชื่อ service ของเราที่จะมาใช้ configuration ไฟล์นี้นะครับ มีหน้าตาประมาณนี้ครับ

server:
port: 8082
message:
greeting: Hello! =====>>>>> customer-service configuration
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8080/eureka/
management:
security:
enabled: false

เมื่อรัน application แล้วเราสามารถตรวจสอบได้โดย http://localhost:8585/customer-service/default

2. Spring Cloud Client

จาก บทความก่อนหน้านี้ https://medium.com/@zengcode/service-discovery-ด้วย-spring-boot-netflix-eureka-ตอนที่-2-69d77755060e.

เราจะสร้างเพิ่มเติม ต่อจาก customer-service นะครับ

bootstrap.yml

spring:
application:
name:
customer-service
cloud:
config:
enabled:
true
failFast: true
uri: http://localhost:8585

ใน Controller เราสร้าง API อีกตัว

package com.zengcode.customer.controllers;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping(value = "customer")
public class CustomerController {
private final RestTemplate restTemplate;

@Autowired
public CustomerController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}

@Value("${message.greeting}")
String message;

@GetMapping("/search")
public Customer getCustomer(@RequestParam(value = "email") final String email) {
String url = "http://ADDRESS-SERVICE/customers/address?email=" + email;
return restTemplate.getForObject(url, Customer.class);
}

@GetMapping("/hi")
public String hi() {
return message;
}
}

@Data
class Customer {
private String email;
private String address;
}

เราจะเรียกใช้ centralize configuaration

@Value("${message.greeting}")
String message;

เข้า http://localhost:8082/customer/hi ก็จะได้ดังรูป

ง่ายไหมล่ะครับ

--

--

Chiwa Kantawong (Pea)
Chiwa Kantawong (Pea)

Written by Chiwa Kantawong (Pea)

Software Development Expert at Central Tech

Responses (1)