您现在的位置是:首页 > 编程 > 

【Spring Cloud】006

2025-07-26 20:37:52
【Spring Cloud】006 一、Feign概述1、简介Feign是声明式Web Service客户端,它让微服务之间的调用变得更简单,类似controller调用service;SpringCloud集成了Ribbon和Eureka,可以使用Feigin提供负载均衡的http客户端;只需要创建一个接口,然后添加注解即可~Feign,主要是社区版,大家都习惯面向接口编程。这个是很多开发人员的

【Spring Cloud】006

一、Feign概述

1、简介

Feign是声明式Web Service客户端,它让微服务之间的调用变得更简单,类似controller调用service;

SpringCloud集成了Ribbon和Eureka,可以使用Feigin提供负载均衡的http客户端;

只需要创建一个接口,然后添加注解即可~

Feign,主要是社区版,大家都习惯面向接口编程。这个是很多开发人员的规范;

2、调用微服务访问两种方法

微服务名字 【ribbon】;

接口和注解 【feign】;

、Feign能干什么

Feign旨在使编写Java Http客户端变得更容易; 前面在使用Ribbon + RestTemplate时,利用RestTemplate对Http请求的封装处理,形成了一套模板化的调用方法。但是在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一个客户端类来包装这些依赖服务的调用。所以,Feign在此基础上做了进一步的封装,由他来帮助我们定义和实现依赖服务接口的定义,在Feign的实现下,我们只需要创建一个接口并使用注解的方式来配置它 (类似以前Dao接口上标注Mapper注解,现在是一个微服务接口上面标注一个Feign注解),即可完成对服务提供方的接口绑定,简化了使用Spring Cloud Ribbon 时,自动封装服务调用客户端的开发量;

4、Feign默认集成了Ribbon

利用Ribbon维护了MicroServiceCloud-Dept的服务列表信息,并且通过轮询实现了客户端的负载均衡,而与Ribbon不同的是,通过Feign只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现了服务调用;

二、代码演示

1、创建一个新的模块springcloud-cumer-dept-feign

2、在springcloud-api模块写服务

导入feign坐标:

(在springcloud-api和springcloud-cumer-dept-feign中分别导入)

代码语言:javascript代码运行次数:0运行复制
        <!--feign-->
        <dependency>
            <groupId>org.</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
DeptClientService接口:
代码语言:javascript代码运行次数:0运行复制
package com.zibo.springcloud.service;

import com.zibo.springcloud.pojo.Dept;
import org..openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Component
@FeignClient("SPRIGCLOUD-PROVIDER-DEPT")
public interface DeptClientService {

    @GetMapping("/dept/get/{id}")
    Dept queryById(@PathVariable("id") long id);

    @GetMapping("/dept/get")
    List<Dept> quertAll();

    @PostMapping("/dept/add")
    boolean addDept(Dept dept);

}

、修改dept-feign模块下的CumerDeptController

代码语言:javascript代码运行次数:0运行复制
package com.zibo.;

import com.zibo.springcloud.pojo.Dept;
import com.zibo.springcloud.service.DeptClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CumerDeptController {

    @Autowired
    DeptClientService service = null;

    //http://localhost:8001/dept/get/1
    @RequestMapping("/cumer/dept/get/{id}")
    public Dept getById(@PathVariable("id") long  id){
        return service.queryById(id);
    }

    @RequestMapping("/cumer/dept/add")
    public boolean add(Dept dept){
        return service.addDept(dept);
    }

    @RequestMapping("/cumer/dept/get")
    public List<Dept> get(){
        return service.quertAll();
    }

}

4、修改dept-feign模块下的主启动类

代码语言:javascript代码运行次数:0运行复制
package com.zibo.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.flix.eureka.EnableEurekaClient;
import org..openfeign.EnableFeignClients;
import org.annotation.ComponentScan;

