首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Spring Cloud【Finchley】-06服务消费者整合Feign

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

作者头像
小小工匠
发布2021-08-17 15:35:14
发布2021-08-17 15:35:14
4490
举报
文章被收录于专栏:小工匠聊架构小工匠聊架构

文章目录

概述

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

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

设想下如果有多个参数呢? 假设URL如下 http://localhost:8080/search?name=小工匠&age=20&username=artisan

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

代码语言:javascript
复制
  @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 ,如下


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

增加maven依赖

代码语言:javascript
复制
<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

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

代码语言:javascript
复制
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接口

代码语言:javascript
复制
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注解

代码语言:javascript
复制
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. 启动该微服务

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

8900端口

8901端口

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


源码

https://github.com/yangshangwei/SpringCloudMaster

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018/12/09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 概述
  • 实例
    • 新建工程
    • 增加maven依赖
    • 创建一个Feign接口,并添加@FeignClient注解
    • 修改Controller层,将RestTemplate改为调用Feign接口
    • 启动类增加@EnableFeiginClients注解
    • 测试
  • 源码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档