✨ 远程调用-声明式实现
This commit is contained in:
parent
041b8c0218
commit
42d21d0947
|
@ -70,11 +70,11 @@
|
|||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- knife4j -->
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
|
@ -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);
|
||||
|
|
|
@ -5,7 +5,7 @@ spring:
|
|||
application:
|
||||
name: service-order
|
||||
profiles:
|
||||
active: prod
|
||||
active: dev
|
||||
cloud:
|
||||
nacos:
|
||||
server-addr: 192.168.95.135:8848
|
||||
|
|
Loading…
Reference in New Issue