网关:
- 无注册中心的服务,配置服务列表健康检查
- 注册中心服务配置路由
springcloud gateway 无注册中心的负载均衡,配置健康检查实时获取实例状态
新建项目
- 在线新建:https://start.spring.io/
- IDEA 新建
目录如下:
opengateway
├── pom.xml
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── gateway
│ │ │ └── Application.java
│ │ └── resources
│ │ ├── application-dev.yml
│ │ └── bootstrap.yml
选择组件,对应pom 文件如下:
pom.xml
<?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 https://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.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.weiyimu.opengateway</groupId>
<artifactId>opengateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>opengateway</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.0</spring-cloud.version>
<easy-captcha.version>1.6.2</easy-captcha.version>
<fastjson.version>1.2.79</fastjson.version>
<nacos.version>2021.1</nacos.version>
<sentinel.version>2021.1</sentinel.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--常用工具类 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!--验证码-->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>${easy-captcha.version}</version>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.18.2</version>
</dependency>
<!--启动nacos配置管理-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${nacos.version}</version>
</dependency>
<!--nacos 客户端 配置中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${nacos.version}</version>
</dependency>
<!-- sentinel
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>${sentinel.version}</version>
</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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
启动类
Application.java
package com.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
配置文件
bootstap.yml 配置,接入nacos注册配置中心
server:
port: 7080
# Spring配置
spring:
codec:
max-in-memory-size: 10MB ## 出现DataBufferLimitException: Exceeded limit on max bytes to buffer :262144
application:
name: opengateway
profiles:
active: dev
#日期格式化
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
cloud:
# sentinel:
# transport:
# dashboard: 127.0.0.1:8080
gateway:
metrics:
enabled: true
# 配置全局 http 超时
httpclient:
connect-timeout: 30000
response-timeout: 5s
discovery:
locator:
lower-case-service-id: true
nacos:
server-addr: localhost:8848
username: nacos
password: nacos
discovery:
namespace: dev
watch-delay: 15000 # 拉取配置中心服务的时间间隔
config:
namespace: dev
name: opengateway
#group: DEFAULT_GROUP 自定义需要修改
file-extension: YAML
management:
metrics:
tags:
application: ${spring.application.name}
endpoints:
web:
exposure:
include: "*"
# 开启指定端点
#include: "health,info,gateway,metrics"
logging:
level:
root: info
org.springframework.boot: info
org.springframework.cloud.gateway: info
org.springframework.cloud.loadbalancer: debug
com.alibaba.nacos.client.naming: ERROR
com.netflix: info
file:
path: ./log/opengateway.log
logback:
rollingpolicy:
max-history: 7
max-file-size: 100MB
配置无注册中心的服务 application.yml (不接入配置中心则放置在resources)
配置放在nacos dataId opengateway
### 配置无注册中心的服务,加入监控检查
spring:
cloud:
gateway:
routes:
- id: online #路由名
uri: lb://online # 服务名
predicates:
- Path=/online/**
filters:
- StripPrefix=1
metadata: # 单个服务请求超时时间
- connect-timeout=180000
- response-timeout=180000
- id: openapi #路由名
uri: lb://openapi #服务名对应注册中心服务名
predicates:
- Path=/openapi/** # 通过网关访问的地址路径
filters:
- StripPrefix=1 # 路径从第几个开始
metadata: # 单个服务请求超时时间
- connect-timeout=180000
- response-timeout=180000
discovery:
client:
simple:
instances: ## 服务发现实力列表,无注册中心服务实例的地址,新增或删除服务需要手动维护这个列表
online[0]:
uri: http://10.20.201.113:8080
online[1]:
uri: http://10.20.201.118:8080
online[2]:
uri: http://127.0.0.1:7080
loadbalancer:
service-discovery:
timeout: 9000
configurations: health-check # 启用health-check
## 全局
health-check:
initial-delay: 3s
clients: ## 服务发现无注册中心服务的实例(实例列表)健康检查
online: # 单个服务指定服务名
health-check:
interval: 30s
initial-delay: 1s
refetch-instances-interval: 5s
path:
online: /serviceEndpoint/json