前言:
springcloud gateway本身就是一個很優(yōu)秀的路由網(wǎng)關(guān)板鬓,alibaba本身也對這個網(wǎng)關(guān)支持的很好计福,只需要把原來的eureka的配置換成nacos的就可以了,但由于這個地方我沒有打算用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的朋友可以不加入,還是這個simons base也是一樣结胀,因為里面有一些基礎(chǔ)的工具類會在攔截器里面用到所以引入赞咙,普通的只是小demo的話可以這兩個配置去掉。
配置文件的話也是一樣在糟港,nacos服務(wù)發(fā)現(xiàn)的基礎(chǔ)上加上gateway的路由配置即可
routes里面配置里面:
id:設(shè)置成對應(yīng)服務(wù)的applicationname
uri: 其中l(wèi)b => 就是loadbalance負(fù)載均衡的意思攀操,就是說uri里面有這個nacos的那么就去找服務(wù)名為nacos的進(jìn)行負(fù)載均衡
predicates:說明下面的信息依附于上面服務(wù)上,path對應(yīng)的就是說要做路由的地址路徑
filters: 這個是gateway的自定義過濾器的配置在后面的篇章會說到這個東西的詳細(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);
}
}
只要上面三個配置里面配好了對與基本的路由來說就已經(jīng)足夠了那么把第一張的nacos 的測試項目中的test接口跑起來測試一下
當(dāng)我們在開啟相同的服務(wù)的時候秸抚,多次請求路由速和,會實現(xiàn)負(fù)載均衡的操作
ok完成