分布式下feign的服务

首先看的是项目结构:
在这里插入图片描述
如图feign-serve模块就是我抽取的Feign
在这里插入图片描述
Spring.factories文件是SpringBoot在启动时进行自动装配:

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.itheima.feign.config.FeignAutoConfiguration

FeignAutoConfiguration类的内容:

@Configuration
@EnableFeignClients(basePackages = "com.xxx.feign")
public class FeignAutoConfiguration {
}

feign接口定义举例:
TeachingApiAgent接口内容:

import com.xuecheng.api.company.CompanyDTO;

import com.xuecheng.common.constant.XcFeignServiceNameList;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 *  Feign远程调用 教学机构管理
 */
@FeignClient(value = XcFeignServiceNameList.XC_TEACHING_SERVICE)
public interface TeachingApiAgent {

    String SERVICE_CONTEXT_PRE = "/teaching";

    /**
     * 根据租户Id获取机构信息(机构时一种租户)
     * @param tenantId
     * @return
     */
    @GetMapping(SERVICE_CONTEXT_PRE + "/l/company/{tenantId}")
    CompanyDTO getCompInfoDetail(@PathVariable(value = "tenantId") Long tenantId);

}

sentinel熔断降级选择FallbackFactory模式
TeachingApiAgentFallbackFactory类内容

package com.itheima.feign.agent;

import com.itheima.feign.agent.TeachingApiAgent;
import com.xuecheng.api.company.CompanyDTO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.domain.response.RestResponse;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class TeachingApiAgentFallbackFactory implements FallbackFactory<TeachingApiAgent> {

    @Override
    public TeachingApiAgent create(Throwable throwable) {
        return new TeachingApiAgent() {
            @Override
            public CompanyDTO getCompInfoDetail(Long tenantId) {
                log.error("Feign 接口调用熔断降级,错误信息:{}", throwable.getMessage());
                return null;
            }
        };
    }
}

做完上述工程,就在远程调用方导入该模块的依赖

 <dependency>
            <groupId>com.xuecheng</groupId>
            <artifactId>feign-server</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

并且再启动类上:
在这里插入图片描述
进行扫描feign接口所在的包
如此就可以远程调用了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值