Auth-Service는 유저 관련 로직을 처리하는 서비스입니다. 예를 들면 회원가입, 로그인, 유저 정보 확인 등이 있겠죠. 또한 Spring-Security를 해당 서비스에서 구현하고, API-Gateway의 필터를 이용하여 인증, 인가에 대한 부분도 구현할 예정입니다.
데이터베이스는 mariadb를 이용할 것이기 때문에 mariadb connector에 관한 디펜던시를 추가하였고, jpa 디펜던시도 추가하였습니다.
server:
port: ${port:7000}
spring:
application:
name: auth-service
jpa:
generate-ddl: true
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
hibernate:
ddl-auto: create
properties:
hibernate:
show_sql: true
format_sql: true
use_sql_comments: true
datasource:
url: "jdbc:mariadb://localhost:3306/AUTHSERVICE?useSSL=false&characterEncoding=UTF-8&serverTimezone=UTC"
username: biuea
password: password
driver-class-name: org.mariadb.jdbc.Driver
eureka:
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://127.0.0.1:8761/eureka
저는 메인 함수위에 @EnableEurekaClient 어노테이션을 통해 해당 서비스가 EurekaServer에 등록되는 서비스임을 명시했습니다. 그리고 application.yml파일에서는 jpa와 datasource 설정을 다음과 같이 명기했습니다.
그리고 ddl-auto: create는 서비스를 시작하면 자동으로 테이블을 생성하겠다는 의미입니다. 추후에 validate 옵션으로 변경하도록 하겠습니다.
우선 apigateway-service에서 pom.xml파일에 다음과 같은 디펜던시를 추가하겠습니다.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
그리고 메인 함수위에 @EnableEurekaClient 어노테이션을 달고 application.yml파일을 다음처럼 수정하여 loadbalancer를 주고 fetch-registry와 register-with-eureka에 true값을 넣어 apigateway-service도 eureka-server에 등록시키도록 하겠습니다.
spring:
application:
name: apigateway-service
cloud:
gateway:
routes:
- id: auth
uri: lb://AUTH-SERVICE
predicates:
- Path=/auth-service/**
default-filters:
- name: GlobalFilter
args:
baseMessage: Spring Cloud Gateway Global Filter
preLogger: true
postLogger: true
server:
port: ${port:8900}
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/eureka
lb://AUTH-SERVICE에서 lb는 로드발란서를 의미하며 AUTH-SERVICE는 http://localhost:8761에 등록되어있는 AUTH-SERVICE의 이름입니다. 즉 predicates아래 Path로 요청이 들어오면 해당 요청에 맞는 Eureka-Server에 등록된 서비스로 요청을 보내겠다는 의미입니다.
다음처럼 컨트롤러를 구현하고 터미널에서 discovery-service, api-service를 구동시키도록 하겠습니다. 그리고 컨트롤러에 uri가 잘 매핑되는지 값을 확인해보도록 하겠습니다.
화면처럼 apigateway-service의 port인 8900번으로 요청을 보낸 결과 /auth-service/로 된 요청이 AUTH-SERVICE에 잘 보내진 것을 알 수 있습니다.