Discovery
: 다른 서비스의 연결 정보를 찾는 것
Registry
: 서비스의 연결 정보를 등록하는 것
Eureka server
Eureka client
@EnableEureKaServer or @ EnableEurekaClient 추가
# Eureka Server
// application.properties
server.port=8761
spring.application.name=discovery-service
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
# Eureka Client
// application.properties
server.port=0
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
spring.application.name=my-service1 // 2는 바꿔서
eureka.instance.instance-id=${spring.application.name}:${spring.application.instance_id:${random.value}}
# Gateway
//application.yml
server:
port: 8080
eureka:
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://localhost:8761/eureka
instance:
instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
spring:
application:
name: gateway-service
cloud:
gateway:
routes:
- id: my-serv1
uri: lb://MY-SERVICE1
predicates:
- Path=/serv1/**
- id: my-serv2
uri: lb://MY-SERVICE2
predicates:
- Path=/serv2/**
# Mycontroller // client에
@RestController
public class MyController {
@GetMapping("/serv1/test")
public String test() {
return "test";
}
}
# Filter, application.yml 에 추가
cloud:
gateway:
routes:
- id: my-serv1
uri: lb://MY-SERVICE1
predicates:
- Path=/serv1/**
filters:
- RewritePath=/serv1/?(?<segment>.*), /$\{segment}
# Logging Filter
# Global Filter
# Custom Filter 등등 이 있음
feign : RestTemplate 호출 등을 JPA Repository 처럼 interface로 단순히 추상화 한 프로젝트
# build.gradle 에 추가
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
@EnableFeignClients // @FeignClient가 있는 interface를 찾아서 등록
# FeignServiceBRemoteService 인터페이스 추가
@FeignClient(name = "MY-SERVICE2")
public interface FeignServiceBRemoteService {
@RequestMapping("info/{test}")
String getServiceBInfo(@PathVariable("test") String test);
}
# Client controller 변경
@RequiredArgsConstructor
@RestController
public class MyController {
private final FeignServiceBRemoteService feignServiceBRemoteService;
@GetMapping("/test")
public String test() {
return feignServiceBRemoteService.getServiceBInfo("service1");
}
}
# Client2 controller 변경
@RequiredArgsConstructor
@RestController
public class MyController{
@GetMapping("/info/{test}")
publicStringtest(@PathVariable Stringtest) {
return "service2에서 응답"+test;
}
}