SpringCloud Fegin服务调用失败

1、注意在启动类上标注 @EnableFeignClients//服务调用 我这里忘记标了此注解,导致服务调用失败

 2、服务调用流程

一、服务提供方

package com.zeal.sms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zeal.commonutils.R;
import com.zeal.sms.entity.Push;
import com.zeal.sms.service.PushService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mangoubiubiu
 * @since 2022-07-03
 */
@Slf4j
@RestController
@RequestMapping("/inner/sms")
@RequiredArgsConstructor
public class CenterController {

    final PushService pushService;
    /**
     * 返回消息未读数量
     * @return
     */
    @GetMapping("getUnreadCount")
    public R getUnreadCount(@RequestParam String userId){
        List<Push> pushList = pushService.list(new QueryWrapper<Push>().eq("user_id", userId));
        long count = pushList.stream().filter(res -> false==res.getIsRead()).count();
        return R.ok().data("count",count);
    }
}

二、服务消费方

 接口

package com.zeal.acl.client;

import com.zeal.acl.client.impl.SmsFileDegradeFeignClient;
import com.zeal.commonutils.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name = "zeal-sms", fallback = SmsFileDegradeFeignClient.class)
@Component
public interface SmsClient {

    @GetMapping("/inner/sms/getUnreadCount")
    public R getUnreadCount(@RequestParam("userId") String userId);

}

实现类(里面的返回是接口调用失败的情况下才返回)

package com.zeal.acl.client.impl;

import com.zeal.commonutils.R;
import com.zeal.acl.client.SmsClient;
import org.springframework.stereotype.Component;

@Component
public class SmsFileDegradeFeignClient implements SmsClient {
    @Override
    public R getUnreadCount(String userId) {
        return R.error().message("系统繁忙,请稍后再试");
    }
}

调用成功

 

posted @ 2022-07-03 18:15  KwFruit  阅读(643)  评论(0编辑  收藏  举报