목표 : UserMicroservice와 Spring Cloud Gateway 연결
server:
port: 8000
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/eureka
spring:
application:
name: apigateway-service
cloud:
gateway:
default-filters:
- name: GlobalFilter
args:
baseMessage: Spring Cloud Gateway Global Filter
preLogger: true
postLogger: true
routes:
- id: user-service
uri: lb://USER-SERVICE
predicates:
- Path=/user-service/**
Path=/user-service/**
로 요청이오면 유레카의 lb://USER-SERVICE로 매핑된다. 그래서 UserController의 url앞에 'user-service'를 붙여줘야 한다.@SpringBootApplication
public class ApigatewayServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ApigatewayServiceApplication.class, args);
}
}
유레카 > API Gateway -> UserService를 차례대로 실행한다
유레카에 접속 : http://localhost:8761/
접속을 위해 UserController를 수정해야 한다.
private final Environment env;
private final UserService userService;
private final Greeting greeting;
@GetMapping("/user-service/health_check")
public String status() {
return String.format("It's working in User Service on PORT %s",
env.getProperty("local.server.port"));
}
@GetMapping("/user-service/welcome")
public String welcome() {
//return env.getProperty("greeting.message");
return greeting.getMessage();
}
UserService로 바로 접속 테스트 성공
API Gateway로 접속하여 테스트 성공