Spring Cloud(二):服務消費者 Feign

一、Feign簡介:

Feign是一個聲明式的僞Http客戶端,使得寫Http客戶端變得更簡單。使用Feign,只需要創建一個接口並註解,可使用Feign 註解和JAX-RS註解。Feign默認集成了Ribbon,並和Eureka結合,默認實現了負載均衡的效果。

二、前提:

繼續用上一節的工程, 啓動eureka-server,端口爲8763; 啓動eureka-client-zyy-demo兩次,端口分別爲8762 、8764。

三、創建一個feign的服務

1.新建一個spring-boot工程,取名爲demo_eureka_feign,在pom文件引入Feign的起步依賴spring-cloud-starter-feign、Eureka的起步依賴spring-cloud-starter-eureka、Web的起步依賴spring-boot-starter-web。

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo_eureka_feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo_eureka_feign</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <!--Eureka的起步依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <!--Feign的起步依賴-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <!--Web的起步依賴-->
        <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>
    </dependencyManagement>

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

</project>

2.在配置文件application.yml中,指定項目名爲service-feign,端口號爲8765,服務註冊地址爲http://localhost:8763/eureka/

3.在項目啓動類DemoEurekaFeignApplication,加上@EnableFeignClients註解開啓Feign功能。

package com.example.demo_eureka_feign;

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

@SpringBootApplication  //springBoot 啓動標識
@EnableDiscoveryClient  //微服務調用
@EnableFeignClients     // 微服務調用
/**
 * @EnableFeignClients註解開啓Feign的功能
 */
public class DemoEurekaFeignApplication {

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

}

4.創建一個service層,定義一個feign接口,通過@ FeignClient(“服務名”),來指定調用哪個服務。

比如在代碼中調用了eureka-client-zyy-demo服務的“/hi”接口,代碼如下:

package com.example.demo_eureka_feign.service;

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

/**
 * 定義一個feign接口,通過@ FeignClient(“服務名”),來指定調用哪個服務。
 * 比如在代碼中調用了eureka-client-zyy-demo服務的“/hi”接口.
 */
@FeignClient(value = "eureka-client-zyy-demo")
public interface SchedualServiceHi {

    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);

}

5.創建一個controller層,對外暴露一個"/hi"的API接口,通過上面定義的Feign客戶端SchedualServiceHi 來消費服務。

package com.example.demo_eureka_feign.controller;

import com.example.demo_eureka_feign.service.SchedualServiceHi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 對外暴露一個"/hi"的API接口,通過上面定義的Feign客戶端SchedualServiceHi 來消費服務
 */
@RestController
public class HiController {
    @Autowired
    SchedualServiceHi schedualServiceHi;

    @GetMapping(value = "/hi")
    public String sayHi(@RequestParam String name) {
        return schedualServiceHi.sayHiFromClientOne( name );
    }

}

6.啓動程序,多次訪問http://localhost:8765/hi?name=forezp,瀏覽器交替顯示:

hi forezp,i am from port:8762

hi forezp,i am from port:8764

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章