小编Ric*_*cky的帖子

如何在Spring Boot中为每个用户设置速率限制?

我正在开发一个Spring Boot Rest API来处理大量的传入请求调用.我的控制器如下所示:

@RestController

public class ApiController {
    List<ApiObject>  apiDataList;   

    @RequestMapping(value="/data",produces={MediaType.APPLICATION_JSON_VALUE},method=RequestMethod.GET)
    public ResponseEntity<List<ApiObject>> getData(){                                       
        List<ApiObject> apiDataList=getApiData();
        return new ResponseEntity<List<ApiObject>>(apiDataList,HttpStatus.OK);
    }
    @ResponseBody 
    @Async  
    public List<ApiObject>  getApiData(){
        List<ApiObject>  apiDataList3=new List<ApiObject> ();
        //do the processing
        return apiDataList3;
    }
}
Run Code Online (Sandbox Code Playgroud)

所以现在我想为每个用户设置一个ratelimit.假设每个用户每分钟只能请求5个请求或类似的东西.如何设置每个用户的速率限制,每分钟只能进行5次api呼叫,如果用户请求的次数超过了我可以发回429响应?我们需要他们的IP地址吗?

任何帮助表示赞赏.

java rest controller spring-mvc spring-boot

18
推荐指数
3
解决办法
1万
查看次数

ApplicationEventMulticaster未初始化 - 在通过上下文组播事件之前调用'刷新'

我正在开发一个spring启动应用程序并使用HikariCP连接池和JDBC模板将数据插入Database.But当我开始时我得到了错误.

