溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

8、服務(wù)發(fā)現(xiàn)&服務(wù)消費(fèi)者Feign

發(fā)布時(shí)間:2020-07-23 08:54:21 來(lái)源:網(wǎng)絡(luò) 閱讀:710 作者:huangjinjin520 欄目:編程語(yǔ)言

spring cloud的Netflix中提供了兩個(gè)組件實(shí)現(xiàn)軟負(fù)載均衡調(diào)用,分別是Ribbon和Feign。上一篇和大家一起學(xué)習(xí)了Ribbon。
Ribbon :Spring Cloud Ribbon是基于HTTP和TCP的客戶端負(fù)載工具,它是基于Netflix Ribbon實(shí)現(xiàn)的,它可以在客戶端配置 ribbonServerList(服務(wù)端列表),然后輪詢請(qǐng)求以實(shí)現(xiàn)均衡負(fù)載。
Feign :spring cloud feign 是一個(gè)使用起來(lái)更加方便的 HTTP 客戶端。 在使用ribbon時(shí),通常會(huì)使用RestTemplate實(shí)現(xiàn)對(duì)http請(qǐng)求的封裝,形成了模板化的調(diào)用方法。spring cloud feign在此基礎(chǔ)上做了進(jìn)一步的封裝,F(xiàn)eign是一種聲明式、模板化的HTTP客戶端。在Spring Cloud中使用Feign, 可以做到使用HTTP請(qǐng)求遠(yuǎn)程服務(wù)時(shí)能與調(diào)用本地方法一樣的編碼體驗(yàn),完全感知不到這是遠(yuǎn)程方法,更感知不到這是個(gè)HTTP請(qǐng)求。

1、 新建項(xiàng)目sc-eureka-client-consumer-feign,對(duì)應(yīng)的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>spring-cloud</groupId>
    <artifactId>sc-eureka-client-consumer-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>sc-eureka-client-consumer-feign</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>

        <!-- 說(shuō)明是一個(gè) eureka client -->
        <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-feign</artifactId> 
                    <version>1.4.5.RELEASE</version> 
             </dependency> -->

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

    </dependencies>
</project>

備注:spring cloud 2.x后spring-cloud-starter-feign已經(jīng)被標(biāo)識(shí)為過(guò)期,推薦使用spring-cloud-starter-openfeign
8、服務(wù)發(fā)現(xiàn)&服務(wù)消費(fèi)者Feign

2、 新建spring boot啟動(dòng)類ConsumerFeignApplication.java

package sc.consumer;

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

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ConsumerFeignApplication {

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

}

3、 創(chuàng)建配置文件bootstrap.yml和application.yml,對(duì)應(yīng)的內(nèi)容如下

bootstrap.yml

server:
    port: 5800

application.yml

spring:
    application:
        name: sc-eureka-client-consumer-feign

eureka:
    client:
        registerWithEureka: true #是否將自己注冊(cè)到Eureka服務(wù)中,默認(rèn)為true
        fetchRegistry: true #是否從Eureka中獲取注冊(cè)信息,默認(rèn)為true
        serviceUrl:
            defaultZone: http://localhost:5001/eureka/

4、 編寫feign客戶端

package sc.consumer.service;

import java.util.Map;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import sc.consumer.model.User;

@FeignClient(value="sc-eureka-client-provider")
public interface UserService {

    @GetMapping("/user/getUser/{id}")
    Map<String, Object> getUser(@PathVariable(value ="id") Long id);

    @RequestMapping("/user/listUser")
    Map<String, Object> listUser();

    @PostMapping("/user/addUser")
    Map<String, Object> addUser(@RequestBody User user);

    @PutMapping("/user/updateUser")
    Map<String, Object> updateUser(@RequestBody User user);

    @DeleteMapping("/user/deleteUser/{id}")
    Map<String, Object> deleteUser(@PathVariable(value ="id") Long id);

}

5、 分別啟動(dòng)注冊(cè)中心項(xiàng)目sc-eureka-server和服務(wù)提供者sc-eureka-client-provider

6、 啟動(dòng)項(xiàng)目sc-eureka-client-consumer-feign,并驗(yàn)證是否啟動(dòng)成功
方法一
8、服務(wù)發(fā)現(xiàn)&服務(wù)消費(fèi)者Feign

方法二
8、服務(wù)發(fā)現(xiàn)&服務(wù)消費(fèi)者Feign
7、 使用postman驗(yàn)證
查詢:
http://127.0.0.1:5800/feign/user/getUser/4
8、服務(wù)發(fā)現(xiàn)&服務(wù)消費(fèi)者Feign
列表:
http://127.0.0.1:5800/feign/user/listUser
8、服務(wù)發(fā)現(xiàn)&服務(wù)消費(fèi)者Feign
添加:
http://127.0.0.1:5800/feign/user/addUser
8、服務(wù)發(fā)現(xiàn)&服務(wù)消費(fèi)者Feign
更新:
http://127.0.0.1:5800/feign/user/updateUser
8、服務(wù)發(fā)現(xiàn)&服務(wù)消費(fèi)者Feign
刪除:
http://127.0.0.1:5800/feign/user/deleteUser/6
8、服務(wù)發(fā)現(xiàn)&服務(wù)消費(fèi)者Feign

備注:
sc-eureka-client-provider項(xiàng)目的UserController.java 需要修正
8、服務(wù)發(fā)現(xiàn)&服務(wù)消費(fèi)者Feign

https://gitee.com/hjj520/spring-cloud-2.x/tree/master/sc-eureka-client-consumer-feign
向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI