您现在的位置是:首页 > 文章详情

SpringBoot实现session共享和国际化

日期:2018-06-26点击:389

SpringBoot Session共享

修改pom.xml添加依赖

org.springframework.session spring-session-data-redis

添加配置类 RedisSessionConfig

@Configuration@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 60)//默认是1800秒过期,这里测试修改为60秒public class RedisSessionConfig {}

添加一个控制器类 SessionController 来进行测试

@RestControllerpublic class SessionController { @RequestMapping("/uid") String uid(HttpSession session) { UUID uid = (UUID) session.getAttribute("uid"); if (uid == null) { uid = UUID.randomUUID(); } session.setAttribute("uid", uid); return session.getId(); }}

先访问http://localhost:8083/boot/uid

img_59d66c8b9b25c5539e24fec675779fe4.jpe

然后修改配置文件application.yml

spring: profiles: active: test

重新运行IDEA,test配置文件配置的端口是8085,所以浏览器输入http://localhost:8085/boot/uid

img_3cb5da8df6b2320eda40a0ac3ca56a45.jpe

我们看到两个uid是一样的。

在这里我是使用spring boot redis来实现session共享,你还可以配合使用nginx进行负载均衡,同时共享session。

关于nginx可以参考我的另一篇文章:Nginx详解-服务器集群

spring boot 国际化

在spring boot中实现国际化是很简单的的一件事情。

(1)在resources目录下面,我们新建几个资源文件,messages.properties相当于是默认配置的,当其它配置中找不到记录的时候,最后会再到这个配置文件中去查找。

messages.propertiesmessages_en_US.propertiesmessages_zh_CN.properties

依次在这三个配置文件中添加如下配置值:

msg=我是中国人msg=I'm Chinesemsg=我是中国人

添加完之后,会自动将这几个文件包在一块

img_5130b2b9539567cddbcaf3ad10ce7f6e.jpe

需要注意的是这个命名是有讲究的, messages.properties 部分是固定的,不同语言的话,我们可以在它们中间用_区分。为什么是固定的命名,因为源码是硬编码这样命名的。

(2)新建一个配置文件 LocaleConfig

@Configuration@EnableAutoConfiguration@ComponentScanpublic class LocaleConfig extends WebMvcConfigurerAdapter 

{                 @Bean public LocaleResolver localeResolver()

          {                  SessionLocaleResolver slr = new SessionLocaleResolver(); // 默认语言 

                             slr.setDefaultLocale(Locale.CHINA); return slr;

            } 

                  @Bean public LocaleChangeInterceptor localeChangeInterceptor() 

           {                  LocaleChangeInterceptor lci = new LocaleChangeInterceptor(); // 参数名

                              lci.setParamName("lang"); return lci;

            } 

                   @Override public void addInterceptors(InterceptorRegistry registry) 

           {                   registry.addInterceptor(localeChangeInterceptor());

            }

}

(3)在控制器中,我们添加测试用的方法

// i18n

 @RequestMapping("/") public String i18n() { return "i18n"; } 

@RequestMapping("/changeSessionLanauage") public String changeSessionLanauage(HttpServletRequest request, HttpServletResponse response, String lang)

{ System.out.println(lang); LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request); if("zh".equals(lang)){ localeResolver.setLocale(request, response, new Locale("zh", "CN")); }else if("en".equals(lang)){ localeResolver.setLocale(request, response, new Locale("en", "US")); } return "redirect:/"; }

(4)添加视图来展示,在templates下新建文件i18n.html,通过#可以直接获取国际化配置文件中的配置项的值。

$Title$English(US)简体中文

(5)运行查看效果

img_dff19cc24af615ec823cf434928ef467.gif

欢迎加入Java高级架构学习交流群:375989619

本群提供免费的学习指导 架构资料 以及免费的解答

不懂得问题都可以在本群提出来 之后还会有职业生涯规划以及面试指导 进群修改群备注:开发年限-地区-经验 方便架构师解答问题

免费领取架构师全套视频!!!!!!!!

原文链接:https://yq.aliyun.com/articles/688474
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章