java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1c90ca10: startup date [Wed Jul 26 13:55:18 SGT 2017]; root of context hierarchy
    at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:404) [spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.ApplicationListenerDetector.postProcessBeforeDestruction(ApplicationListenerDetector.java:97) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:253) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:578) [spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:554) [spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:961) [spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:523) [spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:968) [spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1033) [spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:555) [spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at com.dmmltasmu.DriverguidanceapiApplication.main(DriverguidanceapiApplication.java:24) [classes/:na]

2017-07-26 13:55:20.222 …
Run Code Online (Sandbox Code Playgroud)

java spring maven

17
推荐指数
1
解决办法
5万
查看次数

如何在spring boot中从属性文件中读取限定符?

我有一个资格赛,我从中读取

public class TestController{
     @Autowired
     @Qualifier("jdbc")
     private JdbcTemplate jtm;
     //.....
}
Run Code Online (Sandbox Code Playgroud)

限定符"jdbc"是定义为的bean

@Bean(name = "jdbc")
@Autowired
public JdbcTemplate masterJdbcTemplate(@Qualifier("prod") DataSource prod) {
            return new JdbcTemplate(prod);
        }
Run Code Online (Sandbox Code Playgroud)

这是返回该限定符的数据源并且工作正常.

现在我想要从application.properties中读取限定符名称.所以我将代码更改为

public class TestController{
     @Autowired
     @Qualifier("${database.connector.name}")
     private JdbcTemplate jtm;
     //.....

}
Run Code Online (Sandbox Code Playgroud)

在那里database.connector.name=jdbc,我application.properties.

但是,当我这样做时,会抛出一个错误

应用程序未能启动

描述:

main.java.rest.TestController中的字段userService需要一个无法找到的类型为'org.springframework.jdbc.core.JdbcTemplate'的bean.

行动:

考虑在配置中定义类型为'org.springframework.jdbc.core.JdbcTemplate'的bean.

任何帮助表示赞赏.

java properties qualifiers spring-boot

8
推荐指数
2
解决办法
3884
查看次数

如何在ggplot中绘制堆积和分组的条形图?

我有一个如下所示的数据框:

id    month     type    count
___  _______   ______   ______
1      1          1       10
1      1          2       09
1      1          3       26
1      2          1       60
1      2          2       90
2      2          3       80
2      1          1       10
2      1          2       09
2      1          3       26
2      2          1       60
2      2          2       90
2      2          3       80
3      1          1       10
3      1          2       09
3      1          3       26
3      2          1       60
3      2          2       90
3      2          3 …
Run Code Online (Sandbox Code Playgroud)

r bar-chart ggplot2

7
推荐指数
2
解决办法
9400
查看次数

筛选R中某个时间范围之间的所有天数

我有一个如下数据框:

entry_no      id            time
_________     ___           _____
1              1        2016-09-01 09:30:09
2              2        2016-09-02 10:36:18
3              1        2016-09-01 12:27:27
4              3        2016-09-03 10:24:30
5              1        2016-09-01 12:35:39
6              3        2016-09-06 10:19:45
Run Code Online (Sandbox Code Playgroud)

从中我想过滤每天上午9点至上午10点之间发生的条目。我知道有一天我可以使用类似以下内容的条目:

results=filter(df,time>='2016-09-01 09:00:00' && time<='2016-09-01 10:00:00') 
Run Code Online (Sandbox Code Playgroud)

但要过滤出每月的每一天的结果。不胜感激。

r filter dataframe dplyr

3
推荐指数
2
解决办法
1280
查看次数

如何在 spring 中查询 postgres 整数数组

我正在使用 Java 7 和 JDBC 模板从 PostgreSQL 查询整数数组。我的代码如下:

@Autowired
private JdbcTemplate jdbcTemp;

String SQL = "select item_list from public.items where item_id=1";
List<Integer> ListOfitems=jdbcTemp.queryForList(SQL , Integer.class);
Run Code Online (Sandbox Code Playgroud)

我的item_list专栏integer[]在 PostgreSQL 中。但是当我这样尝试时,它会抛出一个错误:

类型 int psql 异常的错误值

我也试过:

List<List<Integer>> ListOfitems=jdbcTemp.queryForList(SQL , Integer.class);
Run Code Online (Sandbox Code Playgroud)

但它仍然抛出相同的异常。

任何帮助表示赞赏。

java postgresql spring jdbctemplate

3
推荐指数
1
解决办法
4387
查看次数

即使在使用正则化器后,LSTM 中的过度拟合

我有一个时间序列预测问题,正在构建一个如下所示的 LSTM:

def create_model():
    model = Sequential()
    model.add(LSTM(50,kernel_regularizer=l2(0.01), recurrent_regularizer=l2(0.01), bias_regularizer=l2(0.01), input_shape=(train_X.shape[1], train_X.shape[2])))
    model.add(Dropout(0.591))
    model.add(Dense(1))
    model.compile(loss='mean_squared_error', optimizer='adam')
    return model
Run Code Online (Sandbox Code Playgroud)

当我在 5 个分割上训练模型时,如下所示:

tss = TimeSeriesSplit(n_splits = 5)
X = data.drop(labels=['target_prediction'], axis=1)
y = data['target_prediction'] 
for train_index, test_index in tss.split(X):
   train_X, test_X = X.iloc[train_index, :].values, X.iloc[test_index,:].values
   train_y, test_y = y.iloc[train_index].values, y.iloc[test_index].values
   model=create_model()
   history = model.fit(train_X, train_y, epochs=10, batch_size=64,validation_data=(test_X, test_y), verbose=0, shuffle=False)
Run Code Online (Sandbox Code Playgroud)

我遇到过拟合问题。附上损失图在此处输入图片说明

我不确定为什么在我的 Keras 模型中使用正则化器时会出现过度拟合。任何帮助表示赞赏。

编辑: 尝试架构

def create_model():
    model = Sequential()
    model.add(LSTM(20, input_shape=(train_X.shape[1], train_X.shape[2])))
    model.add(Dense(1))
    model.compile(loss='mean_squared_error', optimizer='adam')
    return model


def create_model(x,y): …
Run Code Online (Sandbox Code Playgroud)

python deep-learning lstm keras recurrent-neural-network

2
推荐指数
1
解决办法
1996
查看次数

如何按条件乘以数组元素

我有一个数组,想将大于 9 的元素乘以 2 且小于或等于 9 的元素乘以 3。

  List<Integer> list = Arrays.asList(3, 6, 9, 12, 15);
  list.stream().map(number -> number * 3).forEach(System.out::println); 
Run Code Online (Sandbox Code Playgroud)

这个乘以 3 ,其中

list.stream().filter(number -> number>3).map(number -> number * 3).forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)

这一乘法但也过滤器。

我想要 3 ,6 ,9 乘以 3 和 12, 15 乘以 2。

任何帮助表示赞赏。

java arrays java-stream

1
推荐指数
1
解决办法
178
查看次数