最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Cloud Gateway替代zuul作為API網(wǎng)關(guān)的方法

 更新時(shí)間:2023年02月20日 09:54:02   作者:russle  
本文簡要介紹如何使用Spring Cloud Gateway 作為API 網(wǎng)關(guān)(不是使用zuul作為網(wǎng)關(guān)),結(jié)合實(shí)例代碼給大家詳細(xì)講解,感興趣的朋友跟隨小編一起看看吧

本文簡要介紹如何使用Spring Cloud Gateway 作為API 網(wǎng)關(guān)(不是使用zuul作為網(wǎng)關(guān)),關(guān)于Spring Cloud Gateway和zuul的性能比較本文不再贅述,基本可以肯定Spring Cloud Finchley版本的gateway比zuul 1.x系列的性能和功能整體要好。

特別提醒:Spring Cloud Finchley版本中,即使你引入了spring-cloud-starter-netflix-zuul,也不是2.0版本的zuul

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

我們的介紹分為3個(gè)部分, 第一,pom文件,第二項(xiàng)目的基本架構(gòu),第三源碼和截圖。

第一,pom文件

因?yàn)槭褂肊ureka作為服務(wù)注冊和發(fā)現(xiàn),因此在pom中引入了eureka,各位可根據(jù)自己的實(shí)際情況修改。

<?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.yq</groupId>
    <artifactId>GatewayDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath/>
    </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>

    <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>

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

        <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-actuator</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.33</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.5</version>
        </dependency>

    </dependencies>


    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

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

</project>

第二,項(xiàng)目結(jié)構(gòu)

總共有3個(gè)項(xiàng)目,
第一個(gè)項(xiàng)目是eureka 注冊中心,非常簡單,基本就一個(gè)Application類。 端口7700
第二個(gè)項(xiàng)目是User service,也非常簡單提供兩個(gè)rest api,為了簡略不連接數(shù)據(jù)庫,直接在內(nèi)存中生成一組數(shù)據(jù)。端口6601
第三個(gè)項(xiàng)目就是我們的網(wǎng)關(guān)。端口6604

目前項(xiàng)目中集成websocket服務(wù)配置,本文暫不介紹可直接忽略。

第三,項(xiàng)目代碼和運(yùn)行截圖

網(wǎng)關(guān)的主代碼

package com.yq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;


@SpringCloudApplication
public class APIGatewayApplication  {
    private static final Logger logger = LoggerFactory.getLogger(APIGatewayApplication.class);

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route(r -> r.path("/baidu")
                        .uri("http://baidu.com:80/")
                )
               .route("websocket_route", r -> r.path("/apitopic1/**")
                .uri("ws://127.0.0.1:6605"))
                .route(r -> r.path("/userapi3/**")
                        .filters(f -> f.addResponseHeader("X-AnotherHeader", "testapi3"))

                        .uri("lb://user-service/")
                )
                .build();
    }

    public static void main(String[] args) {
        SpringApplication.run(APIGatewayApplication.class, args);
        logger.info(" Start APIGatewayApplication Done");
    }

}

網(wǎng)關(guān)的配置文件application.yml

server:
  port: 6604


