栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

微服务之Feign远程调用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

微服务之Feign远程调用

微服务之Feign远程调用 1.微服务之间一般都是业务分离,会创建一个独立的模块来存放接口,方便以后调用,以及防止代码的冗余和降低耦合性

2.导入feign的依赖

            org.springframework.cloud
            spring-cloud-starter-openfeign
            2.2.7.RELEASE

3.构建feign模块,并创建接口
@FeignClient(value = "leadnews-wemedia")//服务调用者
public interface IWemediaClient {

    
    @GetMapping("/api/v1/channel/list")
    public ResponseResult channel();
    
}
4.在leadnews-wemedia模块feign包下,创建一个实现类来实现远程接口,然后再对业务进行一个实现

import com.heima.apis.wemedia.IWemediaClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.wemedia.service.WmChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WemediaClient implements IWemediaClient {

    @Autowired
    private WmChannelService wmChannelService;
    
    @GetMapping("/api/v1/channel/list")
    @Override
    public ResponseResult channel() {
        return wmChannelService.findAllChannel();
    }
}

在leadnews-wemedia模块下也导入feign客户端依赖


            org.springframework.cloud
            spring-cloud-starter-openfeign
            2.2.7.RELEASE

并在启动类上添加注解@EnableFeignClients(basePackages = “com.wcl.apis”),basePackages对应的是feign模块下接口的路径

5.测试,通过spring容器注入接口,进行一个调用
    @Autowired
    private IWemediaClient iWemediaClient;
    
    @Test
    public void test(){
        ResponseResult channel = iWemediaClient.chanwnel();
    }
    

注:不同的模块业务下都可以进行一个远程调用,但一定要导入feign客户端的依赖和启动类添加注解
@EnableFeignClients(basePackages = “com.wcl.apis”)


            org.springframework.cloud
            spring-cloud-starter-openfeign
            2.2.7.RELEASE

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1019927.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号