1 什么是OpenFeign
Feign是Spring Cloud提供的一个声明式的伪Http客户端, 它使得调用远程服务就像调用本地服务一样简单, 只需要创建一个接口并添加一个注解即可。
Nacos很好的兼容了Feign, Feign负载均衡默认集成了 Ribbon, 所以在Nacos下使用Fegin默认 f就实现了负载均衡的效果。
2 feign的使用
1.加入feign依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
2 在主启动类上加入开启feign的注解
package com.cyy.order;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @program: springcloud-parent
* @description: 主启动类
* @author: 崔艺耀
* @create: 2021-07-06 20:25
**/
@SpringBootApplication
@EnableDiscoveryClient//开启nacos注解
@EnableFeignClients//开启feign注解
@MapperScan(basePackages = "com.cyy.order.mapper")
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(com.cyy.order.OrderApplication.class,args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
3 创建feign的接口
package com.cyy.order.feign; import com.cyy.coment.entry.Product; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @FeignClient(value = "product") public interface ProductFeign { @GetMapping("/product/FindById/{pid}") public Product selectById(@PathVariable Integer pid); }
4 修改OrderController的代码
package com.cyy.order.controller;
import com.cyy.coment.entry.Order;
import com.cyy.coment.entry.Product;
import com.cyy.order.feign.ProductFeign;
import com.cyy.order.sevice.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
/**
* @program: springcloud-parent
* @description: 订单的控制层
* @author: 崔艺耀
* @create: 2021-07-06 19:50
**/
@RestController
@RequestMapping("order")
public class OrderController {
@Autowired
private OrderService orderService;
@Autowired
private ProductFeign productFeign;
@GetMapping("saveOrder")
public String saveOrder(@RequestParam Integer pid,Integer num){
Order order=new Order();
order.setNumber(num);
order.setUid(2);
order.setUsername("张三");
order.setPid(pid);
Product product = productFeign.selectById(pid);
order.setPname(product.getPname());
order.setPprice(product.getPprice());
orderService.saveOrder(order);
return "下单成功";
}
}
5 重启order微服务,查看效果