#服務(wù)名
spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      filter:
        remove-non-proxy-headers:
          headers:
          - dummy
      routes:
        - id:  apiuser
        # 重點(diǎn)!/info必須使用http進(jìn)行轉(zhuǎn)發(fā),lb代表從注冊中心獲取服務(wù)
          uri: lb://user-service
          predicates:
          # 重點(diǎn)!轉(zhuǎn)發(fā)該路徑!,/userapi/**,
          - Path=/userapi/**
          # http://localhost:6601/userapi/user/users/2, 必須加上StripPrefix=1,否則訪問服務(wù)時(shí)會帶上userapi
          #而不是我們期望的去掉userapi,只保留**部分
          filters:
          - StripPrefix=1
        - id:  api2user
          uri: lb://user-service
          predicates:
          - Path=/userapi2/**
          filters:
          - StripPrefix=1


eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7700/eureka/

我們簡要分析分析一下配置文件

  • id: apiuser

# 重點(diǎn)!/info必須使用http進(jìn)行轉(zhuǎn)發(fā),lb代表從注冊中心獲取服務(wù)
uri: lb://user-service
predicates:
# 重點(diǎn)!轉(zhuǎn)發(fā)該路徑!,/userapi/,
- Path=/userapi/

# http://localhost:6601/userapi/user/users/2, 必須加上StripPrefix=1,否則訪問服務(wù)時(shí)會帶上userapi
#而不是我們期望的去掉userapi,只保留**部分
filters:
- StripPrefix=1

配置了一個(gè)路由apiuser, 當(dāng)路徑( - Path=/userapi/**),就轉(zhuǎn)發(fā)到服務(wù)(lb://user-service),同時(shí)把路徑中的userapi這部分去掉(- StripPrefix=1)。

運(yùn)行效果圖

直接訪問User service
http://localhost:6601/user/users/2

通過網(wǎng)關(guān)訪問user service
http://localhost:6604/userapi/user/users/2

參考文檔:

1, http://cloud.spring.io/spring-cloud-static/Finchley/single/spring-cloud.html#_spring_cloud_gateway
2, https://github.com/spring-cloud-samples/spring-cloud-gateway-sample

到此這篇關(guān)于Spring Cloud Gateway替代zuul作為API網(wǎng)關(guān)的文章就介紹到這了,更多相關(guān)Spring Cloud Gateway替代zuul作為API網(wǎng)關(guān)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • spring通過導(dǎo)入jar包和配置xml文件啟動(dòng)的步驟詳解

    spring通過導(dǎo)入jar包和配置xml文件啟動(dòng)的步驟詳解

    這篇文章主要介紹了spring通過導(dǎo)入jar包和配置xml文件啟動(dòng),本文分步驟通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 關(guān)于Spring統(tǒng)一異常處理及說明

    關(guān)于Spring統(tǒng)一異常處理及說明

    這篇文章主要介紹了關(guān)于Spring統(tǒng)一異常處理及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • 解決springboot引入swagger2不生效問題

    解決springboot引入swagger2不生效問題

    這篇文章主要為大家介紹了解決springboot引入swagger2不生效問題的方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • java自定義jar包讓jmeter使用的方法

    java自定義jar包讓jmeter使用的方法

    在本篇文章里小編給大家整理了一篇關(guān)于java自定義jar包讓jmeter使用的方法以及實(shí)例代碼,需要的朋友們參考下。
    2019-10-10
  • Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(8)

    Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(8)

    下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-07-07
  • Spring Boot整合MybatisPlus逆向工程(MySQL/PostgreSQL)

    Spring Boot整合MybatisPlus逆向工程(MySQL/PostgreSQL)

    MyBatis-Plus是MyBatis的增強(qiáng)工具,本文主要介紹了Spring Boot整合MybatisPlus逆向工程(MySQL/PostgreSQL),具有一定的參考價(jià)值,感興趣的可以了解一下
    2021-07-07
  • IDEA下因Lombok插件產(chǎn)生的Library source does not match the bytecode報(bào)錯(cuò)問題及解決方法(親測可用)

    IDEA下因Lombok插件產(chǎn)生的Library source does not match the bytecode報(bào)

    這篇文章主要介紹了IDEA下因Lombok插件產(chǎn)生的Library source does not match the bytecode報(bào)錯(cuò)問題及解決方法,親測試過好用,需要的朋友可以參考下
    2020-04-04
  • Java Session會話追蹤原理深入分析

    Java Session會話追蹤原理深入分析

    web開發(fā)階段我們主要是瀏覽器和服務(wù)器之間來進(jìn)行交互。瀏覽器和服務(wù)器之間的交互就像人和人之間進(jìn)行交流一樣,但是對于機(jī)器來說,在一次請求之間只是會攜帶著本次請求的數(shù)據(jù)的,但是可能多次請求之間是會有聯(lián)系的,所以提供了會話機(jī)制
    2022-11-11
  • Java使用NIO包實(shí)現(xiàn)Socket通信的實(shí)例代碼

    Java使用NIO包實(shí)現(xiàn)Socket通信的實(shí)例代碼

    本篇文章主要介紹了Java使用NIO包實(shí)現(xiàn)Socket通信的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • 關(guān)于IDEA無法預(yù)覽Markdown文件的解決思路

    關(guān)于IDEA無法預(yù)覽Markdown文件的解決思路

    在IntelliJ IDEA中,有時(shí)Markdown文件無法預(yù)覽可能是因?yàn)槲募P(guān)聯(lián)設(shè)置不正確或配置信息錯(cuò)誤,首先,檢查IDE的File Types設(shè)置,確保.md和.markdown后綴已正確注冊,其次,對照官方配置信息,調(diào)整Markdown設(shè)置
    2024-09-09

最新評論

佛山市| 榆中县| 莱西市| 安溪县| 交城县| 祁门县| 鄂托克前旗| 汝州市| 义乌市| 华亭县| 蒲江县| 绍兴县| 加查县| 建水县| 凤庆县| 调兵山市| 柯坪县| 象州县| 长治市| 晋宁县| 将乐县| 乐东| 德化县| 亚东县| 湘西| 绥阳县| 晋中市| 治县。| 扶绥县| 玉溪市| 涿州市| 九龙县| 聂荣县| 阜城县| 兴和县| 金湖县| 邵武市| 岳阳市| 福建省| 榆树市| 积石山|