在微服务中,需要我们在各个微服务*享session,使用redis来共享session是一个很好的解决方法,redis是运行在内存中,查取速度很快。
1.pom文件中添加依赖
1
2
3
4
5
6
7
8
|
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-redis</artifactid>
</dependency>
<dependency>
<groupid>org.springframework.session</groupid>
<artifactid>spring-session-data-redis</artifactid>
</dependency>
|
2.使用redis的session替换spring的session
1
2
3
4
5
6
7
8
9
10
|
package com.xueqing.demo.sleuthserverhi;
import org.springframework.context.annotation.configuration;
import org.springframework.session.data.redis.config.annotation.web.http.enableredishttpsession;
/**
* 添加redis配置类启用redis代码spring默认session
*/
@configuration
@enableredishttpsession
public class redissessionconfig {
}
|
3.application.properties配置文件中添加redis配置
1
2
|
spring.redis.port= 6379
spring.redis.host=localhost
|
4.启动两个端口以一样的tomcat测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
package com.xueqing.demo.sleuthserverhi;
import java.util.logging.level;
import java.util.logging.logger;
import brave.sampler.sampler;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.cloud.client.loadbalancer.loadbalanced;
import org.springframework.context.annotation.bean;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
import org.springframework.web.client.resttemplate;
import javax.servlet.http.httpservletrequest;
@springbootapplication
@restcontroller
public class sleuthserverhiapplication {
public static void main(string[] args) {
springapplication.run(sleuthserverhiapplication. class , args);
}
private static final logger log = logger.getlogger(sleuthserverhiapplication. class .getname());
@autowired
private resttemplate resttemplate;
@bean
@loadbalanced
public resttemplate getresttemplate(){
return new resttemplate();
}
@requestmapping ( "/hi" )
public string callhome(httpservletrequest request){
log.log(level.info, "calling trace service-hi " );
request.getsession().setattribute( "hi" , "111" );
log.log(level.warning, "加入成功" );
return resttemplate.getforobject( "http://localhost:8989/miya" , string. class );
}
@requestmapping ( "/info" )
public string info(httpservletrequest request){
log.log(level.info, request.getsession().getattribute( "miya" )+ "" );
log.log(level.warning, "获取成功" );
return "i'm service-hi" ;
}
@bean
public sampler defaultsampler() {
return sampler.always_sample;
}
}
package com.xueqing.demo.sleuthservermiya;
import brave.sampler.sampler;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.cloud.client.loadbalancer.loadbalanced;
import org.springframework.context.annotation.bean;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
import org.springframework.web.client.resttemplate;
import javax.servlet.http.httpservletrequest;
import java.util.logging.level;
import java.util.logging.logger;
@springbootapplication
@restcontroller
public class sleuthservermiyaapplication {
public static void main(string[] args) {
springapplication.run(sleuthservermiyaapplication. class , args);
}
private static final logger log = logger.getlogger(sleuthservermiyaapplication. class .getname());
@requestmapping ( "/hi" )
public string home(httpservletrequest request){
log.log(level.info, "hi is being called" );
request.getsession().setattribute( "miya" , "111" );
log.log(level.warning, "加入成功" );
return "hi i'm miya!" ;
}
@requestmapping ( "/miya" )
public string info(httpservletrequest request){
log.log(level.info, "info is being called" );
log.log(level.info, request.getsession().getattribute( "hi" )+ "" );
log.log(level.warning, "获取成功" );
return resttemplate.getforobject( "http://localhost:8988/info" ,string. class );
}
@autowired
private resttemplate resttemplate;
@bean
@loadbalanced
public resttemplate getresttemplate(){
return new resttemplate();
}
@bean
public sampler defaultsampler() {
return sampler.always_sample;
}
}
|
5.注意事项:我用的springcloud版本为f版本需要redis版本为2.8以上 如果不是2.8以上请升级,地址如下
https://github.com/microsoftarchive/redis/releases
总结
以上所述是小编给大家介绍的springcloud实现redis在各个微服务的session共享,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:https://blog.csdn.net/wangxueqing52/article/details/81871389