MSA ๊ตฌ์„ฑ Discovery, Gateway, Config

์ตœ์ค€ํ˜ธยท2022๋…„ 3์›” 14์ผ
0

jayeon

๋ชฉ๋ก ๋ณด๊ธฐ
6/10
post-thumbnail

๐Ÿ”จDiscovery (Eureka) ์ƒ์„ฑ

ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ 

@SpringBootApplication
@EnableEurekaServer	//Eureka Server
public class DiscoveryApplication {
	...
}

@EnableEurekaServer Eureka Server๋กœ์จ ๊ธฐ๋Šฅ์„ ํ• ์ˆ˜ ์žˆ๋„๋ก ์„ ์–ธํ•œ๋‹ค.

server:
  port: 8761

spring:
  application:
    name: discovery

eureka:
  client: #Eureka ์„œ๋ฒ„ ์ž์ฒด๊ฐ€ Eureka์— ๋“ฑ๋ก๋˜์ง€ ์•Š๋„๋ก ์„ค์ •
    fetch-registry: false
    register-with-eureka: false

yml ์„ค์ •์„ ํ•ด์ฃผ๊ณ 

server๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ฉด Eureka๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋œ ๊ฒƒ์„ ํ™•์ธํ–ˆ๋‹ค.

https://github.com/juno-choi/jayeon-discovery

์†Œ์Šค๋Š” ๊นƒ์—์„œ ์˜ฌ๋ ค์„œ ๊ด€๋ฆฌํ•˜์ž.

๐Ÿ”จGateway ์ถ”๊ฐ€

์ด์ œ ์—”๋“œํฌ์ธํŠธ์—์„œ ์š”์ฒญ์ด ๋“ค์–ด์™”์„ ๋•Œ ์‹ค์ œ ์š”์ฒญ์„ ๊ฐ€์žฅ ์•ž์—์„œ ๋ฐ›์•„์ฃผ๊ณ  service๋กœ ๋ถ„์‚ฐ ์ฒ˜๋ฆฌํ•ด์ค„ gateway๋ฅผ ์ถ”๊ฐ€ํ•ด๋ณด์ž.

ํ”„๋กœ์ ํŠธ๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•˜๊ณ 

server:
  port: 8000

eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka

spring:
  application:  #gateway service ์ด๋ฆ„๋ฆ„
    name: gateway-service

eureka server์— ๋“ฑ๋กํ•˜๋Š” ์„ค์ •์„ ํ•ด์ฃผ์ž.

Eureka Server์— ์ž˜ ๋“ฑ๋ก๋˜์—ˆ๋‹ค.

์•„์ง ์ƒ์„ฑํ•œ ์„œ๋น„์Šค๊ฐ€ ์—†์–ด์„œ ์šฐ์„  Gateway๊ฐ€ Eureka์— ์ž˜ ๋“ฑ๋ก๋˜๋Š”์ง€๋งŒ ํ™•์ธํ–ˆ๋‹ค.

https://github.com/juno-choi/jayeon-gateway

๐Ÿ”จConfig Server ์ƒ์„ฑ

Config Server๋ฅผ ์ƒ์„ฑํ•˜๊ณ 

@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {

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

}

@EnableConfigServer ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

server:
  port: 8888

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: [git url]
          username: [git user name]
          password: [git password]

์—ฌ๊ธฐ์„œ git url์— file๋กœ ์ ์šฉํ•ด๋„ ๋˜์ง€๋งŒ ์šฐ๋ฆฌ๋Š” ํŽธ์˜์„ฑ์„ ์œ„ํ•ด git์— ์˜ฌ๋ ค๋‘˜๊ฑฐ๋‹ค ๋ฌผ๋ก  private์œผ๋กœ ์˜ฌ๋ฆด๊ฑฐ๋‹ค.

