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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • paddle tensorflow_平板屏保图片

    paddle tensorflow_平板屏保图片 tf.pad()文档如下,pad(tensor,paddings,mode=’CONSTANT’,name=None,constant_values=0)   Padsatensor.      Thisoperationpadsa`tensor`accordingtothe`paddings`youspecify.   `paddings`is…

    2022年8月13日
    1
  • spooling技术介绍

    spooling技术介绍SPOOLing(SimultaneousPeripheralOperationOn-Line)技术,即外部设备联机并行操作,是为实现低速输入输出设备与高速的主机之间的高效率数据交换而设计的。通常称为“假脱机技术”,又称为排队转储技术。  具体来说,SPOOLing技术在输入输出之间增加了“输入井”和“输出井”的排队转储环节,以消除用户的“联机”等待时间。而所谓“输入井”和“输出井”则是在高…

    2022年10月22日
    0
  • 7-16 三天打鱼两天晒网 (15分) 中国有句俗语叫“三天打鱼两天晒网”。假设某人从某天起,开始“三天打鱼两天晒网”,问这个人在以后的第N天中是“打鱼”还是“晒网”?「建议收藏」

    7-16 三天打鱼两天晒网 (15分) 中国有句俗语叫“三天打鱼两天晒网”。假设某人从某天起,开始“三天打鱼两天晒网”,问这个人在以后的第N天中是“打鱼”还是“晒网”?「建议收藏」7-16 三天打鱼两天晒网 (15分)中国有句俗语叫“三天打鱼两天晒网”。假设某人从某天起,开始“三天打鱼两天晒网”,问这个人在以后的第N天中是“打鱼”还是“晒网”?输入格式: 输入在一行中给出一个不超过1000的正整数N。输出格式: 在一行中输出此人在第N天中是“Fishing”(即“打鱼”)还是“Drying”(即“晒网”),并且输出“in day N”。 输入样例1: 103…

    2022年8月18日
    17
  • Linux内核有没有rootfs,Linux内核rootfs的初始化过程[通俗易懂]

    Linux内核有没有rootfs,Linux内核rootfs的初始化过程[通俗易懂]由于在下水平相当有限,不当之处,还望大家批评指正^_^在Linuxshell中执行mount命令,通常可以看到某个做了文件系统的磁盘分区或flash分区或内存文件系统做为所谓的根文件系统被mount到了挂载点/处。实际上内核中最初始的根文件系统,并不是来自内核外部,他是由内核自己构建出来的。为了说明这个过程,我们先说说mount的过程。系统调用sys_mount是在fs/namespace.c…

    2022年9月28日
    0
  • SecureCRT显示乱码的解决办法

    SecureCRT显示乱码的解决办法SecureCRT是一款支持SSH的终端仿真程序,用于连接运行包括Windows、UNIX和VMS的工具。对于学ARM的人来说,这个软件也是十分的好用!下面来看看SecureCRT的显示问题,如果没有设置好,那么就会出现乱码这种情况。比如:我发现在连接Linux系统之后,因为我装的是中文版的Linux系统,所以在显示中文的时候,SecureCRT显示出乱码。原因在于我们的Linux

    2022年7月17日
    45
  • 《Cocos2D权威指南》——3.5 CCTexture纹理类「建议收藏」

    《Cocos2D权威指南》——3.5 CCTexture纹理类

    2022年3月5日
    108

发表回复

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

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