SpringCloud 服务消费者(Feign)

SpringCloud 服务消费者(Feign)

Feign简介

Feign 是一个声明web服务客户端,这便得编写web服务客户端更容易,使用Feign 创建一个接口并对它进行注解,它具有可插拔的注解支持包括Feign注解与JAX-RS注解,Feign还支持可插拔的编码器与解码器,Spring Cloud 增加了对 Spring MVC的注解。在Spring Cloud中使用Feign, 我们可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验。Feign 采用的是基于接口的注解,Feign 整合了ribbon,具有负载均衡的能力。

准备工作

启动注册中心eureka-server,服务提供者say-hello。对这两个项目各自启动两个实例。

创建Feign客户端

1.新建一个springboot工程,取名为service-feign

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.definesys</groupId>
        <artifactId>my_cloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.definesys</groupId>
    <artifactId>service-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>service-feign</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.修改配置文件

server.port=4444

eureka.client.service-url.defaultZone=http://server2:11112/eureka/,http://server1:11111/eureka/

spring.application.name=service-feign

3.修改启动类

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceFeignApplication.class, args);
    }

}

4.定义Feign接口
通过@ FeignClient(“服务名”),来指定调用哪个服务。

@FeignClient(value = "say-hello")
public interface SayHelloFeignSer {
    @RequestMapping(value = "/sayHello",method = RequestMethod.GET)
    String feignSayHelloSer(@RequestParam(value = "helloName") String helloName);
}

5.创建controller,对外提供接口

@RestController
public class FeignServiceController {

    //编译器报错,无视。 因为这个Bean是在程序启动的时候注入的,编译器感知不到,所以报错。
    @Autowired
    private SayHelloFeignSer sayHelloFeignSer;

    @GetMapping("/feignSayHello")
    public String feignSayHelloCtr(@RequestParam("helloName")String helloName){
        return sayHelloFeignSer.feignSayHelloSer(helloName);

    }
}

6.启动service-feign
访问http://localhost:4444/feignSayHello?helloName=adaad,发现浏览器交替显示端口,说明feign已经集成ribbon。

Feign多参数请求

1.修改say-hello项目,在SayHelloController中添加两个方法

/**
     *  get请求多请求参数
     * @param userName
     * @param userPassword
     * @return
     */
    @RequestMapping(value = "/manyParams",method = RequestMethod.GET)
    public String manyParamsCtr(@RequestParam("userName")String userName,@RequestParam("userPassword")String userPassword){
        return "用户名:"+userName+",用户密码"+userPassword;
    }

    /**
     * post 对象参数
     * @param user
     * @return
     */
    @RequestMapping(value = "/objParams",method = RequestMethod.POST)
    public User objParamsCtr(@RequestBody User user){
        System.out.println(JSON.toJSON(user));
        return user;
    }
public class User {
    private String userName;

    private String userPassword;

    private String userSex;
    
    ...get(),set()
}

2.修改service-feign项目Feign接口

/**
     * 多参数get请求
     * @param userName
     * @param userPassword
     * @return
     */
    @RequestMapping(value = "/manyParams",method = RequestMethod.GET)
    String manyParamsSer(@RequestParam("userName")String userName,@RequestParam("userPassword")String userPassword);

    /**
     * 对象参数 post请求
     * @param user
     * @return
     */
    @RequestMapping(value = "/objParams",method = RequestMethod.POST)
    Object objParamsCtr(@RequestBody Object user);

3.修改service-feign项目FeignServiceController

    @GetMapping("/feignManyParams")
    public String feignManyParamsCtr(@RequestParam("userName")String userName,@RequestParam("userPassword")String userPassword){
        return sayHelloFeignSer.manyParamsSer(userName,userPassword);
    }

    @PostMapping("/feignObjParam")
    public Object feignObjParamCtr(@RequestBody Map<String,Object> map){
        return sayHelloFeignSer.objParamsCtr(map);
    }

注:为了不重复创建User实体类,这里用map去接收参数。
4.重新启动say-hello,service-feign两个项目
通过postman访问 /feignManyParams接口
图片描述
访问 /feignObjParam接口
图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/101170.html原文链接:https://javaforall.net

(0)
上一篇 2021年6月21日 下午2:00
下一篇 2021年6月21日 下午3:00


相关推荐

  • 【C++学习五】STL库的应用

    【C++学习五】STL库的应用文章目录初识C++之STL标准库1.C++STL的三大核心组件2.自定义函数与算法对容器实现操作3.基于自定义函数以及操作模板实现简易数字图像处理3.1图像灰度变换3.2图像二值化4.初识STL容器之:set集合5.初识STL容器之:map(关联容器)结语初识C++之STL标准库STL是StandardTemplateLibrary的缩写,中文译为“标准模板库”。STL是C++标准库的一部分。我们之前已经基本了解了C++中的模板templet,以及模板的作用。可以说,C

    2022年10月15日
    4
  • micro hdmi引脚定义义_Unity SRP 1.自定义管线「建议收藏」

    micro hdmi引脚定义义_Unity SRP 1.自定义管线「建议收藏」翻译汇总文章:HipHopBoy:UnitySRP系列翻译汇总​zhuanlan.zhihu.com原文链接:https://catlikecoding.com/unity/tutorials/scriptable-render-pipeline/custom-pipeline/​catlikecoding.com原作者:JasperFlick由于水平有限,可能翻译的会有错误,请大家在评论…

    2025年5月28日
    6
  • AI早知道|科大讯飞发布星火X2大模型;蚂蚁集团开源全模态大模型

    AI早知道|科大讯飞发布星火X2大模型;蚂蚁集团开源全模态大模型

    2026年3月14日
    3
  • 移动端app开发,框架的选择。

    移动端app开发,框架的选择。从事java开发快三年了,最近公司因项目要求需要做一款app,个人对这方面兴趣比较大,于是网上收集资料,框架的选择,技术的论证,到今天项目需求的各个功能都做出了相应的demo同时也打好了框架,接下来就是完成细节的部分了。接下来自己会在github更新自己相应的demo,最后也将会更新整个项目,写博客的目的就是希望自己养成做笔记的习惯,同时鞭策自己不断的学习新的知识。框架的选择,因为一直在关注前端方

    2022年6月29日
    34
  • 扣子Coze实战:从0到1搭建小红书图文改写智能体

    扣子Coze实战:从0到1搭建小红书图文改写智能体

    2026年3月12日
    3
  • interrupt用法C语言,interrupt

    interrupt用法C语言,interrupt知识点 interrupt 收集 茹盒呜编辑 康乃馨姐姐本知识点包括 1 1 disturb interrupt disrupt 的区别 2 单片机中断 interrupt0us 后面的 using1 是 3 c51 单片机中断程序中的 interrupt1 2 3 是由什么决 4 interrupt 的用法 5 C 语言 interrupt interrupt 相关知识 int

    2026年3月26日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号