Spring eureka
๊ณต์๋ฌธ์๊ฐ ์๊ฐ๋ณด๋ค ๋ชจ๋ ๋ด์ฉ์ ๋ค๋ฃจ๋ ๊ฒ ๊ฐ์ง๋ ์๋ค.
eureka ์ ๋ํด์ ์์๋ณด๊ธฐ ์ ์ Service discovery ์ ํ์์ฑ์ด๋ ๊ฐ๋ ์ ๋จผ์ ์์์ผ ํ๋ค. ์๋ ํฌ์คํ ์ ๋งค์ฐ ์ ์ ๋ฆฌ๊ฐ ๋์ด ์๋ค.d
Service Discovery ์ ํ์์ฑ์ ๋ํด์ ์์ฝํ์๋ฉด ์๋์ ๊ทธ๋ฆผ์ผ๋ก ์ค๋ช ํ ์ ์๋ค.

์์ ๊ณผ๋ ๋ค๋ฅด๊ฒ ์ค๋๋ ์ cloud-based MSA ์์๋ ์์ ๊ฐ์ด service instance ๋ค์ด network location ์ ๋์ ์ผ๋ก ํ ๋น๋ฐ๊ฒ ๋๋ค. ์๋ํ๋ฉด autoscaling ์ด ๋ฐ์ํ๊ธฐ ๋๋ฌธ์ด๋ค. ๊ฒฐ๊ณผ์ ์ผ๋ก client ๊ฐ ์ด๋๋ก ํต์ ํด์ผํ ์ง ๋ชจ๋ฅด๋ ์ํฉ์ด ๋ฐ์ํ ์ ์๋ค. ์ด ๋ service instance ๋ค์ network location์ ํ์ ํ๋ ํ์๋ฅผ service discovery ๋ผ๊ณ ํ ์ ์๋ค.
์ ํฌ์คํ ์์๋ service discovery pattern ์ผ๋ก client side, server side ๋ ๊ฐ์ง๋ฅผ ์ค๋ช ํ๊ณ ์๋ค.
Client-Side Discovery Pattern
client side ๋ client ๊ฐ ๋ก๋๋ฐธ๋ฐ์ฑ์ ์ฑ ์์ ๊ฐ๊ณ ์๋ ํํ์ด๋ค. service instance๋ค์ด registry ์ ๋ฑ๋กํ๋ ๊ฒ์ ๋ง๋๋ฐ ์ฌ๊ธฐ์ client ๊ฐ registry ์ ์ง์ํ ํ ์์์ ๋ก๋๋ฐธ๋ฐ์ฑ ํ๋จ์ ํด์ผํ๋ ๊ฒ์ด๋ค. Netflix OSS ๊ฐ ๋ํ์ ์ธ client side discovery pattern ์ด๋ผ๊ณ ํ๋ค.

Server-Side Discovery Pattern

๋ฐ๋ฉด์ server side discovery pattern์ client ์ ์์ฒญ์ด service ๋ฅผ ์ง์ ํฅํ์ง ์๊ณ ๋ก๋๋ฐธ๋ฐ์๋ฅผ ํฅํ๋ค. ์ฆ, ์์ฒญ์ ๋ก๋๋ฐธ๋ฐ์๋ฅผ ํตํด์ ํ๊ฒ ๋๋ค.
AWS ELB๊ฐ server side discovery pattern์ ์์๋ผ๊ณ ํ๋ค.
The AWS Elastic Load Balancer (ELB) is an example of a server-side discovery router. An ELB is commonly used to load balance external traffic from the Internet. However, you can also use an ELB to load balance traffic that is internal to a virtual private cloud (VPC). A client makes requests (HTTP or TCP) via the ELB using its DNS name. The ELB load balances the traffic among a set of registered Elastic Compute Cloud (EC2) instances or EC2 Container Service (ECS) containers. There isnโt a separate service registry. Instead, EC2 instances and ECS containers are registered with the ELB itself.
Self-Registration Pattern

