From 42d21d09477ae251b86db3bdecaca53f7db906b5 Mon Sep 17 00:00:00 2001 From: bunny <1319900154@qq.com> Date: Mon, 26 May 2025 17:44:53 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E8=BF=9C=E7=A8=8B=E8=B0=83?= =?UTF-8?q?=E7=94=A8-=E5=A3=B0=E6=98=8E=E5=BC=8F=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cloud-demo/services/pom.xml | 4 ++-- .../bunny/service/OrderServiceApplication.java | 2 ++ .../bunny/service/feign/ProductFeignClient.java | 17 +++++++++++++++++ .../service/service/impl/OrderServiceImpl.java | 5 ++++- .../src/main/resources/application.yaml | 2 +- 5 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 cloud-demo/services/service-order/src/main/java/cn/bunny/service/feign/ProductFeignClient.java diff --git a/cloud-demo/services/pom.xml b/cloud-demo/services/pom.xml index 5ebf59a..9940483 100644 --- a/cloud-demo/services/pom.xml +++ b/cloud-demo/services/pom.xml @@ -70,11 +70,11 @@ org.springframework.cloud - spring-cloud-starter-openfeign + spring-cloud-starter-loadbalancer org.springframework.cloud - spring-cloud-starter-loadbalancer + spring-cloud-starter-openfeign diff --git a/cloud-demo/services/service-order/src/main/java/cn/bunny/service/OrderServiceApplication.java b/cloud-demo/services/service-order/src/main/java/cn/bunny/service/OrderServiceApplication.java index 1b74aba..ab1c6a1 100644 --- a/cloud-demo/services/service-order/src/main/java/cn/bunny/service/OrderServiceApplication.java +++ b/cloud-demo/services/service-order/src/main/java/cn/bunny/service/OrderServiceApplication.java @@ -8,6 +8,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Bean; import java.util.concurrent.Executor; @@ -16,6 +17,7 @@ import java.util.concurrent.Executors; @SpringBootApplication @EnableDiscoveryClient @RefreshScope +@EnableFeignClients public class OrderServiceApplication { public static void main(String[] args) { SpringApplication.run(OrderServiceApplication.class, args); diff --git a/cloud-demo/services/service-order/src/main/java/cn/bunny/service/feign/ProductFeignClient.java b/cloud-demo/services/service-order/src/main/java/cn/bunny/service/feign/ProductFeignClient.java new file mode 100644 index 0000000..c3c2e68 --- /dev/null +++ b/cloud-demo/services/service-order/src/main/java/cn/bunny/service/feign/ProductFeignClient.java @@ -0,0 +1,17 @@ +package cn.bunny.service.feign; + +import cn.bunny.model.product.bean.Product; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; + +// Feign 客户端 +@FeignClient(value = "service-product", path = "/api/product") +public interface ProductFeignClient { + + // 标注在 Controller 上是接受请求 + // 标注在 FeignClient 时发送请求 + @GetMapping("{id}") + Product getProduct(@PathVariable("id") Long productId); + +} diff --git a/cloud-demo/services/service-order/src/main/java/cn/bunny/service/service/impl/OrderServiceImpl.java b/cloud-demo/services/service-order/src/main/java/cn/bunny/service/service/impl/OrderServiceImpl.java index 2fef61d..23feb36 100644 --- a/cloud-demo/services/service-order/src/main/java/cn/bunny/service/service/impl/OrderServiceImpl.java +++ b/cloud-demo/services/service-order/src/main/java/cn/bunny/service/service/impl/OrderServiceImpl.java @@ -2,6 +2,7 @@ package cn.bunny.service.service.impl; import cn.bunny.model.order.bean.Order; import cn.bunny.model.product.bean.Product; +import cn.bunny.service.feign.ProductFeignClient; import cn.bunny.service.service.OrderService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -22,6 +23,7 @@ public class OrderServiceImpl implements OrderService { private final DiscoveryClient discoveryClient; private final RestTemplate restTemplate; private final LoadBalancerClient loadBalancerClient; + private final ProductFeignClient productFeignClient; /** * 创建订单信息 @@ -32,7 +34,8 @@ public class OrderServiceImpl implements OrderService { */ @Override public Order createOrder(Long productId, Long userId) { - Product product = getProductFromRemoteWithLoadBalancerAnnotation(productId); + // Product product = getProductFromRemoteWithLoadBalancerAnnotation(productId); + Product product = productFeignClient.getProduct(productId); Order order = new Order(); order.setId(1L); diff --git a/cloud-demo/services/service-order/src/main/resources/application.yaml b/cloud-demo/services/service-order/src/main/resources/application.yaml index c4a3033..617c10a 100644 --- a/cloud-demo/services/service-order/src/main/resources/application.yaml +++ b/cloud-demo/services/service-order/src/main/resources/application.yaml @@ -5,7 +5,7 @@ spring: application: name: service-order profiles: - active: prod + active: dev cloud: nacos: server-addr: 192.168.95.135:8848