目录
一、Feign是啥
二、应用
Feign是一个声明式、模板化的HTTP客户端,有了Feign,我们可以更加便捷、优雅地调用HTTP API。
在Spring Cloud中,使用Feign非常简单------创建一个接口,并在接口上添加一些注解即可。Feign支持自带注解和JAX-RS注解等。
在Spring Cloud中,对Feign进行了增强,使其支持了Spring MVC注解,并整合了Ribbon和Eureka,从而,让其使用更加方便。
1.创建项目
2.加入依赖
4.0.0
com.itmuch.cloud
microservice-consumer-movie-feign
0.0.1-SNAPSHOT
jar
org.springframework.boot
spring-boot-starter-parent
1.4.3.RELEASE
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.cloud
spring-cloud-starter-feign
org.springframework.cloud
spring-cloud-dependencies
Camden.SR4
pom
import
org.springframework.boot
spring-boot-maven-plugin
3.创建User类
package com.itmuch.cloud.study.user.entity;
import java.math.BigDecimal;
public class User {
private Long id;
private String username;
private String name;
private Integer age;
private BigDecimal balance;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return this.age;
}
public void setAge(Integer age) {
this.age = age;
}
public BigDecimal getBalance() {
return this.balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
4.创建Feign接口
package com.itmuch.cloud.study.user.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.itmuch.cloud.study.user.entity.User;
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public User findById(@PathVariable("id") Long id);
}
这里,@FeigntClient注解标识这是一个Feign客户端,name的值,代表要请求的微服务应用名称,一定要与微服务名称对应上,否则,请求会失败。另外,加入这个服务,还有一个目的,就是创建Ribbon负载均衡器。Feign本身,就集成了Ribbon功能,即负载均衡功能。
5. 修改controller代码,让其调用Feign接口
package com.itmuch.cloud.study.user.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.itmuch.cloud.study.user.entity.User;
import com.itmuch.cloud.study.user.feign.UserFeignClient;
@RestController
public class MovieController {
@Autowired
private UserFeignClient userFeignClient;
@GetMapping("/user/{id}")
public User findById(@PathVariable Long id) {
return this.userFeignClient.findById(id);
}
}
6.创建启动类,为其添加@EnableFeignClients注解
package com.itmuch.cloud.study;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class ConsumerMovieApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerMovieApplication.class, args);
}
}
7.创建应用程序配置文件
server:
port: 8010
spring:
application:
name: microservice-consumer-movie
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
8.测试
(1)启动发现服务平台
(2)启动2个或更多mircoservice-provider-user实例(能够体现负载均衡)
(3)启动microservice-consumer-movie-feign
(4)多次访问http://localhost:8010/user/1
补充:声明式与命令式区别
声明式,就是你告诉手下人,你要完成这个任务,至于如何完成,就是他操心的事情了,你就不用管了
命令式,就是你不仅要告诉手下人,要完成任务,还要告诉他,如何如何去完成,你都要管。
在Spring Cloud中,Feign的默认配置类是FeignClientsConfiguration,该类定义了Feign默认使用的编辑器、解码器、所使用的契约等。
Spring Cloud允许通过注解@FeignClient的configuration属性,来自定义Feign配置,从而覆盖默认使用的FeignClientsConfiguration类。