@SpringBootApplication
@EnableEurekaClient //自动在服务启动后自动注册到Eureka中
//feign客户端注解,并指定要扫描的包以及配置接口DeptClientService
@EnableFeignClients(basePackages = {"com.zibo.springcloud"})
//扫描所有自己的包,让所有注解也能生效
@ComponentScan("com.zibo.springcloud")
public class FeignDeptCumer_80 {
    public static void main(String[] args) {
        SpringApplication.run(FeignDeptCumer_,args);
    }
}

5、测试访问

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2025-01-06,如有侵权请联系 cloudcommunity@tencent 删除cloudfeign接口负载均衡spring

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/biancheng/1198320.html

相关标签:无
上传时间: 2025-07-23 11:22:27
留言与评论(共有 19 条评论)
本站网友 成都房产
10分钟前 发表
【Spring Cloud】006 一
本站网友 斜视
27分钟前 发表
形成了一套模板化的调用方法
本站网友 电力论坛
3分钟前 发表
这个是很多开发人员的规范;2
本站网友 空心村
27分钟前 发表
往往一个接口会被多处调用
本站网友 关于性的网站
10分钟前 发表
但是在实际开发中
本站网友 包头团购
26分钟前 发表
创建一个新的模块springcloud-cumer-dept-feign2
本站网友 粮食价格
4分钟前 发表
代码演示1
本站网友 子宫内膜增生
21分钟前 发表
代码演示1
本站网友 北海房屋出租
4分钟前 发表
简介Feign是声明式Web Service客户端
本站网友 保利城市果岭
7分钟前 发表
分享自作者个人站点/博客
本站网友 朱买臣传
10分钟前 发表
形成了一套模板化的调用方法
本站网友 中桥二手房
8分钟前 发表
在Feign的实现下
本站网友 光子嫩肤是什么
7分钟前 发表
在springcloud-api模块写服务导入feign坐标:(在springcloud-api和springcloud-cumer-dept-feign中分别导入)代码语言:javascript代码运行次数:0运行复制 <!--feign--> <dependency> <groupId>org.</groupId> <artifactId>spring-cloud-starter-feign</artifactId> <version>1.4.7.RELEASE</version> </dependency>DeptClientService接口:代码语言:javascript代码运行次数:0运行复制package com.zibo.springcloud.service; import com.zibo.springcloud.pojo.Dept; import org..openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import java.util.List; @Component @FeignClient("SPRIGCLOUD-PROVIDER-DEPT") public interface DeptClientService { @GetMapping("/dept/get/{id}") Dept queryById(@PathVariable("id") long id); @GetMapping("/dept/get") List<Dept> quertAll(); @PostMapping("/dept/add") boolean addDept(Dept dept); }
本站网友 扬州采购
18分钟前 发表
代码演示1
本站网友 应对金融危机
8分钟前 发表
现在是一个微服务接口上面标注一个Feign注解)
本站网友 中国电
12分钟前 发表
代码演示1
本站网友 qq云存储
8分钟前 发表
在springcloud-api模块写服务导入feign坐标:(在springcloud-api和springcloud-cumer-dept-feign中分别导入)代码语言:javascript代码运行次数:0运行复制 <!--feign--> <dependency> <groupId>org.</groupId> <artifactId>spring-cloud-starter-feign</artifactId> <version>1.4.7.RELEASE</version> </dependency>DeptClientService接口:代码语言:javascript代码运行次数:0运行复制package com.zibo.springcloud.service; import com.zibo.springcloud.pojo.Dept; import org..openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import java.util.List; @Component @FeignClient("SPRIGCLOUD-PROVIDER-DEPT") public interface DeptClientService { @GetMapping("/dept/get/{id}") Dept queryById(@PathVariable("id") long id); @GetMapping("/dept/get") List<Dept> quertAll(); @PostMapping("/dept/add") boolean addDept(Dept dept); }
本站网友 散神
9分钟前 发表
所以