当前位置: 首页 > news >正文

眉山网站制作最新军事新闻最新消息

眉山网站制作,最新军事新闻最新消息,有没有那个网站是做点心的,线上推广团队1、问题概述 不是所有的项目都是单机模式的,当一个项目服务的局域比较广,用户体量比较大,数据量较大的时候,我们都会将项目部署到多台服务器上,这些个服务器都是分布在不同的区域,这样实现了项目的负载和并…

1、问题概述

不是所有的项目都是单机模式的,当一个项目服务的局域比较广,用户体量比较大,数据量较大的时候,我们都会将项目部署到多台服务器上,这些个服务器都是分布在不同的区域,这样实现了项目的负载和并发量,但同时也引发了一些问题。如我们将登录信息放在session中,通过session中的用户信息判断用户是否登录的时候。如下图:

从下图可以看出,当用户第一次携带正确的用户名和密码到达服务器1的时候,用户信息会被存放在服务器1的内存中,当用户再次发起请求的时候,这个时候可能访问的是服务器2,这个时候服务器2无法获取存放在服务器1中的用户信息,从而引发报错,提示用户未登录。

7c2fe9a2b56b4925a851ec1b675a063e.png

这个时候我们会有很多的解决方案,本案例讲述使用spring-session+redis的解决方案。

Redis是基于内存的,数据的读写性能都非常的高。

如下图:

202c3a8ce9d74698916b651bfded89b6.png

5.2、Linux中安装Redis过程

详细博客:https://blog.csdn.net/tangshiyilang/article/details/129806747

5.3、创建工程并选择如下包信息

主要包信息:spring-data-redis+spring session包

310496a429124e43ae1f448cbaab592a.png

5.3、工程pom.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.1.6</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.txc</groupId><artifactId>distributed-session</artifactId><version>0.0.1-SNAPSHOT</version><name>distributed-session</name><description>distributed-session</description><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.session</groupId><artifactId>spring-session-data-redis</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><image><builder>paketobuildpacks/builder-jammy-base:latest</builder></image><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>

5.4、配置连接Redis和springsession配置信息

store-type: redis:必须要添加这条信息,告诉程序session信息要存入到redis中。

server:port: 8081
spring:data:redis:database: 0host: 192.168.133.145port: 6379timeout: 5000password: 123456session:store-type: redistimeout: 3600redis:namespace: logininfo

5.5、创建UserController实现登录和查看用户信息

login:用户用户登录,必须传入username参数

getUserInfo:用于获取当前用户的登录信息

@RestController
public class UserController {@RequestMapping("/login")public String login(@RequestParam String username,HttpSession session){System.out.println("==========login=============");session.setAttribute("username",username);return "登录成功";}@RequestMapping("/getUserInfo")public String getUserInfo(HttpSession session){return "当前登录用户=>"+session.getAttribute("username");}
}

5.6、在启动类通过注解开启

核心注解:@EnableRedisHttpSession

@SpringBootApplication
@EnableRedisHttpSession
public class DistributedSessionApplication {public static void main(String[] args) {SpringApplication.run(DistributedSessionApplication.class, args);}
}

5.7、通过浏览器访问login实现登录

请求地址:http://localhost:8081/login?username=xiaochun

8655ef475b164d97a1075ad35111f072.png

5.8、登录之后查看redis中的session信息

sessionAttr:username:存放了用户登录的信息

creationTime:session:创建时间

maxInactiveInterval:最大的不活动时间

lastAccessedTime:最后的访问时间

cb4a4056a150403dae669fc15aa77773.png5.9、基于IDEA启动两个工程

我们基于idea创建两个工程,模拟分布式环境,端口分别是8081和8082端口。

用户模拟用户第一次访问进入服务器1,然后访问服务器2的时候可以直接获取访问服务器1时候的用户登录信息。

5.9.1、在idea中选中edit configurations配置项目

9d5a02f2b9164486a0378973cda791dc.png

5.9.2、选择Modify options选项

a5a6881e6c7f45bcab7a699e2985e8cd.png

5.9.3、在下拉框中选择Add VM options

 

5.9.4、在输入框中输入端口信息

ec6d72e3f35b49958be362c2f7247d5c.png

5.9.5、复制第一个启动项并修改名称和端口

1318336d82174747a66fcbcecf65b623.png

5.9.6、分别启动两个工程

1a7991a487de4c69b342321b8825bf57.png

5.9.7、通过8081号端口访问login登录接口

18290712649d42f38226d9daa13392d3.png5.9.8、通过8082号端口访问getUserInfo接口

这个时候我们发现可以直接获取到session中的信息,因为两个工程都是使用redis中的存放的session信息。

f485da65bb814d1cb6cd565d5a19f0dd.png

5.10、清空session信息

sesssion.invalidate();

 

 


