Spring Cloud【Finchley】-06服务消费者整合Feign

简介: Spring Cloud【Finchley】-06服务消费者整合Feign

20190806093230928.jpg


概述


回想下我们在使用Eureka 和 Ribbon的时候是怎么调用注册在Eureka Server上的微服务的地址呢?

20181209213253513.png


可以看到其实是通过拼接的方式,当然了我们上面的这个例子只有一个参数 id,看起来没有这麻烦。


设想下如果有多个参数呢?

假设URL如下

http://localhost:8080/search?name=小工匠&age=20&username=artisan


那我们用RestTemplate如何调用对方的微服务呢? 可以采用如下方式

  @GetMapping("/searchUser")
  public User searchUser(String name ,String age ,String username) {
    Map<String, Object> paraMap = new HashMap<String ,Object>() {
      {
        put("name",name);
        put("age",age);
        put("username",username);
      }  
    };
   return  this.restTemplate.getForObject("http://microservice-provider-user/search?name={name}&age={age}&username={username}", User.class, paraMap);
  }


是不是已经很麻烦了?


Spring Cloud为我们整合了Fegin解决上述苦恼。


Feign官方文档: https://cloud.spring.io/spring-cloud-static/Finchley.SR2/single/spring-cloud.html#_spring_cloud_openfeign


Feign是Netflix开发的声明模板化的HTTP客户端。 在Spring Cloud中使用Feign,只需要创建一个接口,并在接口上添加一些注解即可。 Spring Cloud对Feign进行了增强,使Feign支持了SpringMVC的总结,并整合了Ribbon和Eureka。


实例

新建工程

在父工程上右键,新建Maven Module ,如下


20181209222253911.png

下面根据官方文档操作即可


20181209222608915.png


增加maven依赖

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


创建一个Feign接口,并添加@FeignClient注解

package com.artisan.micorservice.feignclient;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.artisan.micorservice.model.User;
@FeignClient("microservice-provider-user")
public interface UserFeignClient {
  @RequestMapping(method = RequestMethod.GET, value = "/user/{id}")
  public User findById(@PathVariable Long id);
}


FeignClient中的microservice-provider-user是要调用的微服务的名称,用于创建Ribbon负载均衡器。


因为我们这里使用了Eureka,所以Ribbon会把microservice-provider-user解析成Eureka Server中注册的服务。


另外,也可以通过url属性指定请求的URL ,比如 @FeignClient("microservice-provider-user", url="http://localhost:8900/")


修改Controller层,将RestTemplate改为调用Feign接口

package com.artisan.micorservice.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.artisan.micorservice.feignclient.UserFeignClient;
import com.artisan.micorservice.model.User;
@RestController
public class MovieController {
 @Autowired
 private UserFeignClient userClient;
  @GetMapping("/movie/{id}")
  public User findById(@PathVariable Long id) {
    return userClient.findById(id);
  }
}


启动类增加@EnableFeiginClients注解

package com.artisan.micorservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class MicorserviceConsumerFeginApplication {
  public static void main(String[] args) {
    SpringApplication.run(MicorserviceConsumerFeginApplication.class, args);
  }
}



测试

  1. 启动eureka server微服务
  2. 启动2个 provider-user微服务
  3. 启动该微服务


20181209225838562.png


2次请求http://localhost:7901/movie/1 ,观察 provider-user微服务的日志打印情况。

8900端口

20181209230212894.png

8901端口

2018120923015076.png



通过日志可以看到不仅实现了声明式的REST API调用,同时也实现了客户端的负载均衡。


源码

https://github.com/yangshangwei/SpringCloudMaster

相关文章
|
10天前
|
存储 Nacos 数据安全/隐私保护
【SpringCloud】Nacos的安装、Nacos注册、Nacos服务多级存储模型
【SpringCloud】Nacos的安装、Nacos注册、Nacos服务多级存储模型
31 1
|
20天前
|
应用服务中间件 nginx 微服务
SpringCloud解决feign调用token丢失问题
【5月更文挑战第2天】在feign调用中可能会遇到如下问题: * 同步调用中,token丢失,这种可以通过创建一个拦截器,将token做透传来解决 * 异步调用中,token丢失,这种就无法直接透传了,因为子线程并没有**token**,这种需要先将token从父线程传递到子线程,再进行透传
197 3
|
2天前
|
消息中间件 Java 持续交付
Spring Cloud Alibaba 项目搭建步骤和注意事项
Spring Cloud Alibaba 项目搭建步骤和注意事项
21 0
Spring Cloud Alibaba 项目搭建步骤和注意事项
|
3天前
|
人工智能 Java Spring
使用 Spring Cloud Alibaba AI 构建 RAG 应用
本文介绍了RAG(Retrieval Augmented Generation)技术,它结合了检索和生成模型以提供更准确的AI响应。示例中,数据集(包含啤酒信息)被加载到Redis矢量数据库,Spring Cloud Alibaba AI Starter用于构建一个Spring项目,演示如何在接收到用户查询时检索相关文档并生成回答。代码示例展示了数据加载到Redis以及RAG应用的工作流程,用户可以通过Web API接口进行交互。
|
10天前
|
负载均衡 算法 Java
【SpringCloud】Eureka原理分析、搭建Eureka服务、服务注册、服务发现
【SpringCloud】Eureka原理分析、搭建Eureka服务、服务注册、服务发现
26 3
|
21天前
|
消息中间件 Java 数据安全/隐私保护
Spring Cloud 项目中实现推送消息到 RabbitMQ 消息中间件
Spring Cloud 项目中实现推送消息到 RabbitMQ 消息中间件
|
21天前
|
负载均衡 监控 Java
我把Spring Cloud的超详细资料介绍给你,面试官不会生气吧?geigei
我把Spring Cloud的超详细资料介绍给你,面试官不会生气吧?geigei
|
21天前
|
负载均衡 Java 应用服务中间件
Spring Cloud 负载平衡的意义什么?
负载平衡是指将网络流量在多个服务器之间分布,以达到提高系统性能、增强可靠性和提供更好用户体验的目的。在负载平衡的架构中,多个服务器被组织成一个集群,共同处理用户的请求。
51 4
|
22天前
|
Java Nacos Sentinel
Spring Cloud Alibaba 面试题及答案整理,最新面试题
Spring Cloud Alibaba 面试题及答案整理,最新面试题
298 0
|
22天前
|
Java API Nacos
第十二章 Spring Cloud Alibaba Sentinel
第十二章 Spring Cloud Alibaba Sentinel
41 0