关于Feign的使用以及JS中setInterval的使用

Feign

一、Feign概述

Feign是Spring Cloud提供的声明式、模板化的HTTP客户端, 它使得调用远程服务就像调用本地服务一样简单,只需要创建一个接口并添加一个注解即可。

Feign 是一种声明式、模板化的 HTTP 客户端。在 Spring Cloud 中使用 Feign,可以做到使用 HTTP 请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问 HTTP 请求。

Spring Cloud集成Feign并对其进行了增强,使Feign支持了Spring MVC注解;Feign默认集成了Ribbon,所以Fegin默认就实现了负载均衡的效果。

openFeign:调用远程就像调用本地一样。

二、Feign使用

导入依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

创建服务提供者

@RestController
@RequestMapping("feign")
public class FeignController {
    @PostMapping("feignInfo")
    public String feignInfo(@RequestBody String info){
        return info + ":So will you";
    }
}

创建feign接口

@Service
@FeignClient(name = "FeignSys",url = "http://127.0.0.1:8080/feign/")
public interface IFeignService {
    @PostMapping("feignInfo")
    public String feignInfo(@RequestBody String info);
}

创建服务消费者

@RestController
public class FeignController {
    @Autowired
    IFeignService iFeignService;
    @GetMapping("remoteInfo")
    public String remoteInfo(){
        return iFeignService.feignInfo("post请求");
    }

}

JS定时调度setInterval

setInterval()

* - 定时调用

* - 可以将一个函数,每隔一段时间执行一次

* - 参数:

* 1.回调函数,该函数会每隔一段时间被调用一次

* 2.每次调用间隔的时间,单位是毫秒

*

* - 返回值:

* 返回一个Number类型的数据

* 这个数字用来作为定时器的唯一标识

this.atime = setInterval(function () {

that.getOrderType();

}, 1000);

关闭定时器

clearInterval()可以用来关闭一个定时器

方法中需要一个定时器的标识作为参数,这样将关闭标识对应的定时器

clearInterval()可以接收任意参数,

如果参数是一个有效的定时器的标识,则停止对应的定时器

如果参数不是一个有效的标识,则什么也不做

clearInterval(this.atime);

注意:

在使用setInterval的时候,回调函数需要使用that来代替this,使用this会报错,找不到该函数。

且将定时器赋值给变量(此时变量已经赋值了定时器),不会清除变量中之前赋值的定时器。

经验分享 程序员 微信小程序 职场和发展