Feign 的英文表意为“假装,伪装,变形”, 是一个http请求调用的轻量级框架,可以以Java接口注解的方式调用Http请求,而不用像Java中通过封装HTTP请求报文的方式直接调用。Feign通过处理注解,将请求模板化,当实际调用的时候,传入参数,根据参数再应用到请求上,进而转化成真正的请求,这种请求相对而言比较直观。
Feign被广泛应用在Spring Cloud 的解决方案中,是学习基于Spring Cloud 微服务架构不可或缺的重要组件。
封装了Http调用流程,更适合面向接口化的编程习惯
在服务调用的场景中,我们经常调用基于Http协议的服务,而我们经常使用到的框架可能有HttpURLConnection、Apache HttpComponnets、OkHttp3 、Netty等等,这些框架在基于自身的专注点提供了自身特性。而从角色划分上来看,他们的职能是一致的提供Http调用服务。具体流程如下:
<!-- Feign Client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
package com.mazong.serverbloguser;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ServerBlogUserApplication {
public static void main(String[] args) {
SpringApplication.run(ServerBlogUserApplication.class, args);
}
}
package com.mazong.serverbloguser.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "server-blog-article")
@Component(value = "iUserService")
public interface IUserService {
@RequestMapping(value = "/hello")
public String hello();
}
@Controller
@RequestMapping("/")
public class UserController {
@Autowired
private IDbUserMapper iUserDbMapper;
@Autowired
HttpServletRequest request;
@Value("${spring.application.name}")
private String serverName;
@Value("${server.port}")
private int serverPort;
@Autowired
private IUserService iUserService;
@RequestMapping(value = "/getUser")
@ResponseBody
public Object getUser(@RequestParam("id") Integer id) {
// http://localhost:8000/getUser?id=2
TbUser user = iUserDbMapper.getUserById(id);
user.setServerInfo(serverName+":"+serverPort);
System.out.println(user);
String blog = iUserService.hello();
System.out.println(blog);
return user;
}
// ... 省略
}
## application.properties
spring.application.name=server-blog-user
server.port=8000
## MyBatis 数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/db_user?autoReconnect=true&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
## mybatis
# 对应实体类的包名
mybatis.typeAliasesPackage=com.mazong.serverbloguser.mapper
# mapper.xml文件所在位置
# mybatis.mapperLocations=classpath:**/mapper/*.xml
logging.level.com.mazong.serverbloguser.mapper=debug
# eureka client
eureka.client.service-url.defaultZone=http://localhost:6060/eureka/
eureka.instance.prefer-ip-address=true
## 自定义配置负载均衡策略
server-blog-article.ribbon.NFLoadBalancerRuleClassName= com.netflix.loadbalancer.RoundRobinRule
参考文档: