openfeign的快速使用

一、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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值