1. 程式人生 > >Spring Cloud-06服務消費者整合Feign

Spring Cloud-06服務消費者整合Feign

文章目錄

概述

回想下我們在使用Eureka 和 Ribbon的時候是怎麼呼叫註冊在Eureka Server上的微服務的地址呢?

在這裡插入圖片描述

可以看到其實是通過拼接的方式,當然了我們上面的這個例子只有一個引數 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 ,如下

在這裡插入圖片描述


下面根據官方文件操作即可
在這裡插入圖片描述

增加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. 啟動該微服務

在這裡插入圖片描述

2次請求http://localhost:7901/movie/1 ,觀察 provider-user微服務的日誌列印情況。

8900埠

在這裡插入圖片描述

8901埠
在這裡插入圖片描述

通過日誌可以看到不僅實現了宣告式的REST API呼叫,同時也實現了客戶端的負載均衡。


原始碼

https://github.com/yangshangwei/SpringCloudMaster