1. 程式人生 > >Spring系列學習之Spring Cloud OpenFeign宣告性HTTP REST客戶端

Spring系列學習之Spring Cloud OpenFeign宣告性HTTP REST客戶端

英文原文:https://spring.io/projects/spring-cloud-openfeign

目錄

概述

特性

入門

快速開始

學習

文件

示例


概述

該專案通過自動配置和Spring環境以及其他Spring程式設計模型習慣用法為Spring Boot應用程式提供OpenFeign整合。

特性

  •      宣告性REST客戶端:Feign建立使用JAX-RS或Spring MVC註釋修飾的介面的動態實現


入門



@SpringBootApplication
@EnableFeignClients
public class WebApplication {

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

	@FeignClient("name")
	static interface NameService {
		@RequestMapping("/")
		public String getName();
	}
}

https://github.com/spring-cloud-samples/feign-eureka/blob/master/client/src/main/java/demo/HelloClientApplication.java 

package demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * @author Spencer Gibb
 */
@SpringBootApplication
@EnableDiscoveryClient
@RestController
@EnableFeignClients
public class HelloClientApplication {
	@Autowired
	HelloClient client;

	@RequestMapping("/")
	public String hello() {
		return client.hello();
	}

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

	@FeignClient("HelloServer")
	interface HelloClient {
		@RequestMapping(value = "/", method = GET)
		String hello();
	}
}

 

快速開始

使用Spring Initializr引導您的應用程式。

學習

文件

每個Spring專案都有自己的; 它詳細解釋瞭如何使用專案功能以及使用它們可以實現的功能。

2.1.0 RC3 PRE CURRENT Reference Doc. API Doc.
2.0.3 SNAPSHOT CURRENT Reference Doc. API Doc.
2.0.2 CURRENT GA Reference Doc.
API Doc.

示例

嘗試一些例子: