✨ 远程调用-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:
|
service-product:
|
||||||
logger-level: full
|
logger-level: full
|
||||||
connect-timeout: 3000
|
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.model.product.bean.Product;
|
||||||
import cn.bunny.service.service.ProductService;
|
import cn.bunny.service.service.ProductService;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/product")
|
@RequestMapping("/api/product")
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
|
@ -20,12 +19,14 @@ public class ProductController {
|
||||||
|
|
||||||
@Operation(summary = "根据id查询商品")
|
@Operation(summary = "根据id查询商品")
|
||||||
@GetMapping("{id}")
|
@GetMapping("{id}")
|
||||||
public Product getProduct(@PathVariable("id") Long productId) {
|
public Product getProduct(@PathVariable("id") Long productId, HttpServletRequest request) {
|
||||||
try {
|
// try {
|
||||||
TimeUnit.SECONDS.sleep(6);
|
// TimeUnit.SECONDS.sleep(6);
|
||||||
} catch (InterruptedException e) {
|
// } catch (InterruptedException e) {
|
||||||
throw new RuntimeException(e);
|
// throw new RuntimeException(e);
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
System.out.println("request-X-Token: " + request.getHeader("X-Token"));
|
||||||
return productService.getProductById(productId);
|
return productService.getProductById(productId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue