博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot+redis实现session共享
阅读量:4203 次
发布时间:2019-05-26

本文共 3969 字,大约阅读时间需要 13 分钟。

配置多数据源

com.sto.stock.datasource.DataSourceConfig

@Primary注解只能加在一个数据源上

常用注解

  • @ConfigurationProperties

@ConfigurationProperties(“spring.datasource.druid.one”)

@ConfigurationProperties(prefix=“spring.rabbitmq”)

  • 解决myBatis下 不能嵌套jar文件的问题

VFS.addImplClass(SpringBootVFS.class);

  • log4j.properties

log4j.appender.file.File=/app/server/logs/sto-service-stock/log.log

入口类运用CountDownLatch

@SpringBootApplication@DubboComponentScan(basePackages = "com.sto.stock.service.impl")public class StockApplication {
private static CountDownLatch latch = new CountDownLatch(1); public static void main(String[] args) {
SpringApplication.run(StockApplication.class, args); try {
latch.await(); } catch (InterruptedException e) {
e.printStackTrace(); } }}

CountDownLatch设置等待线程数为1,且主方法没有调用countDown(),故使得主线程永远阻塞

  • 原因解释

因为POM文件中没有加spring-boot-starter-web,运行入口类,程序出错【没有提供Tomcat容器】,主线程退出,就无法提供dubbo服务,所以要加CountDownLatch,阻塞主线程。

而,spring-boot-starter-web内嵌Tomcat容器,主线程可以正常跑【Spring Boot支持容器的自动配置,默认是Tomcat】

  • 采取措施
  1. 运用CountDownLatch使得主线程永远阻塞
  2. POM文件中添加spring-boot-starter-web【支持全栈式Web开发,包括Tomcat和spring-webmvc。】

oracle hint

存储在数据库中数据的分布情况开发人员或管理员比Oracle优化器更加的清楚,在优化器不能作出最有查询路径选择的情况下,使用HINT(提示)人为的固定查询路径,一定程度能生成更优的执行计划。

用到别名时,/+INDEX(TABLE INDEX_NAME)/中的TABLE一定是别名,否则不走执行强制索引。

select /*+INDEX(t IDX_SEL_ADD_DATE)*/ t.own_sys, count(1)   from std_entp_login t  where t.add_date >                to_date('2010-12-28 00:00:00', 'yyyy-mm-dd hh24:mi:ss')            and t.add_date <                to_date('2010-12-28 23:59:59', 'yyyy-mm-dd hh24:mi:ss')  group by t.own_sys
  • 模糊查询
where cusname like '%' || #{cusname} || '%' or cuscode like '%' || #{cusname} || '%'

共享session

package com.fuzzy.search;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.web.support.SpringBootServletInitializer;import org.springframework.context.annotation.Bean;import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;import org.springframework.session.web.http.CookieHttpSessionStrategy;import org.springframework.session.web.http.DefaultCookieSerializer;@SpringBootApplication@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 21600, redisNamespace = "fuzzy-search-web")public class FuzzySearchApplication extends SpringBootServletInitializer {
public static void main(String[] args){
SpringApplication.run(FuzzySearchApplication.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder){
return builder.sources(FuzzySearchApplication.class); } @Bean public CookieHttpSessionStrategy cookieHttpSessionStrategy(){
CookieHttpSessionStrategy strategy=new CookieHttpSessionStrategy(); DefaultCookieSerializer cookieSerializer=new DefaultCookieSerializer(); cookieSerializer.setCookieName("fuzzy-search-web");//cookies名称 cookieSerializer.setCookieMaxAge(21600);//过期时间(秒) strategy.setCookieSerializer(cookieSerializer); return strategy; }}

集群和分布式部署,需要考虑session共享@EnableRedisHttpSession,将HttpSession存储在Redis中

@RequestMapping("/execute")    public ModelAndView execute(HttpServletRequest request){
HttpSession session=request.getSession(); session.setAttribute("hello","world"); ModelAndView view=new ModelAndView("fuzzysearch"); DateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Date date=new Date(); Calendar c=Calendar.getInstance(); c.setTime(date); c.add(Calendar.DAY_OF_MONTH, -3); //当前天数-3 view.addObject("start", sdf.format(c.getTime())); view.addObject("end", sdf.format(c.getTime())); view.addObject("timestamp", System.currentTimeMillis()); helloBean.executeProcess("sayBean"); System.out.println(session.getAttribute("hello")); return view; }

在这里插入图片描述

相关文档

转载地址:http://fnvli.baihongyu.com/

你可能感兴趣的文章