✨ 远程调用-Feign拦截器
This commit is contained in:
parent
44457c9971
commit
ccc68630f0
|
@ -0,0 +1,17 @@
|
|||
package cn.bunny.service.interceptor;
|
||||
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Component
|
||||
public class XTokenRequestInterceptor implements RequestInterceptor {
|
||||
|
||||
@Override
|
||||
public void apply(RequestTemplate requestTemplate) {
|
||||
requestTemplate.header("X-Token", UUID.randomUUID().toString());
|
||||
}
|
||||
|
||||
}
|
|
@ -13,4 +13,6 @@ spring:
|
|||
service-product:
|
||||
logger-level: full
|
||||
connect-timeout: 3000
|
||||
read-timeout: 5000 # 最多等待对方 5s
|
||||
read-timeout: 5000 # 最多等待对方 5s
|
||||
request-interceptors:
|
||||
- cn.bunny.service.interceptor.XTokenRequestInterceptor
|
|
@ -3,14 +3,13 @@ package cn.bunny.service.controller;
|
|||
import cn.bunny.model.product.bean.Product;
|
||||
import cn.bunny.service.service.ProductService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/product")
|
||||
@RequiredArgsConstructor
|
||||
|
@ -20,12 +19,14 @@ public class ProductController {
|
|||
|
||||
@Operation(summary = "根据id查询商品")
|
||||
@GetMapping("{id}")
|
||||
public Product getProduct(@PathVariable("id") Long productId) {
|
||||
try {
|
||||
TimeUnit.SECONDS.sleep(6);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
public Product getProduct(@PathVariable("id") Long productId, HttpServletRequest request) {
|
||||
// try {
|
||||
// TimeUnit.SECONDS.sleep(6);
|
||||
// } catch (InterruptedException e) {
|
||||
// throw new RuntimeException(e);
|
||||
// }
|
||||
|
||||
System.out.println("request-X-Token: " + request.getHeader("X-Token"));
|
||||
return productService.getProductById(productId);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue