1. 程式人生 > >Spring Cloud入門教程之服務消費者 Feign(三)(Finchley版本+Boot2.0)

Spring Cloud入門教程之服務消費者 Feign(三)(Finchley版本+Boot2.0)

什麼是Feign?

        Feign是受到Retrofit,JAXRS-2.0和WebSocket的影響,它是一個java的到http客戶端繫結的開源專案。 Feign的主要目標是將Java Http 客戶端變得簡單。

推薦部落格:

常見錯誤:

1、Spring Cloud服務消費者使用Feign,不識別@EnableFeignClients 註解解決辦法

 查閱資料後是Spring Cloud對Feign的支援由org.springframework.cloud:spring-cloud-netflix-core 移到org.springframework.cloud:spring-cloud-openfeign-core下。

     報錯資訊:cannot resolve sysmol EnableFeignClients

    

    解決方案:

    更改Spring Cloud的版本Finchley.RC2換為Dalston.RC1由於Finchley.RC2使用需要Spring Boot2.0,這裡需要把Spring Boot版本換到1.5即可

<dependencies>
   <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>Dalston.RC1</version>
      <type>pom</type>
      <scope>import</scope>
   </dependency>
</dependencies>

一、建立服務消費者

    2、專案pom.xml 新增

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

完整pom.xml

<?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>

	<groupId>com.serverfeign</groupId>
	<artifactId>serverfeign</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>serverfeign</name>
	<description>Demo project for Spring Boot</description>

	<!--<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.2.RELEASE</version>
		<relativePath/> <!– lookup parent from repository –>
	</parent>-->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>


	<!--<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>Finchley.RC2</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>-->

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<!--<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>-->
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.RC1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

</project>

    3、編寫服務消費Service和Controller

        通過@ FeignClient(“服務名”),來指定呼叫哪個服務

package com.serverfeign.serverfeign.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;


/**
 * 通過@FeignClient(“服務名”),來指定呼叫哪個服務
 * Created by zhoujh on 2018/6/04
 */
