一、OpenFeign是什么?
可以用来替换ribbon+restTemplate(以对象调用方法的方式去调用服务)
OpenFeign是Spring Cloud在Feign的基础上支持了SpringMVC的注解,如@RequesMapping等等。OpenFeign的@Feignclient可以解析SpringMVc的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。
二、OpenFeign的快速使用
1.在消费者端引入依赖
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2.主启动添加@EnableFeignClients
package per;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import ribbon.RandonRuleConfig;
@SpringBootApplication
@RibbonClients(value = {@RibbonClient(name = "stock-service",configuration = RandonRuleConfig.class)}) //配置负载均衡策略
@EnableFeignClients //使用openfeign后无需RestTemplate
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
// @Bean
// @LoadBalanced // 负载均衡器注解,nacos的服务调用依赖于负载均衡(nacos无法将服务名称转化为服务地址,需要使用负载均衡器,默认使用轮询的方式)
// public RestTemplate restTemplate(RestTemplateBuilder builder){
// RestTemplate RestTemplate = builder.build();
// return RestTemplate;
// }
}
3.创建业务类(逻辑层)
package per.pz.openfeign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* name指定服务提供者端的服务名
* path指定服务提供者端所在的Controller层指定的RequestMapping
*/
@FeignClient(name = "stock-service",path = "/stock")
public interface StockFeignService {
//声明需要调用的rest接口对应的方法
@RequestMapping("/reduct")
String reduct();
}
/** 服务提供者端controller的代码
@RestController
@RequestMapping("stock")
public class stockController {
//读取配置文件中的端口
@Value("${server.port}")
String port;
@RequestMapping("/reduct")
public String reduct(){
System.out.println("扣减库存");
return "扣减库存111"+port;
}
}
*/
4.服务消费者调用
package per.pz.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import per.pz.openfeign.StockFeignService;
@RestController
@RequestMapping("order")
public class oderController {
// @Autowired
// RestTemplate RestTemplate;
@Autowired
StockFeignService StockFeignService;
@RequestMapping("/add")
public String add(){
System.out.println("下单成功");
// 服务间的通讯,没有使用微服务的方式
// 方法的缺点是在代码中写死服务地址,所以使用注册中心解决
// String msg = RestTemplate.getForObject("http://localhost:8021/stock/reduct",String.class);
// nacos服务调用,无需使用地址,使用服务名称即可
// String msg = RestTemplate.getForObject("http://stock-service/stock/reduct",String.class);
//openfeign代替RestTemplate
String msg = StockFeignService.reduct();
return "下单成功111"+msg;
}
}