在使用springCloud时 可以配置网关作为权限控制等一些主要控制的入口,
首先,配置网关相关的pom文件:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.delta</groupId>
<artifactId>whLab-edm-gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>whLab-edm-gateway</name>
<description>whLab-edm-gateway</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
<myabtis.version>3.4.1</myabtis.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.57</version>
</dependency>
<dependency>
<groupId>com.delta.</groupId>
<artifactId>whlab-common</artifactId>
<version>1.0.0</version>
<!-- <systemPath>${project.basedir}/lib/whlab_common-1.0.0.jar</systemPath>
<systemPath>${pom.basedir}/lib/whlab_common-1.0.0.jar</systemPath>
<scope>system</scope>
-->
</dependency>
<dependency>
<groupId>com.delta</groupId>
<artifactId>whLab-Token-1.0.0</artifactId>
<version>1.0.0</version>
<!--
<systemPath>${pom.basedir}/lib/whLab-Token-0.0.1-SNAPSHOT.jar</systemPath>
<scope>system</scope>
-->
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</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-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 启动redis druid mysql -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${myabtis.version}</version>
</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>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.配置网关相关的consul配置,包括连接池、注册名、连接redis等
#程序启动端口
server:
port: 8082
spring:
application:
name: gateway-service
cloud:
#consule注册中心的地址
consul:
host: localhost
port: 8500
discovery:
serviceName: gateway-service
gateway:
globalcors:
corsConfigurations:
'[/**]':
allowedOrigins: "*"
allowedMethods:
# - GET
discovery:
locator:
lower-case-service-id: true
enabled: true
routes:
- id: consul-provider
uri: lb://consul-provider
predicates:
- Path=/edm/**
filters:
- StripPrefix=1
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/whlab_systemadmin_schema?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
password: 123456
initial-size: 5
min-idle: 5
max-active: 20
#超时时间
max-wait: 60000
#间隔多久进行检测,监测需要关闭的空闲连接
time-between-eviction-runs-millis: 60000
#一个连接在池中最小生存时间
min-evictable-idle-time-millis: 300000
validation-query: SELECT 'x'
test-while-idle: true
test-on-borrow: false
test-on-return: false
#打开PSCache,并且制定每个连接上PSCache的大小,分库分表比较多的DB此项可以设置为false
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
filters: stat
#WebStatFilter配置
web-stat-filter:
enabled: true
url-pattern: /*
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
session-stat-enable: false
session-stat-max-count: 1000
principal-session-name: admin
principal-cookie-name: admin
profile-enable: true
#StatViewServlet配置
stat-view-servlet:
enabled: true
url-pattern: /druid/*
reset-enable: true
login-username: root
login-password: 123456
##deny优先于allow,如果在deny列表中,就算在allow列表中,也会被拒绝。如果allow没有配置或者为空,则允许所有访问
allow:
deny:
redis:
database: 0
host: 10.148.132.23
port: 6379
password:
timeout: 1000ms
jedis:
pool:
max-active: 200
max-idle: 10
min-idle: 0
max-wait: -1ms
mybatis:
mapper-locations: classpath:com/delta/whLab/mapper/*.xml
type-aliases-package: com.delta.whlab.pojo
logging:
level:
com.delta.whLab.mapper: debug
3.在启动项上进行设置
package com.delta.whLab.application;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.GetMapping;
@SpringBootApplication
@EnableDiscoveryClient
@ComponentScan(basePackages = {"com.XXX"})
@MapperScan(basePackages= {"com.dXXX.mapper"})
public class GatewayDemoApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayDemoApplication.class, args);
}
@GetMapping("/whLab/test")
public String test() {
return "ok";
}
}
这样就配置好了,就可以加入各种过滤器了