目标
- 新建gateway项目
- 配置网关
- 监控actuator
新建gateway项目
D:\springcloud\gateway
file-new -maven project-create a simple project-工程名为gateway
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<groupId>com.example.demo</groupId>
<artifactId>gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.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-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- 网关健康情况、路由情况监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</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>
</plugins>
</build>
</project>
主启动类
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
配置文件application.yml
#springboot基本配置
spring:
application:
name: gateway
#gateway新增配置
cloud:
gateway:
enabled: true
discovery:
locator:
#路由前缀
routeIdPrefix: ${spring.application.name}
#从注册中心获取注册的服务
enabled: true
#默认服务名大写,我们改为服务id小写,方便程序调用时引用服务名称,这个规则自己牢记
lowerCaseServiceId: true
#负载均衡
loadbalancer:
#找不到服务默认返回503,我们改为返回404
use404: true
server:
port: 9001
eureka:
instance:
instanceId: ${eureka.instance.ipAddress}:${spring.application.name}:${server.port}
ipAddress: 127.0.0.1
#是否优先使用ip地址
preferIpAddress: true
#用上述三行替代localhost,改为用ip地址访问
#hostname: localhost
client:
#是否在eureka服务器上注册自己的信息以供其他服务发现 (server端为单机时false,server端有负载时为true client端为true)
registerWithEureka: true
#是否获取eureka服务器注册表上的注册信息 server端为false,client端如果需要访问其他服务为true
#gateway配置此处必须为true
fetchRegistry: true
#此处为eureka-server服务地址,即指定向谁注册
serviceUrl:
defaultZone: http://127.0.0.1:8090/eureka
management:
endpoint:
health:
showDetails: ALWAYS
gateway:
enabled: true
endpoints:
web:
base-path: /actuator
exposure:
include: "*"
测试
这次通过网关来访问注册中心服务 注意我们配置了服务名全小写
http://127.0.0.1:9001/服务名/服务地址
http://127.0.0.1:9001/eureka-service/hello?name=wang.qj
虽然看起来效果一样,但是此次请求经过了网关,并且有负载均衡
下一步可以把actuator加上,监控服务路由和负载情况
网关监控actuator
step 1:pom.xml
<!-- 网关健康情况、路由情况监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
step2 :application.yml的根节点下新增以下配置
management:
endpoint:
health:
showDetails: ALWAYS
gateway:
enabled: true
endpoints:
web:
base-path: /actuator
exposure:
include: "*
step3 测试
http://127.0.0.1:9001/actuator
可以看到以下信息
image.png上面列表的内容都可以试一下
关键是看一个网关
http://127.0.0.1:9001/actuator/gateway
他不能直接访问,下面有一些子目录,负责看详细情况:
- 全局过滤器列表:/actuator/gateway/globalfilters
- 路由过滤器列表:/actuator/gateway/routefilters
- 刷新路由缓存POST:/actuator/gateway/refresh(需要用postman之类的post请求测试工具)
- 查看路由表:/actuator/gateway/routes(这个比较关键,重点看一下)
- 查看路由信息:/actuator/gateway/routes/{id}
我们就看一下routes吧:
http://127.0.0.1:9001/actuator/gateway/routes
格式化后的内容如下(lb:前缀表示底层负载均衡loadbalance 简单了解,如果想手动配置路由建议自学):
[{
"route_id": "gatewayEUREKA-SERVICE",
"route_definition": {
"id": "gatewayEUREKA-SERVICE",
"predicates": [{
"name": "Path",
"args": {
"pattern": "/eureka-service/**"
}
}],
"filters": [{
"name": "RewritePath",
"args": {
"regexp": "/eureka-service/(?<remaining>.*)",
"replacement": "/${remaining}"
}
}],
"uri": "lb://EUREKA-SERVICE",
"order": 0
},
"order": 0
}, {
"route_id": "gatewayGATEWAY",
"route_definition": {
"id": "gatewayGATEWAY",
"predicates": [{
"name": "Path",
"args": {
"pattern": "/gateway/**"
}
}],
"filters": [{
"name": "RewritePath",
"args": {
"regexp": "/gateway/(?<remaining>.*)",
"replacement": "/${remaining}"
}
}],
"uri": "lb://GATEWAY",
"order": 0
},
"order": 0
}]
网友评论