springboot 2.0 + springcloud alibaba 整合gateway(一)基礎(chǔ)引入

前言:

springcloud gateway本身就是一個(gè)很優(yōu)秀的路由網(wǎng)關(guān),alibaba本身也對(duì)這個(gè)網(wǎng)關(guān)支持的很好,只需要把原來的eureka的配置換成nacos的就可以了,但由于這個(gè)地方我沒有打算用netfilx的hystrix,所以我把原本hystrix的一些斷路器配置去掉了,打算后期用Sentinel 替換。

代碼配置

首先在pom文件中加入gateway以及nacos的配置

<?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">
    <parent>
        <groupId>com.gitee.simons.cloud</groupId>
        <artifactId>simons-cloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>simons-gateway</artifactId>
    <name>${project.artifactId} [web]</name>
    <packaging>jar</packaging>

    <properties>
        <start-class>com.gitee.simons.gateway.GatewayApplication</start-class>
    </properties>

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

        <!--swagger相關(guān) -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>com.gitee.simons.cloud</groupId>
            <artifactId>simons-base</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <id>spring-boot</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <version>${spring-boot.version}</version>
                        <configuration>
                            <mainClass>${start-class}</mainClass>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>repackage</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>


    </profiles>


    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!--解決持續(xù)集成無法找到main類 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>${start-class}</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

由于gateway要做swagger的路由分發(fā)所以這邊加入了swagger的配置,如果不用swagger的朋友可以不加入,還是這個(gè)simons base也是一樣,因?yàn)槔锩嬗幸恍┗A(chǔ)的工具類會(huì)在攔截器里面用到所以引入,普通的只是小demo的話可以這兩個(gè)配置去掉。

配置文件的話也是一樣在,nacos服務(wù)發(fā)現(xiàn)的基礎(chǔ)上加上gateway的路由配置即可
routes里面配置里面:

id:設(shè)置成對(duì)應(yīng)服務(wù)的applicationname

uri: 其中l(wèi)b => 就是loadbalance負(fù)載均衡的意思,就是說uri里面有這個(gè)nacos的那么就去找服務(wù)名為nacos的進(jìn)行負(fù)載均衡

predicates:說明下面的信息依附于上面服務(wù)上,path對(duì)應(yīng)的就是說要做路由的地址路徑

filters: 這個(gè)是gateway的自定義過濾器的配置在后面的篇章會(huì)說到這個(gè)東西的詳細(xì)配置

spring:
  application:
    name: gateway
  main:
    banner-mode: "off" #on or off
  profiles:
    active: deve
  http:
    encoding:
      charset: UTF-8
      enabled: true
      force: true
  servlet:
    multipart:
      max-file-size: 100MB
      max-request-size: 100MB
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
      - id: nacos
        uri: lb://nacos
        predicates:
        - Path=/nacos/**
        filters:
        - StripPrefix=1
#        - JwtCheck=true
server:
  port: 2001


management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: ALWAYS
package com.gitee.simons.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

/**
 * @author jsm
 */
@EnableDiscoveryClient
@SpringBootApplication
@ComponentScan("com.gitee")
public class GatewayApplication {

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

    @Bean
    LoadBalancerInterceptor loadBalancerInterceptor(LoadBalancerClient loadBalance) {
        return new LoadBalancerInterceptor(loadBalance);
    }

}

只要上面三個(gè)配置里面配好了對(duì)與基本的路由來說就已經(jīng)足夠了那么把第一張的nacos 的測試項(xiàng)目中的test接口跑起來測試一下


image.png

當(dāng)我們?cè)陂_啟相同的服務(wù)的時(shí)候,多次請(qǐng)求路由,會(huì)實(shí)現(xiàn)負(fù)載均衡的操作


image.png

image.png

ok完成

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容