์์์ ๋ค๋ค๋ฏ์ด client side๋ server side๋ instance ๊ฐ Service Registry ์ ๋ฑ๋ก์ ํ๋ค๋ ์ฌ์ค์ ๋ณํจ์ด ์๋ค. ์ด ๋ ๋ฑ๋ก์ ์ด๋ค์์ผ๋ก ํ๋์ง์ ๋ํ ๊ฒ์ Self-Registration ์ด๋ผ๋ ๊ฐ๋ ์ผ๋ก ์ค๋ช ํ๊ณ ์๋ค.
๋ด๊ฐ eureka๋ฅผ ๋ณด๋ฉด์ ๊ฐ์ฅ ๊ถ๊ธํ๊ณ ํท๊ฐ๋ ธ๋ ๋ถ๋ถ๋ ์ด ๋ถ๋ถ์ด๋ค. ๋จ์ง MSA์์ ์ฌ๋ฌ instance ๋ค์ ์ธํ๋ผ ์ ๋ณด(์์น ๋ฑ) ์ ํ ๊ณณ์ผ๋ก ๋ชจ์๋ค ์ ๋๋ก๋ง ์๊ณ ์์๊ณ , ๋๋ ์ธ๋ถ์ ์ผ๋ก ์ด๋ค ๋ฐฉ์์ผ๋ก ๋ฌด์์ ๋ชจ์ผ๋์ง๋ฅผ ์๊ณ ์ฐ๊ณ ์ถ์๋๋ฐ ๊ณต์ ๋ฌธ์์์ ๋ด๊ฐ ๋ชป์ฐพ์ ๊ฑด์ง ๋๋ฌด์ง ์ฐพ์ ์ ์์๋ค.
๊ฒฐ๊ตญ client์ registry ๋ชจ๋๋ฅผ ์ผ๋๊ณ ๋๋ค ๋ก๊ทธ ๋ ๋ฒจ์ root debug๋ก ๋๊ณ ๋ก๊ทธ๋ฅผ ํตํด์ ๋ญ ํ๋์ง ์ ์ ์์๋๋ฐ ์ด ํฌ์คํ ์์ ๊ทธ ์ฌ์ค์ ๋งค์ฐ ์ ํํ๊ฒ ์ค๋ช ํด์ฃผ๊ณ ์๋ค.
When using the selfโregistration pattern, a service instance is responsible for registering and deregistering itself with the service registry. Also, if required, a service instance sends heartbeat requests to prevent its registration from expiring. The following diagram shows the structure of this pattern.
์ ๊ทธ๋ฆผ๊ณผ ์ ์ค๋ช ์ ๋์์๋ฏ์ด service instance๊ฐ ๋ฑ๋ก์ ์ฑ ์์ ๊ฐ๊ณ ์๋ค. ๊ทธ๋ฆฌ๊ณ ๋ฑ๋ก์ ํ๋ค ์ผ์ ํ๊ฒ heartbeat์ ๋ณด๋ด์ regitstry ์์ ์ฌ๋ผ์ง๋ ๊ฒ์ ๋ฐฉ์งํ๋ค. ๊ฐ์ ๋งฅ๋ฝ์์ registry ๋ ์ผ์ ๊ธฐ๊ฐ ์์กด์ ๊ณ ๋ฅผ ํ์ง ์๋ instance ๋ ์ฃฝ์๋ค๊ณ ํ๋จํ๊ณ instance ์ ๋ณด์์ ์ญ์ ํ๋ค. ๊ทธ๋ฆผ์์ unregister() ๊ฐ ์๋๋ฐ, service instance๊ฐ ๋ด๋ ค๊ฐ๋ '๋ ์ฃฝ๋๋ค' ๋ผ๊ณ ์ ๊ณ ๊น์ง ํ๋ค. ๋ ๊ทธ๋ฆผ์์ ๋์ค์ง ์์๋๋ฐ eureka client๋ registry ์ ๋ฑ๋ก๋ ๋ชจ๋ ๋ค๋ฅธ instance ์ ๋ณด๋ค๋ ๊ฐ์ ธ๊ฐ๋ค.
์ ๋ฆฌํ์๋ฉด eureka ๊ธฐ์ค์ผ๋ก instance ๊ฐ ํ๋ ํ์๋ฅผ ๋ชฉ๋กํ ํ๋ฉด ์๋์ ๊ฐ๋ค.
register(1ํ)
heartbeat(์ง์์ ์ผ๋ก)
fetch(registry ์ ๋ฑ๋ก๋ ๋ค๋ฅธ app๋ค์ ์ ๋ณด(์ต์ด 1ํ ์ดํ delta ๋ก ๋ณํ๋ง ๋ฐ์๊ฐ))
unregister(1ํ)
์ด๋ฅผ eureka ๋ก๊ทธ์์ ๋ณด๋ฉด ์๋์ ๊ฐ๋ค. ๋ก๊ทธ๋ ๋ชจ๋ service registry ์์ ๊ธฐ๋ก๋ ๊ฒ์ด๋ค.
1. register ([POST /eureka/apps/SAMPLE-APP)
2022-07-03 12:02:53.816 DEBUG 1737 --- [nio-8761-exec-9] o.a.coyote.http11.Http11InputBuffer : Received [POST /eureka/apps/SAMPLE-APP HTTP/1.1
Accept: application/json, application/*+json
Accept-Encoding: gzip
Content-Type: application/json
Authorization: Basic YWN0dWF0b3I6MjAw
Content-Length: 968
Host: localhost:8761
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.13 (Java/11.0.15)
body ์๋ ์๋์ ๊ฐ์ ๋ด์ฉ์ด ๋ค์ด๊ฐ๋ค.
{
"instance": {
"instanceId": "192.168.35.111:sample-app:8070",
"app": "SAMPLE-APP",
"appGroupName": null,
"ipAddr": "192.168.35.111",
"sid": "na",
"homePageUrl": "http://localhost:8070/",
"statusPageUrl": "http://localhost:8070/actuator/info",
"healthCheckUrl": "http://localhost:8070/actuator/health",
"secureHealthCheckUrl": null,
"vipAddress": "sample-app",
"secureVipAddress": "sample-app",
"countryId": 1,
"dataCenterInfo": {
"@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
"name": "MyOwn"
},
"hostName": "localhost",
"status": "UP",
"overriddenStatus": "UNKNOWN",
"leaseInfo": {
"renewalIntervalInSecs": 10,
"durationInSecs": 20,
"registrationTimestamp": 0,
"lastRenewalTimestamp": 0,
"evictionTimestamp": 0,
"serviceUpTimestamp": 0
},
"isCoordinatingDiscoveryServer": false,
"lastUpdatedTimestamp": 1656817373188,
"lastDirtyTimestamp": 1656817373767,
"actionType": null,
"asgName": null,
"port": {
"$": 8070,
"@enabled": "true"
},
"securePort": {
"$": 443,
"@enabled": "false"
},
"metadata": {
"management.port": "8070"
}
}
}
]
2. heartbeat(PUT /eureka/apps/SAMPLE-APP/192.168.35.111:sample-app:8070?status=UP&lastDirtyTimestamp=1656817373767)
2022-07-03 12:07:14.316 DEBUG 1737 --- [nio-8761-exec-9] o.a.coyote.http11.Http11InputBuffer : Received [PUT /eureka/apps/SAMPLE-APP/192.168.35.111:sample-app:8070?status=UP&lastDirtyTimestamp=1656817373767 HTTP/1.1
Accept: application/json, application/*+json
Authorization: Basic YWN0dWF0b3I6MjAw
Content-Length: 0
Host: localhost:8761
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.13 (Java/11.0.15)
Cookie: JSESSIONID=D80727444AEA1B1E1998EE338DE98901
Accept-Encoding: gzip,deflate
]
3. fetch
// ์ต์ด (GET /eureka/apps/)
2022-07-03 12:32:57.799 DEBUG 1737 --- [nio-8761-exec-1] o.a.coyote.http11.Http11InputBuffer : Received [GET /eureka/apps/ HTTP/1.1
Accept: application/json, application/*+json
Authorization: Basic YWN0dWF0b3I6MjAw
Host: localhost:8761
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.13 (Java/11.0.15)
Accept-Encoding: gzip,deflate
]
// ์ต์ด ์ดํ(GET /eureka/apps/delta)
2022-07-03 12:33:17.909 DEBUG 1737 --- [io-8761-exec-10] o.a.coyote.http11.Http11InputBuffer : Received [GET /eureka/apps/delta HTTP/1.1
Accept: application/json
DiscoveryIdentity-Name: DefaultClient
DiscoveryIdentity-Version: 1.4
DiscoveryIdentity-Id: 192.168.35.111
Accept-Encoding: gzip
Host: localhost:8761
Connection: Keep-Alive
User-Agent: Java-EurekaClient/v1.10.17
Cookie: JSESSIONID=7C6D8BF2899A76C4A71ABCD5B1338BA4
Authorization: Basic YWN0dWF0b3I6MjAw
]
4. unregister
2022-07-03 12:32:50.008 DEBUG 1737 --- [nio-8761-exec-2] o.a.coyote.http11.Http11InputBuffer : Received [DELETE /eureka/apps/SAMPLE-APP/192.168.35.111:sample-app:8070 HTTP/1.1
Accept: application/json, application/*+json
Authorization: Basic YWN0dWF0b3I6MjAw
Content-Length: 0
Host: localhost:8761
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.13 (Java/11.0.15)
Cookie: JSESSIONID=0AB148142FBDFF067B548B698A243113
Accept-Encoding: gzip,deflate
]
eureka service registry ๋ฐ client ์ค์ ์ ์ ๋ฆฌ๋ ํฌ์คํ ๋ ๋ง๊ฑฐ๋์ ๊ณต์๋ฌธ์๋ง ๋ณด๊ณ ๋ฐ๋ผ๊ฐ๋ฉด ์ถฉ๋ถํ ์ฒ๋ฆฌํ ์ ์์ด์ ์ ๋ฆฌ๋ฅผ ์๋ตํ๋ค.
Last updated