@FeignClient(value = "hello-service")
public interface HelloService {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

package com.serverfeign.serverfeign.controller;

import com.serverfeign.serverfeign.service.HelloService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

@RestController

public class HelloController {

@Autowired

HelloService helloService;

@RequestMapping(value = "/hi",method = RequestMethod.GET)

@ResponseBody

public String HiFeign(@RequestParam String name){

return helloService.sayHiFromClientOne(name);

}

}

4、修改配置檔案

server.port=8765
spring.application.name=service-feign
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

5、修改啟動類

在程式的啟動類ServiceFeignApplication ,加上@EnableFeignClients註解開啟Feign的功能

package com.serverfeign.serverfeign;

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;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServerfeignApplication {

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

二、啟動服務註冊中心、服務提供者(2個)、服務消費者(Feign)

三、重新整理消費者,檢視服務提供者後臺列印,此時已經完成了消費的負載均衡(這裡採用了輪詢策略)

Spring Boot與Spring Cloud學習使用可參看筆者部落格

相關推薦

Spring Cloud入門教程服務消費者 Feign(Finchley版本+Boot2.0)

什麼是Feign?         Feign是受到Retrofit,JAXRS-2.0和WebSocket的影響,它是一個java的到http客戶端繫結的開源專案。 Feign的主要目標是將Java Http 客戶端變得簡單。 推薦部落格: 常見錯誤:

SpringCloud服務消費者Feign

消費 apt boot cal port xmlns sco packaging url 一、Feign簡介Feign是一個聲明式的偽Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要創建一個接口並註解。它具有可插拔的註解特性,可使用Feign 註解和

Spring Cloud 入門教程(二): 服務消費者rest+ribbon(Greenwich.RELEASE)

一、準備工作,eclipse執行兩個例項 1、修改  eurekaclient1 中 application.yml

Spring Cloud入門教程斷路器 Hystrix(Finchley版本+Boot2.0)

什麼是Hystrix? Hystrix是Netflix開源的一款容錯框架,包含常用的容錯方法:執行緒隔離、訊號量隔離、降級策略、熔斷技術。在高併發訪問下,系統所依賴的服務的穩定性對系統的影響非常大,依賴有很多不可控的因素,比如網路連線變慢,資源突然繁忙,暫時不可用,服務離線

Spring Cloud入門教程路由閘道器 Zuul(Finchley版本+Boot2.0)

路由閘道器什麼是Zuul?     Zuul的主要功能是路由轉發和過濾器。路由功能是微服務的一部分,比如/api/payment轉發到到payment服務,/api/login轉發到到login服務。zuul預設和Ribbon結合實現了負載均衡的功能。 zuul有以下功能

Spring Cloud 入門教程 - Eureka服務註冊與發現

spring spring cloud spring cloud eureka spring boot 簡介 在微服務中,服務註冊與發現對管理各個微服務子系統起著關鍵作用。隨著系統水平擴展的越來越多,系統拆分為微服務的數量也會相應增加,那麽管理和獲取這些微服務的URL就會變得十分棘手,如果我們

Spring Cloud 入門教程(一): 服務註冊

1.  什麼是Spring Cloud? Spring提供了一系列工具,可以幫助開發人員迅速搭建分散式系統中的公共元件(比如:配置管理,服務發現,斷路器,智慧路由,微代理,控制匯流排,一次性令牌,全域性鎖,主節點選舉, 分散式session, 叢集狀態)。協調分散式環境中各

Spring Cloud 入門教程(一)服務註冊

1.  什麼是Spring Cloud? Spring提供了一系列工具,可以幫助開發人員迅速搭建分散式系統中的公共元件(比如:配置管理,服務發現,斷路器,智慧路由,微代理,控制匯流排,一次性令牌,全域性鎖,主節點選舉, 分散式session, 叢集狀態)。協調分散式環境中各個系統,為各類服務提供模

Spring Cloud 入門教程(八): 服務鏈路追蹤(Spring Cloud Sleuth)(Greenwich.RELEASE)

參考連結:https://blog.csdn.net/forezp/article/details/81041078 一、準備工

Spring Cloud學習筆記微服務實現Spring Boot+IDEA

我們先使用Spring Boot實現一個微服務,業務非常簡單: 1.商品微服務,通過商品id查詢商品的微服務 2.訂單微服務,通過訂單id查詢訂單資料,同時需要呼叫商品微服務查詢出訂單詳情資料對應的商品資料。 說明: 1.對於商品微服務而言,商品微服務是服務的提供者,訂單微服務是服務的消費

Spring Cloud學習筆記微服務實現Spring Boot+Spring Cloud+IDEA

在【Spring Cloud學習筆記之微服務實現(一)】中,我們實現了微服務,但是在實際的專案中,我們需要實現動態訪問微服務,在此之前,已經介紹了Spring Cloud和Eureka,並且實現了eureka註冊中心。現在我們實現一下動態呼叫。 注:註冊中心的服務在此期間保持啟動狀態。

Spring Cloud學習-Eureka、Ribbon和Feign

Spring Cloud學習-Eureka、Ribbon和Feign   Talk is cheap,show me the code , 書上得來終覺淺,絕知此事要躬行。在自己真正實現的過程中,會遇到很多莫名其妙的問題,而正是在解決這些問題的過程中,你會發現自己之前思維的

SpringCloud入門教程Eureka註冊中心

學習SpringCloud技術前提就是學習Eureka註冊服務中心,而Eureka註冊服務中心,它是什麼呢?今天小編就帶你一起了解一下吧!!! Eureka 1.認識Eureka 2.原理圖 3.入門案例 1.認識

Spring Cloud微服架構分散式配置中心

本文接之前的《Spring Cloud微服架構之分散式配置中心》,繼續來說說Spring Cloud Config的使用。 先來回顧一下,在前文中我們完成了什麼: 構建了config-server,連線到Git倉庫 在Git上建立了一個config-repo目錄,用來儲

Java EE入門教程系列第二章JSP——JSP指令與動作元件

2.3 指令與動作元件 2.3.1 page指令 page指令的基本語法為: <%@ page 屬性1="屬性1的值" 屬性2="屬性2的值"···%> 屬性值記得用“”或者‘’括起來,這樣寫比較規範,不易出錯。 舉例: <%@ page language=

Qt使用教程指定執行設定

在指定執行設定(二)一文中,我們介紹了指定偵錯程式設定、為基於Linux的裝置指定執行設定、為QNX裝置指定執行設定等。本文我們將繼續為大家介紹指定qmake專案執行設定的餘下所有內容,歡迎品鑑! 選擇執行環境 Qt Creator會自動選擇用於執行應用程式的基於裝置

Spring Cloud 入門教程(): 服務消費者Feign(Greenwich.RELEASE)

一、準備工作 同上節 二、新建maven 工程 service-feign (服務消費者 ) 1、修改pom.xm

Spring Cloud 入門教程(六): 用宣告式REST客戶端Feign呼叫遠端HTTP服務

首先簡單解釋一下什麼是宣告式實現? 要做一件事, 需要知道三個要素,where, what, how。即在哪裡( where)用什麼辦法(how)做什麼(what)。什麼時候做(when)我們納入how的範疇。 1)程式設計式實現: 每一個要素(where,wh

Spring Cloud 入門教程(四): 分布式環境下自動發現配置服務

.html article png discover ice conf label tail 註釋 前一章, 我們的Hello world應用服務,通過配置服務器Config Server獲取到了我們配置的hello信息“hello world”. 但自己的配置文件中必須配

Spring Cloud 入門教程 - 搭建配置中心服務

Spring spring boog spring cloud 簡介 Spring Cloud 提供了一個部署微服務的平臺,包括了微服務中常見的組件:配置中心服務, API網關,斷路器,服務註冊與發現,分布式追溯,OAuth2,消費者驅動合約等。我們不必先知道每個組件有什麽作用,隨著教程的深入,我