Eureka and Zuul - TheOpenCloudEngine/uEngine-cloud GitHub Wiki
์ด๋ฒ ์๊ฐ์ Registry Service์ Proxy Service๋ฅผ ์ค์ ํ๋ ๋ฐฉ๋ฒ์ ๋ํ์ฌ ๊ธฐ์ ํ๊ณ ์ ํ๋ค.
Registry
-> ๋ง์ ์์ ์๋น์ค๋ค๊ฐ์ ์ค๋ํฌ์ธํธ๋ฅผ ์ฐพ๊ณ , ์๋น์ค์ ์ํ๋ฅผ ์ด๋๊ฐ์ ๋ฑ๋กํ๋ ๊ธฐ๋ฅ.
Proxy
-> ํด๋ผ์ด์ธํธ๊ฐ ์์ ์ ํตํด์ ๋ค๋ฅธ ๋คํธ์ํฌ ์๋น์ค์ ๊ฐ์ ์ ์ผ๋ก ์ ์ํ ์ ์๊ฒ ํด์ฃผ๋ ๊ธฐ๋ฅ.
Eureka
-> netflix OSS์์ ๊ฐ๋ฐํ Registry Service libary.
Zuul
-> netflix OSS์์ ๊ฐ๋ฐํ Proxy Service libary.
์์ libary๋ฅผ ์ฌ์ฉํ์ฌ ์์ฝ๊ฒ Registry server์ Proxy server๋ฅผ ๊ตฌ์ฑํ๋ ๋ฐฉ๋ฒ์ ์ค๋ช
ํ๋ค.
spring cloud ๋ spring boot + library ๋ก ๊ตฌ์ฑ๋ ํ๋ก์ ํธ๋ค์ ๋ชจ์
์ฌ๊ธฐ์ ์ฌ์ฉํ๋ ์์ ๋ ์๋์ github ์์ ๋ค์ด๋ก๋ ๊ฐ๋ฅํ๋ค.
https://github.com/uengine-oss/msa-tutorial-class-management-msa
๋จผ์ ๋ค์ด๋ก๋๋ ์์ค์ฝ๋์ registry-service ๋ฅผ ์ดํด๋ณด์.
https://github.com/uengine-oss/msa-tutorial-class-management-msa/tree/master/registry-service
ํ์ผ์ ๊ตฌ์กฐ๋ ๊ฐ๋จํ๋ค.
RegistryServiceApplication
๊ณผapplication.yml
๋๊ฐ์ด๋ค.
@SpringBootApplication
@EnableEurekaServer
public class RegistryServiceApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(RegistryServiceApplication.class).web(true).run(args);
}
}
- @SpringBootApplication : ์ผ๋ฐ์ ์ธ ์คํ๋ง ๋ถํธ ์ดํ๋ฆฌ์ผ์ด์ ๊ณผ ๊ฐ๋ค.
- @EnableEurekaServer : ์ด ์๋น์ค๊ฐ Registry server ๋ผ๊ณ ์ค์ ํด์ฃผ๋ Annotation ์ด๋ค.
์ด๋ ๊ฒ ์ค์ ์ ํ ๊ฒฝ์ฐ ์ถํ Registry server์ ํน์ ํ ๊ธฐ๋ฅ์ ์ถ๊ฐ ํ๊ณ ์ถ์๋,
source code level์์ ์ปจํธ๋กค์ด ๊ฐ๋ฅํ๋ค๋ ์ฅ์ ์ด ์๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก spring-boot Application ์ ์ปค์คํฐ ๋ง์ด์ง ๋ ๋ฒจ์ ์ฒซ๋ฒ์งธ๋ก properties ํ์ผ์ด๋ค.
์ฌ๊ธฐ ์์ ์์๋ application.yml ํ์ผ์ ์ผ๋๋ฐ, properties ์ yml ํ์ผ ๋ชจ๋ ์ฐ์ ์ผ๋ก ์ฝ์ด๋ค์ธ๋ค.
์ต๊ทผ cloud computing ์ชฝ์ yml ํ์ผ์ ๋ง์ด ์ฌ์ฉ์ค์ด๋ค.
yml ํ์ผ์ ์ฌ๋์ด ์ฝ๊ณ ์ฐ๊ธฐ ์ข์ ํ์์ผ๋ก (json์ฒ๋ผ) key and value ๋ฐฉ์์ผ๋ก
depth๋ฅผ ์ฃผ์ด์ property๋ฅผ ๊ด๋ฆฌํ๊ธฐ๊ฐ ํธํ๋ค.
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
enableSelfPreservation: false
์์ server, port ๋ถ๋ถ์ properties ํ์ผ๋ก ๋ณ๊ฒฝํ๋ค๋ฉด
server.port=8761
์ด๋ฐ์์ผ๋ก ๋ณ๊ฒฝ์ด ๊ฐ๋ฅํ๋ค.
๋ง์ดํฌ๋ก ์๋น์ค๋ Registry ๊ฐ ํญ์ ์์ด์ผ ํ๋ค.
๊ทธ๋ฆฌ๊ณ Registry๋ฅผ ์ธ์ํ๊ธฐ ์ํ์ฌ Registry ์ฃผ์๋ ๊ฐ ์๋น์ค์์ ๊ฐ์ง๊ณ ์์ด์ผ ํ๋ค.
์๋๋ ๋ค๋ฅธ ๋ง์ดํฌ๋ก ์๋น์ค์์ Registry ์ฃผ์๋ฅผ ์ค์ ํ ์์ ์ด๋ค.
eureka:
client:
enabled: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/
healthcheck:
enabled: true
์ค์ ์ ์ฌ๋ฌ ์ต์
์ด ์์ง๋ง ์ต์ํ์ ์ต์
๋ง ์ค์ ํ์๊ณ , ๊ฐ ์ค์ ๊ฐ์ ์ค๋ช
ํ์๋ฉด
eureka.client.enabled : true / false -> ์ ๋ ์นด ํด๋ผ์ด์ธํธ๋ฅผ ๋ฑ๋ก/ํด์ ํ๊ฒ ๋ค.
eureka.client.defaultZone : ์ ๋ ์นด ์๋ฒ ์ฃผ์
eureka.client.healthcheck.enabled : ์ ๋ ์นด ์๋ฒ์ ์ํ๋ฅผ ๊ณ์ ์ดํด๋ณด๊ฒ ๋ค.
์ฐธ๊ณ
https://cloud.spring.io/spring-cloud-netflix/multi/multi__service_discovery_eureka_clients.html
๊ทธ ์ด์ ๋ Registry Server๊ฐ ์ฃฝ์ผ๋ฉด ๋ง์ดํฌ๋ก ์๋น์ค๊ฐ ์ฐ๊ฒฐ์ด ์์ ์๋๊ธฐ ๋๋ฌธ์ด๋ค.
eureka ๋ก cluster ๋ฅผ ํ๊ธฐ ๊ฐ์ฅ ์ฌ์ด ๋ฐฉ๋ฒ์ eureka server๋ฅผ ์ฌ๋ฌ๋ ๋์ด ํ,
๊ฐ ๋ง์ดํฌ๋ก ์๋น์ค์ eureka ์ค์ ์์ ์๋์ ๊ฐ์ด ์ฃผ์๋ฅผ ์ฌ๋ฌ๊ฐ ๋์ดํ๋ ๋ฐฉ๋ฒ์ด๋ค.
defaultZone: http://127.0.0.1:8761/eureka/,http://127.0.0.1:8762/eureka/
๋ค์ด๋ก๋๋ ์์ค์ฝ๋์ proxy-service ๋ฅผ ์ดํด๋ณด์.
https://github.com/uengine-oss/msa-tutorial-class-management-msa/tree/master/proxy-service
eureka์ ๋ง์ฐฌ๊ฐ์ง๋ก ํ์ผ์ ๊ตฌ์กฐ๋ ๊ฐ๋จํ๋ค.
GatewayApplication
๊ณผapplication.yml
๋๊ฐ์ด๋ค.
@EnableZuulProxy
@EnableDiscoveryClient
@EnableEurekaClient
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
- @SpringBootApplication : ์ผ๋ฐ์ ์ธ ์คํ๋ง ๋ถํธ ์ดํ๋ฆฌ์ผ์ด์ ๊ณผ ๊ฐ๋ค.
- @EnableZuulProxy : ์ด ์๋น์ค๋ Proxy server ๋ผ๊ณ ์ค์ ํด์ฃผ๋ Annotation ์ด๋ค.
- @EnableEurekaClient : ์ด ์๋น์ค๋ Registry client ๋ผ๊ณ ์ค์ ํด์ฃผ๋ Annotation ์ด๋ค.
@EnableDiscoveryClient
๋ @EnableEurekaClient
๊ฐ ์๋ค๋ฉด ์ฌ์ฉํ ํ์๊ฐ ์๋ค.
@EnableDiscoveryClient
๋ฅผ ์ฌ์ฉํ์ฌ Eureka์ client๋ก ๋ฑ๋กํ ๊ฒ์ด @EnableEurekaClient
๋ค.
zuul:
ignored-headers: Access-Control-Allow-Credentials, Access-Control-Allow-Origin
addProxyHeaders: true
routes:
courses:
path: /courses/**
serviceId: course
stripPrefix: false
์ค์ ํ์ผ์ ์ดํด๋ณด๋ฉด zuul.routes ์ ํ๊ธฐ์ํ์ฌ courses ๋ผ๊ณ ํ๋ path๋ก ๋ค์ด์ฌ์ ์
eureka์ ๋ฑ๋ก๋์ด์๋ course ๋ผ๋ ์๋น์ค๋ฅผ ์ฐพ์์ redirect ํด๋ฌ๋ผ๋ ์๋ฏธ์ด๋ค.
stripPrefix ๋ routing ํ๊ธฐ ์ ์ ๊ฒฝ๋ก์์ ์ ๋์ด๋ฅผ ์ ๊ฑฐํ ์ง ์ฌ๋ถ๋ฅผ ๋ํ๋ด๋ ํ๋๊ทธ๋ค.
๊ทธ๋ฌ๋ zuul์ ์ด์๊ฐ์ routes ์ค์ ์ด ์์ด๋ ๊ธฐ๋ณธ์ ์ผ๋ก routing์ด ๊ฐ๋ฅํ๋ค.
url์ ์ฒซ๋ฒ์งธ path๋ serviceId๋ก ์ธ์์ ํ๋๊ฒ์ด zuul์ ๊ธฐ๋ณธ ์ค์ ์ด๋ค.
zuul.routes ์ค์ ์ ํ๋ฉด ์๋์ ๊ฐ์ด ๋๋ฒ์ path๊ฐ ๋ง๋ค์ด ์ง๋ ๊ฒ์ด๋ค.
http localhost:8080/course/courses
http localhost:8080/courses
๊ทธ๋ฌ๋ zuul์ front-end ์์ ์ ๊ทผ์ ํ๋ ์๋น์ค ์ด๊ธฐ๋๋ฌธ์, zuul.routes๋ฅผ ์ค์ ์ํ๊ณ ,
localhost:8080/course/courses
์ด์ ๊ฐ์ด ์ด๋ค๋ฉด ๊ธฐ์กด์ monolith ์์ ์ฌ์ฉํ๋
path์ ์์ ์ด ๋ฌ๋ผ์ง๊ฒ ๋๋ค. ๊ทธ๋์ ์ผ์ผ์ด ์จ์ฃผ๋๊ฒ ์ข์ ๋ฐฉ๋ฒ์ด๋ค.
http://cloud.spring.io/spring-cloud-netflix/multi/multi__router_and_filter_zuul.html
์์ url๋ก ์ ๊ทผํ๋ฉด netflix์์ zuul์ ์ด๋ป๊ฒ ์ฌ์ฉํ๋์ง ๋์จ๋ค.
์ธ์ฆ, Stress Testing, Canary Testing(A/B test ์ฒ๋ผ ๊ธฐ์กด๋ฒ์ ๊ณผ ์๋ฒ์ ์ ํธ์ถํ๋ api๊ฐ ๋๊ฐ์ด ๋ฐ์ํ๋์ง ํ ์คํธ) ๋ฑ์ด ์๋ค.