Service Discovery ด้วย Spring Boot + Netflix Eureka (ตอนที่ 1)

Chiwa Kantawong (Pea)
2 min readMar 20, 2019

--

Service Discovery สามารถสร้างขึ้นมาได้หลากหลายแบบ สำหรับบทความนี้เราจะสร้างด้วย Netflix Eureka ซึ่งเป็น client-server model โดยที่จะมี Eureka Servers และ Eureka Clients ได้หลายตัว โดยที่ client จะมาทำการ registry กับ Servers และ Servers ก็จะทำหน้าที่ในการหา endpoint ในการเชื่อต่อ ของ Clients ให้เอง และ Eureka เองยังช่วยหา dynamic ของ service addresses และในบางกรณีที่มี services down ไป Eureka ยังช่วย client เรียกไปยัง service ที่ยังทำงานอยู่ได้อย่างถูกต้อง

สำหรับตอนนี้เราจะมาสร้าง Eureka Server กันนะครับ โดย application เราจะเป็น Maven Project นะครับ โครงสร้าโปรเจคส์เราจะประมาณนี้

  1. 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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zengcode</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</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>

2. EurekaServerApplication.java

package com.zengcode.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}

3. application.yml

server:
port:
8080

spring:
application:
name:
eurika-service

eureka:
instance:
hostname:
localhost
client:
registerWithEureka:
false
fetchRegistry: false
serviceUrl:
defaultZone:
http://${eureka.instance.hostname}:${server.port}/eureka/

เมื่อเรารัน Application ขึ้นมาก็สามารถตรวจสอบได้ว่าทำงานได้ไหมโดยไปที่ http://localhost:8080/ เราก็จะเจอหน้าตาแบบนี้

ซึ่งตอนนี้ Instances currently registered with Eureka จะไม่มีเลยสักตัว ซึ่งตอนหน้าเราจะมาสร้าง application ที่จะมา register กับ Eureka และก็เรียกใช้กันเองนะครับ

ไปต่อตอนที่สองกันครับ

https://medium.com/@zengcode/service-discovery-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-spring-boot-netflix-eureka-%E0%B8%95%E0%B8%AD%E0%B8%99%E0%B8%97%E0%B8%B5%E0%B9%88-2-69d77755060e

--

--

Chiwa Kantawong (Pea)
Chiwa Kantawong (Pea)

Written by Chiwa Kantawong (Pea)

Software Development Expert at Central Tech

Responses (1)