포스트

240223 기록(1)

환경

  • Spring boot 3.2.2
  • gradle
  • jdk 17
  • sdk temurin-21
  • jpa
  • msa
  • maria DB
  • RestAPI

EurekaServerApplication

build.gradle
1
2
3
4
dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
application.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
server:
  port: 8761
spring:
  application:
    name: eureka-server
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false

  server:
    wait-time-in-ms-when-sync-empty: 5
    enable-self-preservation: true
EurekaServerApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
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);
  }

}

GatewayApplication

build.gradle
1
2
3
4
5
6
7
8
9
dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'

    implementation 'org.springframework.boot:spring-boot-starter-webflux' // webflux
}
application.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
server:
  port: 8000

eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: gateway-service
  config:
    import: "optional:configserver:"
  cloud:
    gateway:
      routes:
        - id: board
          uri: http://localhost:8080/
          predicates:
            - Path=/api/boards/**

        - id: userService
          uri: http://localhost:8090/
          predicates:
            - Path=/api/users/**
GatewayApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {

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

}

BoardApplication

build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
dependencies {
  implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
  implementation 'org.springframework.boot:spring-boot-starter-web'
  implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
  compileOnly 'org.projectlombok:lombok'
  annotationProcessor 'org.projectlombok:lombok'
  testImplementation 'org.springframework.boot:spring-boot-starter-test'
  runtimeOnly 'org.mariadb.jdbc:mariadb-java-client'

  implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.1.0' // swagger. 요구사항정의서 자동화도구
  implementation 'org.springframework.boot:spring-boot-starter-webflux'
  implementation 'com.fasterxml.jackson.core:jackson-databind' // json 라이브러리 설정
  implementation 'org.springframework.boot:spring-boot-starter-log4j2' // 로그
}
application.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
server:
  port: 8080

spring:
  application:
    name: board-service
  datasource:
    driver-class-name: org.mariadb.jdbc.Driver
    url: jdbc:mariadb://localhost:3306/msaPractice
    username: root
    password: 1234

  jpa:
    properties:
      hibernate:
        ddl-auto: update
        format_sql: true
        show_sql: true
    generate-ddl: true

eureka:
  instance:
    prefer-ip-address: true
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8761/eureka/
BoardApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@SpringBootApplication
@EnableDiscoveryClient
@EnableJpaAuditing
public class BoardApplication {

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

}

UserServiceApplication

build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-mustache'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    runtimeOnly 'org.mariadb.jdbc:mariadb-java-client'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    testImplementation 'org.springframework.security:spring-security-test'

    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    implementation 'com.fasterxml.jackson.core:jackson-databind'

    implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
    implementation 'io.jsonwebtoken:jjwt-impl:0.11.5'
    implementation 'io.jsonwebtoken:jjwt-jackson:0.11.5'
}
application.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
server:
  port: 8090

spring:
  application:
    name: user-service
  datasource:
    driver-class-name: org.mariadb.jdbc.Driver  # mariaDB driver dependency 필요
    url: jdbc:mariadb://localhost:3306/msaPMember
    username: root
    password: 1234

  jpa:
    properties:
      hibernate:
        ddl-auto: create
        # ddl-auto 종류는 많은데 create는 @Entity에 변경이 발생했을 시 기존 테이블을 drop하고 다시 create
        # update는 변경이 발생하면 변경된 부분만 alter로 반영
        # 운영DB에서는 절대로 둘다 쓰면 안된다.
        # 운영DB에서는 none(아무일도 X) or validate(엔티티클래스와 테이블이 정상 매핑되는지만 파악, 만약 테이블이 없거나
        # 엔티티의 필드에 매핑되는 컬럼이 없으면 예외를 발생하며 어플리케이션 종료)
        show_sql: true
        # 작성한 sql문을 보여줌
        format_sql: true
        # 작성한 sql을 이쁘게 보여줌
    generate-ddl: true
    # ddl-auto를 사용할건지

jwt:
  header: Authorization
  #HS512 알고리즘을 사용할 것이기 때문에 512bit, 즉 64byte 이상의 secret key를 사용해야 한다.
  #echo 'silvernine-tech-spring-boot-jwt-tutorial-secret-silvernine-tech-spring-boot-jwt-tutorial-secret'|base64
  secret: c2lsdmVybmluZS10ZWNoLXNwcmluZy1ib290LWp3dC10dXRvcmlhbC1zZWNyZXQtc2lsdmVybmluZS10ZWNoLXNwcmluZy1ib290LWp3dC10dXRvcmlhbC1zZWNyZXQK
  token-validity-in-seconds: 86400

eureka:
  instance:
    prefer-ip-address: true
    # 서비스의 호스트 이름이 아닌 IP 주소를 Eureka Server에 등록하도록 지정
  client:
    register-with-eureka: true
    # 레지스트리에 자신을 등록할지 여부
    fetch-registry: true
    # 레지스트리에 있는 정보를 가져올지 여부
    service-url:
      defaultZone: http://localhost:8761/eureka/
      # EurekaServer를 바라보게 세팅
UserServiceApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@SpringBootApplication
@EnableDiscoveryClient
@EnableJpaAuditing
public class UserServiceApplication {
  public static void main(String[] args) {
      SpringApplication.run(UserServiceApplication.class, args);
  }
}
깃 주소

GitHub