private static final String url = "http://localhost:8082/products/";
private final RestTemplate restTemplate;
@Override
public String getProductInfo(String productId) {
return restTemplate.getForObject(url + productId, String.class);
}
@FeignClient(name = "product", url = "http://localhost:8082/")
public interface FeignProductRemoteService {
@RequestMapping(path = "/products/{productId}")
String getProductInfo(@PathVariable("productId") String productId);
}
- 스프링 초창기에 사용
- 비동기 처리(Functional api, 리액트자바 등)가 안 되기 때문에 최근에 잘 사용하지 않음
- 서비스 이름으로 호출
- Health Check
- Spring Cloud Ribbon은 Spring Boot 2.4에서 Maintenance 상태 (더이상 지원하지 않는 보류 상태)
- Spring Boot 2.4 이상에서 지원 안 함
- lombok, spring web, netflix-eureka-client
- lombok, spring web, netflix-eureka-client
# yml 설정
server:
port: 8081
spring:
application:
name: my-first-service
eureka:
client:
fetch-registry: false
register-with-eureka: false
// Controller
package com.example.secondservice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/")
public class SecondController {
@GetMapping("/welcome")
public String welcom(){
return "Welcome to the First service";
}
}
server:
port: 8000
spring:
application:
name: my-zuul-service
zuul:
routes:
first-service:
path: /first-service/**
url: http://localhost:8081
second-service:
path: /second-service/**
url: http://localhost:8082
package com.example.zuulservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
public class ZuulServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulServiceApplication.class, args);
}
}
package com.example.zuulservice.filter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import com.netflix.zuul.context.RequestContext;
import javax.servlet.http.HttpServletRequest;
@Component
@Slf4j
public class ZuulLogginFilter extends ZuulFilter {
// 사전 필터인지 사후 필터인지 정의
@Override
public String filterType() {
return "pre"; // pre 사전필터
}
// 여러개의 필터가 있을경우 필터 순서
@Override
public int filterOrder() {
return 1;
}
// 필터 사용 여부
@Override
public boolean shouldFilter() {
return true;
}
// 어디에서 어떤 요청이 들어왔다는 로그 필터
@Override
public Object run() throws ZuulException {
log.info("******** printing logs: ");
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
log.info("******** " + request.getRequestURI());
return null;
}
}