3.【Spring Cloud Alibaba】声明式HTTP客户端-Feign
drf认证组件、权限组件、jwt认证、签发、jwt框架使用
运用Feign完成长途HTTP挪用
什么是Feign
- Feign是Netflix开源的声明式HTTP客户端
完成
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
@MapperScan("com.itmuch.contentcenter.dao")
@SpringBootApplication
@EnableFeignClients// (defaultConfiguration = GlobalFeignConfiguration.class)
@EnableBinding({Source.class})
public class ContentCenterApplication {
@FeignClient(name = "user-center")
public interface UserCenterFeignClient {
/**
* http://user-center/users/{id}
*
* @param id
* @return
*/
@GetMapping("/users/{id}")
UserDTO findById(@PathVariable Integer id);
}
private final UserCenterFeignClient userCenterFeignClient;
// 1. 代码不可读
// 2. 庞杂的url难以保护:https://user-center/s?ie={ie}&f={f}&rsv_bp=1&rsv_idx=1&tn=baidu&wd=a&rsv_pq=c86459bd002cfbaa&rsv_t=edb19hb%2BvO%2BTySu8dtmbl%2F9dCK%2FIgdyUX%2BxuFYuE0G08aHH5FkeP3n3BXxw&rqlang=cn&rsv_enter=1&rsv_sug3=1&rsv_sug2=0&inputT=611&rsv_sug4=611
// 3. 难以响应需求的变化,变化很没有幸福感
// 4. 编程体验不一致
UserDTO userDTO = this.userCenterFeignClient.findById(userId);
Feign的构成
细粒度设置自定义
- Java代码体式格局
- 设置属性要领
指定日记级别
Java代码体式格局
UserCenterFeignClient
@FeignClient(name = "user-center", configuration = GlobalFeignConfiguration.class)
public interface UserCenterFeignClient {
/**
* http://user-center/users/{id}
*
* @param id
* @return
*/
@GetMapping("/users/{id}")
UserDTO findById(@PathVariable Integer id);
}
GlobalFeignConfiguration
/**
* feign的设置类
* 这个类别加@Configuration注解了,不然必需挪到@ComponentScan能扫描的包之外
*/
public class GlobalFeignConfiguration {
@Bean
public Logger.Level level(){
// 让feign打印一切要求的细节
return Logger.Level.FULL;
}
}
application.yml
logging:
level:
com.itmuch.contentcenter.feignclient.UserCenterFeignClient: debug
设置属性要领
全局设置
- Java代码体式格局
- 设置属性体式格局
Java代码体式格局
ContentCenterApplication
EnableFeignClients
// 扫描mybatis哪些包内里的接口
@MapperScan("com.itmuch.contentcenter.dao")
@SpringBootApplication
@EnableFeignClients(defaultConfiguration = GlobalFeignConfiguration.class)
@EnableBinding({Source.class})
public class ContentCenterApplication {
/**
* feign的设置类
* 这个类别加@Configuration注解了,不然必需挪到@ComponentScan能扫描的包之外
*/
public class GlobalFeignConfiguration {
@Bean
public Logger.Level level(){
// 让feign打印一切要求的细节
return Logger.Level.FULL;
}
}
设置属性体式格局
// 扫描mybatis哪些包内里的接口
@MapperScan("com.itmuch.contentcenter.dao")
@SpringBootApplication
@EnableFeignClients// (defaultConfiguration = GlobalFeignConfiguration.class)
@EnableBinding({Source.class})
public class ContentCenterApplication {
支撑的设置项
Java代码体式格局支撑的设置项
设置属性体式格局支撑的设置项
设置最好实践
Ribbon设置 VS Feign设置
Feign代码体式格局 vs 属性体式格局
最好实践
Feign的继续
关于继续的争议
- 官方看法:官方不引荐运用
- 业界看法:许多公司运用
- 个人看法:权衡利弊
多参数要求组织
Get要求
TestController
@GetMapping("test-get")
public UserDTO query(UserDTO userDTO) {
return testUserCenterFeignClient.query(userDTO);
}
要领1
TestUserCenterFeignClient
import com.itmuch.contentcenter.domain.dto.user.UserDTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name = "user-center")
public interface TestUserCenterFeignClient {
@GetMapping("/q")
UserDTO query(@SpringQueryMap UserDTO userDTO);
}
要领二
@FeignClient(name = "user-center")
public interface UserFeignClient {
@RequestMapping(value = "/q", method = RequestMethod.GET)
public UserDTO query(@RequestParam("id") Long id, @RequestParam("username") String username);
}
POST要求包括多个参数
下面来议论怎样运用Feign组织包括多个参数的POST要求。假定效劳提供者的Controller是如许编写的:
@RestController
public class UserController {
@PostMapping("/post")
public User post(@RequestBody User user) {
...
}
}
我们要怎样运用Feign去要求呢?答案异常简朴,示例:
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
@RequestMapping(value = "/post", method = RequestMethod.POST)
public User post(@RequestBody User user);
}
Feign离开Ribbon的运用
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
// 离开ribbon的运用
@FeignClient(name = "baidu", url = "http://www.baidu.com")
public interface TestBaiduFeignClient {
@GetMapping("")
String index();
}
RestTemplate vs Feign
Feign机能优化
- 连接池[提拔15%摆布]
feign:
sentinel:
# 为feign整合sentinel
enabled: true
client:
config:
# 全局设置
default:
loggerLevel: full
requestInterceptors:
- com.itmuch.contentcenter.feignclient.interceptor.TokenRelayRequestIntecepor
httpclient:
# 让feign运用apache httpclient做要求;而不是默许的urlconnection
enabled: true
# feign的最大连接数
max-connections: 200
# feign单个途径的最大连接数
max-connections-per-route: 50
现在,在Spring cloud中效劳之间经由过程restful体式格局挪用有两种体式格局
- restTemplate+Ribbon
- feign
从实践上看,采纳feign的体式格局更文雅(feign内部也运用了ribbon做负载平衡)。
分析思维 第四篇:数据分析入门阶段——描述性统计分析和相关分析