1. 程式人生 > >Spring Cloud 入門Eureka -Consumer服務消費(聲明式Feign)(三)

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

load control interface github example TP oca des 整合

  Spring Cloud Feign是一套基於Netflix Feign實現的聲明式服務調用客戶端。它使得編寫Web服務客戶端變得更加簡單。我們只需要通過創建接口並用註解來配置它既可完成對Web服務接口的綁定。它具備可插拔的註解支持,包括Feign註解、JAX-RS註解。它也支持可插拔的編碼器和解碼器。Spring Cloud Feign還擴展了對Spring MVC註解的支持,同時還整合了Ribbon和Eureka來提供均衡負載的HTTP客戶端實現。

1、pom.xml,這裏有所不同dependencyManagement有變化,之前的跑不通,依賴標紅處為修改

<?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.example</groupId> <
artifactId>eurekaRibbon</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>eurekaRibbon</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</
groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.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> </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>Dalston.SR3</version> --> <version>Camden.SR4</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> </project>

2、主類:通過@EnableFeignClients註解開啟掃描Spring Cloud Feign客戶端的功能

package com.example.demo;

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;

//掃描com.example.demo.service下的FeignClient @EnableFeignClients(basePackages={"com.example.demo.service"}) @EnableDiscoveryClient @SpringBootApplication public class EurekaFeignApplication { public static void main(String[] args) { SpringApplication.run(EurekaFeignApplication.class, args); } }

3、ClientFeignController

package com.example.demo.controller;

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

import com.example.demo.service.IFeign;

@RestController
public class ClientFeignController {

    @Autowired
    IFeign iFeign;

    @GetMapping("/consumer")
    public String all() {
        // 發起REST請求
        return iFeign.all();
    }
    
}

4、IFeign 一個FeignClient 聲明 服務提供者的信息

package com.example.demo.service;

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

//聲明服務提供名 @FeignClient(
"eurekaClient") public interface IFeign {
//指定要消費的接口名稱
// @GetMapping("/consumer") @RequestMapping(value="/all",method={RequestMethod.GET}) String all(); }

 比上篇使用Ribbon @LoadBalanced,更加具有可配置性

 如果使用Dalston版,FeignClient使用GetMapping 會報java.lang.NoClassDefFoundError: feign/Feign$Builder,網上查了很多 暫時沒有找到原因, 故而 換成Camden版。

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