openfeign可以充当负载均衡,一般使用在客户端。
使用openfeign
- 导pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
- 在主启动类上加上@EnableFeignClients开启openfeign
@SpringBootApplication
@EnableFeignClients
public class OrderFeignMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderFeignMain80.class,args);
}
}
- 写一个调用接口
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")//value:填的是注册中心的别名
@RequestMapping("/payment")
public interface PaymentFeignService {
@GetMapping("/get/{id}")
CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
@GetMapping("/timeOut")
CommonResult timeOut() throws InterruptedException;
}
注:这里面的路径,方法,参数,请求方式都要与你将要调用的那个controller一样例如:
@RestController
@Slf4j
@RequestMapping("/payment")
public class PaymentController {
@Resource
private PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
@Resource
private DiscoveryClient discoveryClient;
@GetMapping(value = "/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
log.info("8001");
Payment result = paymentService.getPaymentById(id);
log.info("*****查询结果:"+result);
if (result!=null){
return new CommonResult(200,"查询成功,serverPort:"+serverPort,result);
}
return new CommonResult(404,"查询失败,serverPort:"+serverPort);
}
@GetMapping("/timeOut")
public CommonResult timeOut() throws InterruptedException {
log.info("8001");
Thread.sleep(3000);
return new CommonResult(200,"用时3秒");
}
}
这样就算完成了openfeign的基本使用。
openfeign的超时
- 它的默认超时时间是1秒
- 通过配置yml
#设置feign客户端超时时间(OpenFeign 默认支持ribbon)
ribbon:
#指的是建立连接所用时间,适用于网络状况正常情况下,两端连接所用时间
ReadTimeout: 5000
#指的是连接建立后,从服务器获取到可用资源所用时间
ConnectTimeout: 5000
openfeign的日志
- 写配置类
@Configuration
public class FeignConfig {
@Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
- yml
#feign 日志以什么级别监控那个接口
logging:
level:
com.wang.service.PaymentFeignService: debug
多个参数传递问题
一般我们会使用@GetMapping和@PostMapping两种方式来调用Rest服务。
而接收的参数则会使用@RequestParam和@RequestBody来获取。
首先我们讲一下@RequestBody,@RequestBody只能用在Post请求,并且一个Post请求只能有一个@RequestBody。@RequestBody的参数可以包括复杂类型。
然后我们讲一下@RequestParam,@RequestParam可以用在Post和Get请求中,但是要注意:@RequestParam 的参数只能是基本类型或者Enum,或者List和Map(List和Map里面也只能是基本类型)。所以@RequestParam可以和@RequestBody一起使用。
如果我们是Get请求,但是又有复合类型怎么办?比如我们想传递一个User对象。User对象里面只有普通的两个String属性。这里我们可以使用@SpringQueryMap:
@GetMapping("getUserAge")
public String getUserAge(@RequestParam("userId") String userId, @SpringQueryMap User user);
注意:@SpringQueryMap后面的参数只能是普通的POJO,不能是复合类型,否则解析不了。如果必须使用复合类型,那么使用@RequestBody吧。