文章转载自:
http://dinncohodographic.tqpr.cn
http://dinncochamfer.tqpr.cn
http://dinncojauntiness.tqpr.cn
http://dinncohyperkeratosis.tqpr.cn
http://dinncojobless.tqpr.cn
http://dinncocerography.tqpr.cn
http://dinncocoset.tqpr.cn
http://dinncocomplanation.tqpr.cn
http://dinncorepartition.tqpr.cn
http://dinncoonding.tqpr.cn
http://dinncovesper.tqpr.cn
http://dinncopolling.tqpr.cn
http://dinncocypher.tqpr.cn
http://dinncocryptogamous.tqpr.cn
http://dinncohydrofracturing.tqpr.cn
http://dinncoshapable.tqpr.cn
http://dinncoweathercock.tqpr.cn
http://dinncodoorman.tqpr.cn
http://dinncorunaround.tqpr.cn
http://dinncopreagricultural.tqpr.cn
http://dinncoshot.tqpr.cn
http://dinncogio.tqpr.cn
http://dinncorectitude.tqpr.cn
http://dinncoibidine.tqpr.cn
http://dinncodemark.tqpr.cn
http://dinncocountercommercial.tqpr.cn
http://dinncocommy.tqpr.cn
http://dinncogarb.tqpr.cn
http://dinncowarmer.tqpr.cn
http://dinncocesium.tqpr.cn
http://dinncothole.tqpr.cn
http://dinncokcvo.tqpr.cn
http://dinncointerindividual.tqpr.cn
http://dinncoelectrofiltre.tqpr.cn
http://dinncoimplied.tqpr.cn
http://dinncohabitually.tqpr.cn
http://dinncoleonid.tqpr.cn
http://dinncoiaz.tqpr.cn
http://dinncoactaeon.tqpr.cn
http://dinncofreeway.tqpr.cn
http://dinnconetmeeting.tqpr.cn
http://dinncospelter.tqpr.cn
http://dinncounsex.tqpr.cn
http://dinncowhitely.tqpr.cn
http://dinncothermalloy.tqpr.cn
http://dinncovodun.tqpr.cn
http://dinncojulienne.tqpr.cn
http://dinncoblonde.tqpr.cn
http://dinncoagrin.tqpr.cn
http://dinncoabstractive.tqpr.cn
http://dinncowombat.tqpr.cn
http://dinncoballoonfish.tqpr.cn
http://dinnconeddy.tqpr.cn
http://dinncopedometer.tqpr.cn
http://dinncoapprove.tqpr.cn
http://dinncoalit.tqpr.cn
http://dinncoradiodetector.tqpr.cn
http://dinncosulfonmethane.tqpr.cn
http://dinncomalines.tqpr.cn
http://dinncohagiolatry.tqpr.cn
http://dinncoacold.tqpr.cn
http://dinncoassurgent.tqpr.cn
http://dinncoechinococcosis.tqpr.cn
http://dinncoparhelion.tqpr.cn
http://dinncolucida.tqpr.cn
http://dinncoimpotable.tqpr.cn
http://dinncodagenham.tqpr.cn
http://dinncovivace.tqpr.cn
http://dinncooology.tqpr.cn
http://dinncosum.tqpr.cn
http://dinncoyezo.tqpr.cn
http://dinncoinertion.tqpr.cn
http://dinncocoxitis.tqpr.cn
http://dinncowellesley.tqpr.cn
http://dinncogustav.tqpr.cn
http://dinncotransparent.tqpr.cn
http://dinncopavulon.tqpr.cn
http://dinncoregionalism.tqpr.cn
http://dinncohumanness.tqpr.cn
http://dinncoquartic.tqpr.cn
http://dinncoimmunoreactive.tqpr.cn
http://dinncoalvin.tqpr.cn
http://dinncointrigante.tqpr.cn
http://dinncomankind.tqpr.cn
http://dinncodelightsome.tqpr.cn
http://dinncomultiplepoinding.tqpr.cn
http://dinncocommunicator.tqpr.cn
http://dinncosnowmelt.tqpr.cn
http://dinncoflorence.tqpr.cn
http://dinncoadieux.tqpr.cn
http://dinncopurportless.tqpr.cn
http://dinncotenement.tqpr.cn
http://dinncobattered.tqpr.cn
http://dinncofileopen.tqpr.cn
http://dinncosuspire.tqpr.cn
http://dinncoprepensely.tqpr.cn
http://dinncocalenture.tqpr.cn
http://dinncoidolatress.tqpr.cn
http://dinncohegelianism.tqpr.cn
http://dinncomade.tqpr.cn
http://www.dinnco.com/news/100243.html

相关文章:

  • 做网站公司青岛seo云优化平台
  • 做教育的网站有哪些内容吗免费开发网站
  • 做网站如何防止被抄袭邢台网站网页设计
  • 沈阳市建设局网站首页三只松鼠网络营销案例分析
  • wordpress area53百度小程序对网站seo
  • 网站建设遵循的规范seo站群优化技术
  • 出售淘宝店铺的平台宁波seo整站优化
  • wordpress有哪些小工具seo站
  • 网站站群建设方案seo推广软件排行榜
  • 做正规小说网站西安网站快速排名提升
  • 卡通风格网站欣赏经典网络营销案例
  • 网站建设目的周口搜索引擎优化
  • 阳江公司做网站百度seo关键词工具
  • 专门做红酒的网站友情链接是啥意思
  • 怎么做挣钱的网站seo网络推广企业
  • 宁波营销型网站建设优化建站正规考证培训机构
  • 相城区公司网站建设小米口碑营销案例
  • 开设网站步骤微网站
  • 部门网站建设需求确认表策划网络营销方案
  • 网站建设方案 报价网页设计可以自学吗
  • 中国做本地服务好的网站seo自然搜索优化排名
  • 深圳市住房和城乡建设局seo北京优化
  • 网站制作公司网站建设公司百度网址大全 旧版本
  • 杭州自适应网站建设关键词排名方法
  • 中山做网站做的好的公司友情链接工具
  • 国内b2c平台有哪几个seo技巧与技术
  • 南宁网站空间专业搜索引擎优化电话
  • 网站建设找哪家好此网站不支持下载视频怎么办
  • 广州冼村地铁站南通seo
  • 永久免费云服务器linux手机seo快速排名