์œ„์—์„œ ์ž‘์—…ํ•œ eureka์™€ gatway์˜ yml์„ ๊ทธ๋Œ€๋กœ ๋ณต์‚ฌํ•ด์„œ ์ด๋ฆ„์„ ๊ทธ๋Œ€๋กœ ์ ์šฉํ–ˆ๋‹ค. profile๋„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์šฐ์„  ๊ธ‰ํ•œ๊ฑด ์•„๋‹ˆ๋‹ˆ ์ด๋ ‡๊ฒŒ ํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ด ํŒŒ์ผ๋“ค์„ ๋ชจ๋‘ git์— ์˜ฌ๋ ค์ฃผ์ž.

publicํ•ด๋„ ๋˜์ง€๋งŒ ์–ด์ฐจํ”ผ ์—ฌ๊ธฐ์— ์ ์„ ๋‚ด์šฉ๋“ค์€ ๋ชจ๋‘ ๋…ธ์ถœ๋˜๋ฉด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— privateํ•˜๊ฒŒ ํ–ˆ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •ํ•ด์ฃผ์—ˆ๊ณ  ์ด์ œ yml์—๋„ url๊ณผ ๋‚˜๋จธ์ง€ ์ •๋ณด๋ฅผ ์ ์–ด์ฃผ์ž.

์ •๋ณด๋ฅผ ๋ชจ๋‘ ์ž…๋ ฅ ํ›„ ์„œ๋ฒ„๋ฅผ ์‹คํ–‰์‹œ์ผœ๋ณด์ž.

{
  "name": "eureka",
  "profiles": [
    "default"
  ],
  "label": null,
  "version": "55254dd31967a078e4e03d7950fc13bcb4f118d6",
  "state": null,
  "propertySources": [
    {
      "name": "https://github.com/juno-choi/jayeon-config-yml.git/file:C:\\Users\\CODEBL~1\\AppData\\Local\\Temp\\config-repo-11282057788757329880\\eureka.yml",
      "source": {
        "server.port": 8761,
        "spring.application.name": "discovery",
        "eureka.client.fetch-registry": false,
        "eureka.client.register-with-eureka": false
      }
    }
  ]
}

๋‚ด ์„ค์ •์˜ ๊ฒฝ์šฐ http://127.0.0.1:8888/eureka/default url๋กœ ์ ‘๊ทผํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •๋ณด๊ฐ€ ๋‚˜์˜จ๋‹ค. url ๋งˆ์ง€๋ง‰ default๋Š” ์•„์ง profile ์„ค์ •์„ ์•ˆํ•ด์ฃผ์–ด์„œ ์ธ๋ฐ ์ผ๋‹จ์€ ์ด๋ ‡๊ฒŒ ์ง„ํ–‰ํ•˜๊ฒ ๋‹ค.

์ด์ œ config server๋Š” ์ •์ƒ์ ์œผ๋กœ ๊ตฌ๋™๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ์œผ๋‹ˆ Eureka์™€ Gateway์˜ ์„ค์ •์„ config server์—์„œ ๊ฐ€์ ธ์˜ค๋„๋ก ์ˆ˜์ •ํ•ด๋ณด์ž.

๐Ÿ‘Eureka, Gateway Config ์ˆ˜์ •

์šฐ์„  Eureka๋ถ€ํ„ฐ ์ˆ˜์ •ํ•ด๋ณด์ž.

Eureka ์ˆ˜์ •

๋จผ์ € Config Server์˜ ์ •๋ณด๋ฅผ ๋ฐ›์•„์˜ค๊ธฐ ์œ„ํ•ด ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•˜์ž

implementation 'org.springframework.cloud:spring-cloud-starter-config'
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'
implementation 'org.springframework.boot:spring-boot-starter-actuator'

์˜์กด์„ฑ์— config, bootstrap, actuator๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

spring:
  cloud:
    config:
      uri: http://127.0.0.1:8888
      name: eureka
#      profile:
management:
  endpoints:
    web:
      exposure:
        include: health, httptrace, busrefresh

