基于Feign实现服务调用

目录

 

1、什么是Feign

2、Feign的使用

2.1、加入Feign的依赖

2.2、在主类上添加Feign的注解

2.3、创建一个service,并使用Feign实现微服务调用

2.4、修改controller调用服务的代码,启动应用进行验证


1、什么是Feign

Feign是Spring Cloud提供的一个声明式的伪http客户端,它使得调用远程服务就像调用本地服务一样简单,只需要创建一个接口,并添加一个注解即可。

Nacos很好的兼容了Feign,Feign默认集成了Ribbon。所以在Nacas下使用Feign默认就实现了负载均衡的效果。

2、Feign的使用

2.1、加入Feign的依赖

        <!--Feign组件-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

2.2、在主类上添加Feign的注解

package cn.jack;

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;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients     // 开启Feign
public class OrderApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

2.3、创建一个service,并使用Feign实现微服务调用

package cn.jack.service;

import cn.jack.domain.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(value = "service-product")     // value是nacos下的服务名
public interface ProductService {

    @RequestMapping(value = "/product/{pid}")       // @FeignClient + @RequestMapping 就是一个完整的请求路径: http://service-product/product/{pid}
    Product findByPid(@PathVariable("pid") Integer pid);
}

2.4、修改controller调用服务的代码,启动应用进行验证

package cn.jack.controller;

import cn.jack.domain.Order;
import cn.jack.domain.Product;
import cn.jack.service.OrderService;
import cn.jack.service.ProductService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Random;

@RestController
@Slf4j
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private OrderService orderService;
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private ProductService productService;

    /**
     * 下单 -- 基于Feign实现服务调用
     * @param pid   商品id
     * @return
     */
    @RequestMapping("/order/prod/{pid}")
    @Deprecated
    public Order order(@PathVariable("pid") Long pid) {
        log.info("收到下单请求,准备查询商品信息。pid={}", pid);

        // 通过Feign调用商品微服务,查询商品信息
        Product product = this.productService.findByPid(pid);
        log.info("商品信息查询成功。内容为:{}", JSON.toJSONString(product));

        // 生成商品信息保存
        Order order = new Order();
        order.setNumber(1);
        order.setPid(pid);
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setUid(1);
        order.setUsername("测试用户");

        this.orderService.createOrder(order);
        log.info("订单信息保存成功。内容为:{}", JSON.toJSONString(order));

        return order;
    }
}

至此,基于Feign实现了微服务调用。关于负载均衡策略的修改,参考https://blog.csdn.net/qq_31155349/article/details/108458378

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值