首页 » spring

org.springframework.data.repository.query.ParameterOutOfBoundsException: Invalid parameter index! You seem to have declared too little query method parameters!

原因是没有少了个By,30后面必须有by

Flux<MySeries> findTop30ByOrderByRatingDesc();

open feign默认不打印日志,这对debug很不方便,配置方法如下

[1]配置config

@Configuration
public class UserClientConfig {

    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.HEADERS;
    }
} 

[2]配置feignclient

@FeignClient(name = "User", url = "http://localhost:8080",configuration=UserClientConfig.class)
public interface UserClient {

    @RequestMapping(method = RequestMethod.GET, value = "/user")
    List<User> getAllUsers();    

}

[2]配置application.properties

logging.level..UserClient:DEBUG

Caused by: org.h2.jdbc.JdbcSQLDataException: Value too long for column """BRIEF"" VARCHAR(255) ,h2 生成的默认长度是255
[1]报错如下:
Caused by: org.h2.jdbc.JdbcSQLDataException: Value too long for column """BRIEF"" VARCHAR(255)": "STRINGDECODE('\uff1aDuenpatra\uff08pim\uff09\u5728\u5979\u7684\u7956\u6bcd\u4f4f\u5b85\u7684Prapim\u5f00\u4e86\u4e00\u5bb6\u81ea... (320)"; SQL statement:
insert into my_season (actors, brief, cat, cover, finish, pinyin, plays, rating, score, season_no, title, title_en, year, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [22001-199]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:455) ~[h2-1.4.199.jar:1.4.199]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:427) ~[h2-1.4.199.jar:1.4.199]
    at org.h2.message.DbException.get(DbException.java:205) ~[h2-1.4.199.jar:1.4.199]
    at org.h2.table.Column.validateConvertUpdateSequence(Column.java:441) ~[h2-1.4.199.jar:1.4.199]
    at org.h2.table.Table.validateConvertUpdateSequence(Table.java:824) ~[h2-1.4.199.jar:1.4.199]
    at org.h2.command.dml.Insert.insertRows(Insert.java:175) ~[h2-1.4.199.jar:1.4.199]
    at org.h2.command.dml.Insert.update(Insert.java:132) ~[h2-1.4.199.jar:1.4.199]
    at org.h2.command.CommandContainer.update(CommandContainer.java:133) ~[h2-1.4.199.jar:1.4.199]
    at org.h2.command.Command.executeUpdate(Command.java:267) ~[h2-1.4.199.jar:1.4.199]
    at org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:200) ~[h2-1.4.199.jar:1.4.199]
    at org.h2.jdbc.JdbcPreparedStatement.executeUpdate(JdbcPreparedStatement.java:154) ~[h2-1.4.199.jar:1.4.199]
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-3.2.0.jar:na]
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-3.2.0.jar:na]
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:175) ~[hibernate-core-5.3.9.Final.jar:5.3.9.Final]
    ... 89 common frames omitted

[2]解决办法,加上下面的声明,注意如果是内存中,需要重启,如果文件中,需要删除重新运行

@Column(length = 2000)
private String brief;

[3]或者


@Column(columnDefinition = "TEXT")
private String brief;

升级java11 ,和spring cloud 2.1.1后,默认的mysql driverclass 是这个 com.mysql.cj.jdbc.Driver,报错如下:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unsatisfied dependency expressed through method 'entityManagerFactory' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactoryBuilder' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unsatisfied dependency expressed through method 'entityManagerFactoryBuilder' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaVendorAdapter' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.JpaVendorAdapter]: Factory method 'jpaVendorAdapter' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:769)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:509)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1288)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1127)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:853)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
        at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:127)
        at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
        at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
        ... 28 more

解决如下,替换原来的依赖

原来

 <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

替换为,或者不指定版本,跟随spring boot

 <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
<version>8.0.13</version>
        </dependency>

spring cloud gateway 自带了个限流器filter,就是RequestRateLimiter,可以使用令牌桶算法来限流

先说下令牌桶算法,就是:每秒向令牌桶放N个令牌,令牌桶的最大容积是M个,那么如果请求过来了,会从令牌桶中拿出一个令牌,令牌桶还剩M-1个。以此类推,当令牌桶的数量还剩0个的时候,就拒绝接受请求。所以这个情况可以支持突发性的请求情况。就是我一致很小量的请求,但是突然来了一大波请求,令牌桶容积决定了它能接受的最大突发情况。

首先看下spring cloud gateway的配置,注意要用到redis

    - id: lb2thorquest
        uri: lb://THORQUEST
        order: 9999
        predicates:
        - Host=10.1.0.75:9001
        filters:
        - name: RequestRateLimiter
          args:
            redis-rate-limiter.replenishRate: 1  # 令牌桶放入的速度
            redis-rate-limiter.burstCapacity: 3  # 令牌桶的容积
            key-resolver: "#{@remoteAddrKeyResolver}" #SPEL表达式去的对应的bean
  redis:
    host: 10.10.2.175
    port: 6379
    database: 5

引入redis

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
        </dependency>

这个key resolver会生成一个key,为这个key生成一个令牌桶。如果是客户端ip,那么就是为每个ip生成一个令牌桶。如果是个固定值,就是全局的令牌桶。

  @Bean
    KeyResolver remoteAddrKeyResolver() {
        return exchange -> Mono.just(Objects.requireNonNull(exchange.getRequest().getRemoteAddress()).getHostName());
    }

那么使劲刷新url,出现429 too many request 就表示成功了,