一、 Feign简介
Feign [feɪn] 译文 伪装。Feign是一个声明式WebService客户端.使用Feign能让编写WebService客户端更加简单,它的使用方法是定义一个接口,然后在上面添加注解。不再需要拼接URL,参数等操作。项目主页:https://github.com/OpenFeign/feign 。
- 集成Ribbon的负载均衡功能
- 集成了Hystrix的熔断器功能
- 支持请求压缩
- 大大简化了远程调用的代码,同时功能还增强啦
- Feign以更加优雅的方式编写远程调用代码,并简化重复代码
快速入门
实现步骤:
- 导入feign依赖
- 编写Feign客户端接口
- 消费者启动引导类开启Feign功能注解
- 访问接口测试
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;//主键idprivate 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;//备注@Overridepublic 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 {@ResourceUserClient userClient;//注入FeignCLient@RequestMapping("/findByIdFeign")public User findByIdFeign(Integer id){return userClient.findById(id);}
}
4. 访问接口测试