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

spring-cloud-alibaba 集成Feign

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

spring-cloud-alibaba 集成Feign

文章目录
  • 前言
  • 一、Feign是什么?
  • 二、使用步骤
    • 1.加依赖
    • 2.加注解
    • 3.加yml配置
    • 4.新建 要请求的微服务 的接口
    • 5. 调用微服务


前言

private final RestTemplate restTemplate;

UserDTO userDTO = restTemplate.getForObject(“http://user-center/users/{userId}”,UserDTO.class,userId);
通过这种RestTemplate 方式调用微服务,存在几个问题:
1.可读性差
2.复杂的url难以维护
3.被调微服务接口一旦有变化,修改复杂。

一、Feign是什么?

是Netflix的开源的声明式HTTP客户端

二、使用步骤 1.加依赖

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

2.加注解

代码如下(示例):

@MapperScan("com.itmuch")
@SpringBootApplication
@EnableFeignClients//加注解
public class ContentCenterApplication {
    public static void main(String[] args) {
        SpringApplication.run(ContentCenterApplication.class, args);
    }  
}
3.加yml配置

不用加

4.新建 要请求的微服务 的接口
import com.itmuch.contentcenter.domain.dto.user.UserDTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(name = "user-center")//要请求的微服务的名称
public interface UserCenterFeignClient {

    
    @GetMapping("/users/{id}")
    UserDTO findById(@PathVariable Integer id);
}
5. 调用微服务
private final UserCenterFeignClient userCenterFeignClient;

UserDTO userDTO = userCenterFeignClient.findById(userId);

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

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

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