0.环境准备:
centos7.2,nginx-1.10.1,apache-tomcat-8.5.16 *2
1.nginx安装;
可以直接yum install nginx ,也可以wget http://nginx.org/download/nginx-1.13.12.tar.gz
tar zxvf nginx.tar.gz;
解压后如图;
./configure&&make&&make install,安装nginx,成功后如图:
个人习惯拷贝sbin下的nginx可执行文件到外面 方便执行:cp sbin/nginx ./startnginx
然后写个脚本用来关闭nginx:
很简单的几句话,分别是ps -ef 查进程,awk截取进程id,然后kill掉
2.安装tomcat
这边省略安装步骤,在tomcat的webapps/ROOT下更改index.jsp ,给2个tomcat不同标识,比如tomcat1,tocmat2这样,然后启动两个tomcat(自行更改端口等)
3.使用nginx做反向代理
nginx做单个tomcat代理:
上面配置即,监听80端口,当访问路径匹配到pro,则代理到本机9999端口的pro项目;
nginx做多台tomcat代理
这边需要使
用一个http节点下的属性叫upstream,如图:
然后我们就可以将访问请求代理到这个upstream,
当请求到达nginx ,则会分发到upstream,从而实现负载均衡。
这么看来nginx的tomcat负载实现要比apache容易的多,但是这边集群重要的问题:session共享没有解决
4.springboot,redis实现session共享
加入依赖
在application.properties中加入redis连接配置:
spring.redis.hostName=localhost
spring.redis.port:6379
spring.redis.password:bc3b50ed11cf496027ace0bda20b53e1694639fca14d405b44503e57e52ce397
spring.redis.pool.max-active: 8
spring.redis.pool.max-wait: -1
spring.redis.pool.max-idle: 8
spring.redis.pool.min-idle: 0
新建RedisConfig.java,创建redistemplate实例
package com.zz.utils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
@EnableAutoConfiguration
@EnableRedisHttpSession
public class RedisConfig {
private static Logger logger = Logger.getLogger(RedisConfig.class);
@Value("${spring.redis.hostName}")
private String host;
@Value("${spring.redis.password}")
private String password;
@Bean
@ConfigurationProperties(prefix = "spring.redis.pool")
public JedisPoolConfig getRedisConfig() {
JedisPoolConfig config = new JedisPoolConfig(); return config; }
@Bean
public JedisConnectionFactory getConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setHostName(host);
factory.setPassword(DES3.decrypt(password));
factory.setUsePool(true);
JedisPoolConfig config = getRedisConfig();
factory.setPoolConfig(config);
logger.info("JedisConnectionFactory bean init success.");
return factory; }
@Bean
public RedisTemplate getRedisTemplate() {
JedisConnectionFactory factory = getConnectionFactory(); logger.info(this.host+","+factory.getHostName()+","+factory.getDatabase()); logger.info(this.password+","+factory.getPassword()); logger.info(factory.getPoolConfig().getMaxIdle()); RedisTemplate template = new StringRedisTemplate(getConnectionFactory()); return template; }}
这边注解@EnableRedisHttpSession用来开启spring session支持
然后我们来验证session共享是否生效:
偷懒不想写页面,在controller里写2个方法,一个setsession,一个get:
@ResponseBody
@RequestMapping("test")
public String test(HttpServletRequest req) {
req.getSession().setAttribute("123123","asdasd"); return "success";
}
@ResponseBody
@RequestMapping("test2")
public String test2( HttpServletRequest req) {
String s = (String) req.getSession().getAttribute("123123");
System.out.println("-------------------------"+s); return s;
}
这样我们只要访问test,然后访问test2看日志便可知道session共享是否实现,运行如图所示,2个服务日志轮流打印: