dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
}
spring.application.name=server
server.port=19090
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
@SpringBootApplication
@EnableEurekaServer
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
}
spring:
application:
name: order-service
server:
port: 19091
eureka:
client:
service-url:
defaultZone: http://localhost:19090/eureka/
spring:
application:
name: product-service
server:
port: 19092
eureka:
client:
service-url:
defaultZone: http://localhost:19090/eureka/
Eureka Client 활성화
@EnableFeignClients 어노테이션 추가 !
Eureka Dashboard에서 확인
Instances currently registered with Eureka 부분에 order-service, product-service가 등록된 것 확인!
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
```java
@Autowired
private RestTemplate restTemplate;
public ProductResponse getProductById(Long id) {
return restTemplate.getForObject(
"http://product-service/products/" + id,
ProductResponse.class
);
}
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
}
@SpringBootApplication
@EnableFeignClients
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
@FeignClient(name="product-service")
public interface ProductClient {
@GetMapping("/product/{id}")
String getProduct(@PathVariable("id") String id);
}
@Service
@RequiredArgsConstructor
public class OrderService {
private final ProductClient productClient;
public String getProductInfo(String productId){
return productClient.getProduct(productId);
}
}
| 항목 | RestTemplate | Feign Client |
|---|---|---|
| 코드 복잡성 | 코드가 많아짐 | 인터페이스만 작성하면 됨 |
| Eureka 연동 | 수동 설정 필요 | 자동 연동 가능 |
| 로드 밸런싱 | Ribbon 필요 | 자동 지원 |
| JSON 변환 | 수동 전환 필요 | 자동 변환 |
| 사용 방식 | 명시적으로HTTP요청 | 선언형 인터페이스 방식 |