๊ทธ ํ›„ bootstrap.ymlํŒŒ์ผ์„ application.yml๊ณผ ๋™์ผํ•œ ์œ„์น˜์— ์ƒ์„ฑํ•ด์ฃผ๊ณ  application.yml์€ ์ฃผ์„์ฒ˜๋ฆฌํ•ด๋ฒ„๋ฆฌ๊ฑฐ๋‚˜ ์ง€์›Œ๋ฒ„๋ฆฌ์ž

server๋ฅผ ๊ธฐ๋™ํ•ด๋ณด์ž.

2022-03-14 14:24:00.115  INFO 10780 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://127.0.0.1:8888
2022-03-14 14:24:01.095  INFO 10780 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=eureka, profiles=[default], label=null, version=55254dd31967a078e4e03d7950fc13bcb4f118d6, state=null
2022-03-14 14:24:01.096  INFO 10780 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-configClient'}, BootstrapPropertySource {name='bootstrapProperties-https://github.com/juno-choi/jayeon-config-yml.git/file:C:\Users\CODEBL~1\AppData\Local\Temp\config-repo-11282057788757329880\eureka.yml'}]
2022-03-14 14:24:01.102  INFO 10780 --- [           main] c.j.j.JayeonDiscoveryApplication         : No active profile set, falling back to 1 default profile: "default"

์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋กœ๊ทธ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์šฐ๋ฆฌ๊ฐ€ ์ง€์ •ํ•œ config server๋กœ ์š”์ฒญํ•ด์„œ ์ •๋ณด๋ฅผ ์ง€์ •ํ•œ eureka.yml๋กœ ์ฝ์–ด์™”๋‹ค๋Š” ๋‚ด์šฉ์ด๋‹ค.

์„œ๋ฒ„๋„ ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋˜์—ˆ๋‹ค. ์ด์ œ ์šฐ๋ฆฌ๋Š” ์„ค์ •์€ config server์— ์žˆ๊ณ  git์—์„œ๋„ privateํ•˜๊ฒŒ ๊ด€๋ฆฌ๋˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ ๋” ์ด์ƒ ์„ค์ • ์ •๋ณด๊ฐ€ ๋…ธ์ถœ๋ ๊ฒƒ์— ๋Œ€ํ•œ ๊ฑฑ์ •๊ณผ container๋กœ ๋„์šธ๋•Œ๋„ ์„ค์ • ํŒŒ์ผ์„ ํ•ญ์ƒ ๋ช…์‹œํ•ด์ค„ ํ•„์š”๊ฐ€ ์—†์–ด์กŒ๋‹ค.

Gateway ์ˆ˜์ •

๋˜‘๊ฐ™์ด ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•ด์ฃผ๊ณ 

spring:
  cloud:
    config:
      uri: http://127.0.0.1:8888
      name: gateway
#      profile:
management:
  endpoints:
    web:
      exposure:
        include: health, httptrace, busrefresh

gateway ๋ถ€๋ถ„๋งŒ ์‹ ๊ฒฝ์“ฐ๊ณ  bootstrap.yml์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋˜์—ˆ๋‹ค.

Eureka์—์„œ๋„ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

์œ„์—์„œ ์ถ”๊ฐ€ํ•ด์ค€ ๋‚ด์šฉ์ค‘ busrefresh๋Š” message queue ๋ฏธ๋“ค์›จ์–ด๋ฅผ ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ํ•˜๋Š”๋ฐ ์„œ๋น„์Šค๋“ค์ด ์ถ”๊ฐ€๊ฐ€ ๋˜์ง€ ์•Š์•„์„œ ๋‹น์žฅ์— ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†์–ด์„œ ์ผ๋‹จ ์ง„ํ–‰ํ–ˆ๋‹ค. ๋‹ค์Œ์— ์„œ๋น„์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด์„œ message queue๋กœ RabbitMQ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  DB ๋ฐ์ดํ„ฐ ๋™๊ธฐํ™”๋ฅผ ์œ„ํ•ด Kafka๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

profile
ํ•ด๋‹น ์ฃผ์†Œ๋กœ ์ด์ „ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. https://ililil9482.tistory.com

0๊ฐœ์˜ ๋Œ“๊ธ€