天天看點

SpringCloud服務發現-Feign

建立springboot應用,添加依賴

  • spring web
  • eureka server
  • OpenFeign

配置application.yml

server:
  port: 8002

spring:
  application:
    name: api-order-add-feign

eureka:
  client:
    service-url:
      defaultZone: http://yzx35515:[email protected]:8761/eureka
           

在啟動類上添加注解

@SpringBootApplication
@EnableDiscoveryClient//聲明為服務消費者
@EnableFeignClients    //聲明啟⽤feign用戶端
public class ApiOrderAddFeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(ApiOrderAddFeignApplication.class, args);
    }

}
           

服務調⽤

使⽤Feign進⾏服務調⽤的時候,需要⼿動建立⼀個服務通路用戶端(接⼝)

建立Feign用戶端

@FeignClient(value = "order-add")
public interface OrderAddClient {
    @PostMapping("order/add")
    public ResultVO addOrder(@RequestBody Order order);

}
           

使⽤Feign用戶端調⽤服務

@Service
public class OrderAddServiceImpl implements OrderAddService {

    @Autowired
    private OrderAddClient orderAddClient;


    @Override
    public ResultVO saveOrder(Order order) {
        //1.調用order-add服務進行儲存
        ResultVO vo = orderAddClient.addOrder(order);
        System.out.println(vo);

        //2.調用orderitem-add儲存


        //3.調用stock-updata修改商品庫存


        //4.調用shopcart-del删除購物車記錄


        return vo;
    }
}