1. 程式人生 > >【SpringCloud Greenwich版本】第三章:服務消費者(Feign)

【SpringCloud Greenwich版本】第三章:服務消費者(Feign)

一、SpringCloud版本

本文介紹的Springboot版本為2.1.1.RELEASE,SpringCloud版本為Greenwich.RC1,JDK版本為1.8,整合環境為IntelliJ IDEA

二、Feign介紹

Feign是一個宣告式的Web服務客戶端。這使得Web服務客戶端的寫入更加方便。要使用Feign建立一個介面並對其進行註釋。它具有可插入註釋支援,包括Feign註釋和JAX-RS註釋。Feign還支援可插拔編碼器和解碼器。Spring Cloud增加了對Spring MVC註釋的支援,並使用Spring Web中預設使用的HttpMessageConverters。Spring Cloud整合Ribbon和Eureka以在使用Feign時提供負載均衡的http客戶端。

整合Feign需在您的專案中包含Feign,請使用組org.springframework.cloud和工件ID spring-cloud-starter-openfeign的啟動器

三、建立Feign服務

  • 3.1建立

選擇Cloud Routing–Feign建立一個新的module工程,取名為cloudcustomer
在這裡插入圖片描述pom配置檔案如下,可以看到增加了spring-cloud-starter-openfeign依賴

<?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.jthao</groupId> <artifactId>cloudcustomer</artifactId> <
version>
0.0.1-SNAPSHOT</version> <name>cloudcustomer</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.jthao</groupId> <artifactId>cloudser</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> </project>

修改工程中配置檔案,指定服務註冊中心地址,埠為8004,服務名為cloudcustomer

eureka.client.service-url.defaultZone: http://localhost:8001/eureka/
server.port=8004
spring.application.name=cloudcustomer
  • 3.2啟動

需在啟動類上增加@EnableDiscoveryClient和@EnableFeignClients註解,其中@EnableFeignClients註解開啟訪問功能

package com.jthao.cloudcustomer;

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
@EnableFeignClients
@SpringBootApplication
public class CloudcustomerApplication {

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

}

通過Feign定義一個介面,這裡我們定義為cloudclient工程的test介面

package com.jthao.cloudcustomer.service;

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

@FeignClient(name = "cloudclient")
public interface TestService {
    @RequestMapping("/test")
    String hello(@RequestParam String name);
}

再定義一個controller來訪問這個介面

package com.jthao.cloudcustomer.controller;

import com.jthao.cloudcustomer.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @Autowired
    TestService testService;

    @RequestMapping(value = "/test")
    public String test(@RequestParam String name) {

        return testService.hello(name);
    }

}
  • 3.3訪問

通過瀏覽器多次訪問http://localhost:8004/test?name=honghong,我們可以看到如下展示

honghong===埠:8002被呼叫了===
honghong===埠:8003被呼叫了===