1. 程式人生 > >Spring Cloud 入門教程(三): 服務消費者(Feign)(Greenwich.RELEASE)

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

一、準備工作

同上節

二、新建maven 工程 service-feign (服務消費者 )

1、修改pom.xml

<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>wg</groupId>
  <artifactId>service-feign</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>service-feign</name>
  <url>http://maven.apache.org</url>

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

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

  <dependencies>
  	<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</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>
  </dependencyManagement>
</project>

2、建立啟動類

package wg;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {
	public static void main(String[] args) {
        SpringApplication.run( ServiceFeignApplication.class, args );
    }
}

3、建立application.yml

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

4、建立 service

package wg.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(value = "EUREKACLIENT")
public interface HelloService {
	@RequestMapping(value = "/",method = RequestMethod.GET)
    String sayHello();
}

5、建立controller

package wg.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import wg.service.HelloService;

@RestController
public class HelloController {
    @Autowired
    HelloService helloService;

    @GetMapping(value = "/hello")
    public String sayHi() {
        return helloService.sayHello();
    }
}

6、啟動專案,多次訪問:http://localhost:8765/hello

交替出現

7、訪問 http://localhost:8761

相關推薦

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

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

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

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

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

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

Spring Cloud 入門Eureka -Consumer服務消費

ppi package AR con .so 1.8 Coding ng- int 這裏介紹:LoadBalancerClient接口,它是一個負載均衡客戶端的抽象定義,下面我們就看看如何使用Spring Cloud提供的負載均衡器客戶端接口來實現服務的消費。 引用之前的文

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

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

SpringCloud入門3:服務消費者Feign

上一篇文章,講述瞭如何通過RestTemplate+Ribbon去消費服務,這篇文章主要講述如何通過Feign去消費服務。 一、Feign簡介 Feign是一個宣告式的偽Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要建立一個介面並註解。它具

Spring Cloud 入門教程(五): 路由閘道器(zuul) (Greenwich.RELEASE)

一、準備工作 啟動前面的專案:erurekaserver,eurekaclient1,service-feign,service

Spring Cloud 入門Eureka -Consumer服務消費聲明式Feign

load control interface github example TP oca des 整合   Spring Cloud Feign是一套基於Netflix Feign實現的聲明式服務調用客戶端。它使得編寫Web服務客戶端變得更加簡單。我們只需要通過創建接口並用

spring-cloud服務消費者Feign(Finchley版本)

Feign是一個宣告式的偽Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要建立一個介面並註解。它具有可插拔的註解特性,可使用Feign 註解和JAX-RS註解。Feign支援可插拔的編碼器和解碼器。Feign預設集成了Ribbon,並和Eureka結合,預設實現了負載均衡的效

Spring Cloud 入門教程(): 配置自動刷新

入門 stc pro 解決方案 con log clas ring color 之前講的配置管理, 只有在應用啟動時會讀取到GIT的內容, 之後只要應用不重啟,GIT中文件的修改,應用無法感知, 即使重啟Config Server也不行。 比如上一單元(Spring Clo

企業級 SpringCloud 教程 服務消費者Feign

pom https www. ram cat -h 客戶端 tin script 上一篇文章,講述了如何通過RestTemplate+Ribbon去消費服務,這篇文章主要講述如何通過Feign去消費服務。一、Feign簡介 Feign是一個聲明式的偽Http客戶端,它使得寫

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

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

SpringCloud教程 | 第篇: 服務消費者Feign(Finchley版本)

lns efault ret java 服務註冊 star tco target hub 上一篇文章,講述了如何通過RestTemplate+Ribbon去消費服務,這篇文章主要講述如何通過Feign去消費服務。 一、Feign簡介 Feign是一個聲明式的偽Http客戶端

史上最簡單的SpringCloud教程 | 第篇: 服務消費者Feign

最新Finchley版本請訪問: https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f3-feign/ 或者 http://blog.csdn.net/forezp/article/details/810409

【轉載】SpringCloud教程 | 第篇: 服務消費者Feign

上一篇文章,講述瞭如何通過RestTemplate+Ribbon去消費服務,這篇文章主要講述如何通過Feign去消費服務。 一、Feign簡介 Feign是一個宣告式的偽Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要建立一個

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

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

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

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

Spring Cloud之路: 服務消費者Feign

一、Feign簡介 Feign是一個宣告式的偽Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要建立一個介面並註解。它具有可插拔的註解特性,可使用Feign 註解和JAX-RS註解。Feign支援可插拔的編碼器和解碼器。Feign預設集成了Ribbon,

史上最簡單的SpringCloud教程 | 第篇: 服務消費者Feign

上一篇文章,講述了通過restTemplate+ribbon去消費服務,這篇文章主要講述通過feign去消費服務。 一、Feign簡介 Feign是一個宣告式的web服務客戶端,它使得寫web服務變得更簡單。使用Feign,只需要建立一個介面並註解。它具有可插拔的註

SpringCloud教程 | 第篇: 服務消費者Feign

上一篇文章,講述了通過restTemplate+ribbon去消費服務,這篇文章主要講述通過feign去消費服務。 一、Feign簡介 Feign是一個宣告式的web服務客戶端,它使得寫web服務變得更簡單。使用Feign,只需要建立一個介面並註解。它具有可插拔的註解特性,