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

Chiwa Kantawong (Pea)
3 min readMar 20, 2019

--

ตอนที่แล้วเราได้สร้าง Eureka Server เพื่อให้ Service ต่างๆ มา register กันแล้ว ตอนนี้เราจะมาสร้าง service ง่ายๆ เพื่อไป register กับ Eureka Server กันครับ
เช่นเคยนะครับ เรามาสร้างโปรเจคส์ Maven ที่มีชื่อว่า customer-service กันครับ

โครงสร้างโปรเจคส์ประมาณนี้นะครับ

  1. 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>customer-service</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</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-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
</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. CustomerServiceApplication.java

package com.zengcode.customer;

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

@SpringBootApplication
@EnableDiscoveryClient
public class CustomerServiceApplication {

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

3. application.yml

server:
port:
8082

spring:
application:
name:
product-service

eureka:
client:
serviceUrl:
defaultZone:
http://localhost:8888/eureka/
healthcheck:
enabled:
true
instance:
leaseRenewalIntervalInSeconds:
5
lease-expiration-duration-in-seconds: 5

4. CustomerController.java

package com.zengcode.customer.controllers;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
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;
}

@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);
}
}

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

5. ApplicatioConfiguration.java

package com.zengcode.customer.configurations;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ApplicationConfiguration {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

แล้วก็พร้อมรัน application ของเราแล้วอย่าลืมรัน Eureka Server ที่เราสร้างเมื่อตอนที่แล้วด้วยนะครับ เมื่อเรารันสำเร็จเราก็ไปตรวจดูว่า Application ของเรา Register ไปที่ Eureka Server หรือยังนะครับ

เราจะเห็นว่ามี Application CUSTOMER-SERVICE ได้ registered เข้ามาแล้วนะครับ

และตอนนี้ถ้าเราเรียกใช้ API
http://localhost:8082/customer/search?email=pea@google.com

มันจะ error นะครับ เพราะว่าเรายังไม่ได้สร้าง ADDRESS-SERVICE ขึ้นมาเลย

ต่อไปเรามาสร้าง Address Service กันครับ ก็คล้ายกับ customer service เลยนะครับ ส่วนที่ต่างกันก็คือ

  1. application.yml
server:
port:
8083

spring:
application:
name:
address-service

eureka:
client:
serviceUrl:
defaultZone:
http://localhost:8080/eureka/

2. Address Controller

ackage com.zengcode.customer.controllers;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
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 = "customers")
public class AddressController {
private final RestTemplate restTemplate;

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

@GetMapping("/address")
public Customer getCustomerAddress(@RequestParam(value = "email") final String email) {
Customer customer = new Customer();
customer.setEmail(email);
customer.setAddress("123 Dokkhamtai Phayao");
return customer;
}
}

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

เมื่อเรารันมันขึ้นมา และเราลองไปดู ที่ Eureka Server จองเราจะเป็นแบบนี้ครับ

จะมี application registered มาสองตัวนะครับ

ต่อไปเราเรียก http://localhost:8082/customer/search?email=pea@google.com

เราจะได้

สุดยอดไปเลยป่ะล่ะ

String url = "http://ADDRESS-SERVICE/customers/address?email=" + email;

ADDRESS-SERVICE จะถูก resolved ให้ด้วย Eureka Server ของเรานั้นเอง ซึ่งถ้ามี ADDRESS-SERVICE หลาย enpoint มา registerd มันก็จะเลือกเอาตัวที่ใช้งานได้ให้เราทันที ซึ่งผมได้ รัน Address Service ขึ้นมาอีกตัวจะเห็นได้จาก Eureka

สำหรับบทความนี้ก็ขอจบแต่เพียงเท่านี้นะครับ ไว้เจอกันใหม่นะครับ

--

--

Chiwa Kantawong (Pea)
Chiwa Kantawong (Pea)

Written by Chiwa Kantawong (Pea)

Software Development Expert at Central Tech

No responses yet