远程调用 Spring Cloud Feign

一、 Feign简介

Feign [feɪn] 译文 伪装。Feign是一个声明式WebService客户端.使用Feign能让编写WebService客户端更加简单,它的使用方法是定义一个接口,然后在上面添加注解。不再需要拼接URL,参数等操作。项目主页:https://github.com/OpenFeign/feign

  • 集成Ribbon的负载均衡功能
  • 集成了Hystrix的熔断器功能
  • 支持请求压缩
  • 大大简化了远程调用的代码,同时功能还增强啦
  • Feign以更加优雅的方式编写远程调用代码,并简化重复代码

快速入门

实现步骤:

  1. 导入feign依赖
  2. 编写Feign客户端接口
  3. 消费者启动引导类开启Feign功能注解
  4. 访问接口测试

1. 导入feign依赖

  <!--配置feign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

1.1创建实体类User

package com.william.domain;

import java.util.Date;

/**
 * @author :lijunxuan
 * @date :Created in 2019/6/30  18:20
 * @description :
 * @version: 1.0
 */
public class User {
    private Integer id;//主键id
    private String username;//用户名
    private String password;//密码
    private String name;//姓名
    private Integer age;//年龄
    private Integer sex;//性别 1男性,2女性
    private Date birthday; //出生日期
    private Date created; //创建时间
    private Date updated; //更新时间
    private String note;//备注

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                ", birthday=" + birthday +
                ", created=" + created +
                ", updated=" + updated +
                ", note='" + note + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public Date getUpdated() {
        return updated;
    }

    public void setUpdated(Date updated) {
        this.updated = updated;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }
}

2. 编写Feign客户端接口

package com.william.service;

import com.william.domain.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient("user-service")
public interface UserClient {
    /**
     *RequestMapping
     * spring mvc 里面的注解
     * RequestMapping 反向映射,生成请求地址。http请求
     *  @RequestParam("id") 是必须要进行请求地址参数的绑定
     * @param id
     * @return
     */
    @RequestMapping("/user/findById")
    User findById(@RequestParam("id") Integer id);
}

3. 消费者启动引导类DemoApplication开启Feign功能注解

在这里插入图片描述

@EnableFeignClients//开启feign客户端支持

3.1编写ConsumerFeignController,使用UserClient访问

package com.william.controller;

import com.william.domain.User;
import com.william.service.UserClient;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author :lijunxuan
 * @date :Created in 2019/6/30  18:33
 * @description :
 * @version: 1.0
 */
@RestController
public class FeigConsumerController {
    @Resource
     UserClient userClient;//注入FeignCLient
    @RequestMapping("/findByIdFeign")
    public User findByIdFeign(Integer id){
        return userClient.findById(id);
    }
}

4. 访问接口测试

在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值