org.springframework.http.codec.ServerCodecConfigurer‘ that could not be found 报错;
出现该错误是因为Spring Cloud Gateway依赖了spring-boot-starter-web包
因为spring cloud gateway是基于webflux的,如果非要web支持的话需要导入spring-boot-starter-webflux而不是spring-boot-start-web。
我们这里的话 ,gateway模块单独引入依赖,不引入common模块;
// @Override
// public void addCorsMappings( CorsRegistry registry) {
// registry.addMapping( "/**" )
// .allowedOrigins( "*" )
// .allowCredentials( true)
// .allowedMethods( "GET" , "HEAD" , "POST" , "PUT" , "DELETE" ,"OPTIONS" )
// .maxAge( 3600 ) ;
// }
server:
port: 88
servlet:
context-path: "/"
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db_miaosha?serverTimezone= Asia/Shanghai
username: root
password: 123456
redis:
host: 192.168 .30.113
port: 6379
password:
connect-timeout: 10s
lettuce:
pool:
max-active: 8
max-wait: 200s
max-idle: 8
min-idle: 0
jackson:
time-zone: Asia/Shanghai
cloud:
nacos:
discovery:
server-addr: 127.0 .0.1:8848
gateway:
globalcors:
cors-configurations:
'[/**]' :
allowCredentials: true
allowedHeaders: "*"
allowedOrigins: "*"
allowedMethods: "*"
routes:
- id: user_router
uri: lb://miaosha-user
predicates:
- Path = /user/**
- id: goods_router
uri: lb://miaosha-goods
predicates:
- Path = /goods/**
- id: miaoShaGoods_router
uri: lb://miaosha-goods
predicates:
- Path = /miaoShaGoods/**
- id: miaosha_router
uri: lb://miaosha-miaosha
predicates:
- Path = /miaoSha/**, /verifyCode/**
filters:
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 1
redis-rate-limiter.burstCapacity: 2
redis-rate-limiter.requestedTokens: 1
key-resolver: "#{@pathKeyResolver}"
- id: order_router
uri: lb://miaosha-order
predicates:
- Path = /order/**
application:
name: miaosha-gateway
export function getServerUrl( url,module) {
if( module== "user" ) {
return "http://localhost:88/user/" +url;
} else if( module== "miaoShaGoods" ) {
return "http://localhost:88/miaoShaGoods/" +url;
} else if( module== "verifyCode" ) {
return "http://localhost:88/verifyCode/" +url;
} else if( module== "miaoSha" ) {
return "http://localhost:88/miaoSha/" +url;
} else if( module== "order" ) {
return "http://localhost:88/order/" +url;
}
}
package com.java1234.miaosha;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class MiaoshaGatewayApplication {
public static void main( String[ ] args) {
SpringApplication.run( MiaoshaGatewayApplication.class, args) ;
}
}
package com.java1234.miaosha.config;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;
/**
* 限流规则配置类
* @author java1234_小锋
* @site www.java1234.com
* @company Java知识分享网
* @create 2021 -01-23 11 :16
*/
@Configuration
public class KeyResolverConfiguration {
@Bean
public KeyResolver pathKeyResolver ( ) {
/*return new KeyResolver ( ) {
@Override
public Mono< String> resolve( ServerWebExchange exchange) {
return Mono.just( exchange.getRequest( ) .getURI( ) .getPath( )) ;
}
} ; */
return exchange -> Mono.just( exchange.getRequest( ) .getURI( ) .getPath( )) ;
}
}