Search in sources :

Example 1 with InterfaceLimit

use of com.lovnx.entity.InterfaceLimit in project micro-service by Lovnx.

the class ComputeController method add.

// static{
// Timer timer = new Timer();
// timer.schedule(new TimerTask(){
// @Override
// public void run() {
// num.set(0);;
// }
// }, 0, timeRound);
// }
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String add(@RequestParam Integer a, @RequestParam Integer b) {
    // num.incrementAndGet();
    // 
    // if (num.get() <= flag) {
    // ServiceInstance instance = client.getLocalServiceInstance();
    // Integer r = a + b;
    // logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
    // return "From Service-B, Result is " + r+"\nPort:"+instance.getPort();
    // }
    // return "调用次数超限,一分钟内最多只能调用10次!";
    InterfaceLimit limit = service.getEntityByPri(1);
    Jedis jedis = RedisUtils.getJedis();
    // redis存的超时时间
    String timeRound_1 = jedis.get("timeRound_1");
    // 如果不存在或者是不等于数据库设置值
    if (timeRound_1 == null || !limit.getUnitTime().toString().equals(timeRound_1)) {
        // 重新设置超时时间
        jedis.set("timeRound_1", limit.getUnitTime().toString());
        jedis.expire("num_1", limit.getUnitTime());
    }
    String num_1 = jedis.get("num_1");
    if (num_1 == null) {
        jedis.set("num_1", String.valueOf(0));
        jedis.expire("num_1", limit.getUnitTime());
    }
    jedis.incr("num_1");
    if (Integer.parseInt(jedis.get("num_1")) <= limit.getUnitNum()) {
        ServiceInstance instance = client.getLocalServiceInstance();
        Integer r = a + b;
        logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
        return "From Service-B, Result is " + r + "\nPort:" + instance.getPort();
    }
    return "调用次数超限!";
}
Also used : InterfaceLimit(com.lovnx.entity.InterfaceLimit) Jedis(redis.clients.jedis.Jedis) ServiceInstance(org.springframework.cloud.client.ServiceInstance) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

InterfaceLimit (com.lovnx.entity.InterfaceLimit)1 ServiceInstance (org.springframework.cloud.client.ServiceInstance)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 Jedis (redis.clients.jedis.Jedis)1