京淘项目day15
码农天地 -1. Redis集群说明1.1 分片/哨兵有哪些缺点
1.分片缺点: 分片的主要功能是实现内存的扩容的,但是没有高可用的效果.
2.哨兵缺点: 数据没有扩容,哨兵本身没有高可用机制
需求: 既可以实现内存的扩容同时实现高可用机制(不用第三方).
1.首先关闭所有的Redis服务器
2.检查配置文件编辑是否正确.
3.删除多余的配置文件
4.重启redis服务器
5.搭建redis集群
redis-cli --cluster create --cluster-replicas 1 192.168.126.129:7000 192.168.126.129:7001 192.168.126.129:7002 192.168.126.129:7003 192.168.126.129:7004 192.168.126.129:7005
1.3 集群入门案例@Test
public void testCluster(){
Set<HostAndPort> sets = new HashSet<>();
sets.add(new HostAndPort("192.168.126.129", 7000));
sets.add(new HostAndPort("192.168.126.129", 7001));
sets.add(new HostAndPort("192.168.126.129", 7002));
sets.add(new HostAndPort("192.168.126.129", 7003));
sets.add(new HostAndPort("192.168.126.129", 7004));
sets.add(new HostAndPort("192.168.126.129", 7005));
JedisCluster jedisCluster = new JedisCluster(sets);
jedisCluster.set("jedis", "集群赋值");
System.out.println(jedisCluster.get("jedis"));
}
1.4 面试题1.redis集群中一共可以存储16384个KEY?? 不对
答:16384只是槽位的数量只负责规划这个数据归谁管理的问题.至于数据如何存储,是由redis内存决定的.
2.Redis集群中最多可以有多少台主机? 16384主机.
3.Redis中如果遇到多线程操作,是否有线程安全性问题 ? 没有
因为:redis服务器是单进程单线程操作.每次操作都是由一个线程执行,所以不会有线程安全性问题.
4.Redis如何实现内存数据的优化? LRU/LFU/随机算法/TTL
说明:将redis集群的节点写入pro配置文件中
# 配置单台redis服务器
#redis.host=192.168.126.129
#redis.port=6379
##配置redis分片
#redis.nodes=192.168.126.129:6379,192.168.126.129:6380,192.168.126.129:6381
# redis集群配置
redis.nodes=192.168.126.129:7000,192.168.126.129:7001,192.168.126.129:7002,192.168.126.129:7003,192.168.126.129:7004,192.168.126.129:7005
1.5.2 编辑配置类package com.jt.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import java.util.HashSet;
import java.util.Set;
@Configuration //标识我是配置类
@PropertySource("classpath:/properties/redis.properties")
public class RedisConfig {
//实现redis集群操作
@Value("${redis.nodes}")
private String nodes; //node,node,node
@Bean
public JedisCluster jedisCluster(){
Set<HostAndPort> nodeSet = new HashSet<>();
String[] nodeArray = nodes.split(",");
for (String node : nodeArray){ //host:port
String host = node.split(":")[0];
int port = Integer.parseInt(node.split(":")[1]);
nodeSet.add(new HostAndPort(host,port));
}
return new JedisCluster(nodeSet);
}
/* *//**
* SpringBoot整合Redis分片,实质:ShardedJedis对象,交给容器管理
*//*
@Value("${redis.nodes}")
private String nodes; //node,node,node
@Bean
public ShardedJedis shardedJedis(){
List<JedisShardInfo> shards = new ArrayList<>();
String[] nodeArray = nodes.split(",");
for(String node : nodeArray){ //node=ip:port
String host = node.split(":")[0];
int port = Integer.parseInt(node.split(":")[1]);
//准备分片节点信息
JedisShardInfo info = new JedisShardInfo(host,port);
shards.add(info);
}
return new ShardedJedis(shards);
}*/
/* @Value("${redis.host}")
private String host;
@Value("${redis.port}")
private Integer port;
@Bean
public Jedis jedis(){
return new Jedis(host,port);
}*/
}
1.5.3 编辑AOP配置在AOP中注入Redis缓存对象
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!--项目坐标-->
<modelVersion>4.0.0</modelVersion>
<artifactId>jt-web</artifactId>
<!--打war包-->
<packaging>war</packaging>
<!--父级工程-->
<parent>
<artifactId>jt2007</artifactId>
<groupId>com.jt</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<!--添加依赖项-->
<dependencies>
<dependency>
<groupId>com.jt</groupId>
<artifactId>jt-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<!--4.添加maven插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2.3 导入静态资源文件说明:将课前资料中的文件src目录导入到jt-web中
说明:jt-web服务器启动时会加载数据源的自动化配置,但是web服务器没有配置数据源,所以报错.
启动类上添加数据源启动.
需求:要求用户通过http://www.jt.com 访问localhost:8092服务器.
2.3.1 修改host文件2.3.2 修改Nginx配置文件#配置前台服务器
server {
listen 80;
server_name www.jt.com;
location / {
proxy_pass http://localhost:8092;
}
}
修改之后,重启nginx服务器
键入地址:
修改完成之后,先清空缓存之后重启浏览器
2.5 开启后缀类型匹配说明: 由于京东商城的商品展现时通过
url:https://item.jd.com/100213774...
URL地址小结:
1.http://www.jt.com/index 该请求会被Controller进行拦截
2.http://www.jt.com/index.html 该请求默认条件下表示获取资源文件,不会被拦截
一般条件下:Controller只拦截前缀类型的请求. 如果需要拦截后缀类型的请求需要单独配置.
3 登录注册页面的跳转3.1 实现通用的页面跳转url1:http://www.jt.com/user/login.html 跳转页面 login.jsp
url2:http://www.jt.com/user/register.html 跳转页面 register.jsp
需求: 能否利用一个Controller方法.实现通用页面的跳转?
package com.jt.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user")
public class UserController {
/**
* 实现用户登录/注册页面的跳转
* url1: http://www.jt.com/user/register.html
* url2: http://www.jt.com/user/login.html
*/
@RequestMapping("/{moduleName}")
public String module(@PathVariable String moduleName){
return moduleName;
}
}
3.2 伪静态的体现伪静态是相对真实静态来讲的,通常我们为了增强搜索引擎的友好性,都将文章内容生成静态页面,但是有的朋友为了实时的显示一些信息。或者还想运用动态脚本解决一些问题。不能用静态的方式来展示网站内容。但是这就损失了对搜索引擎的友好性。怎么样在两者之间找个中间方法呢,这就产生了伪静态技术。伪静态技术是指展示出来的是以html一类的静态页面形式,但其实是用ASP一类的动态脚本来处理的。
总结:以.html结尾的一种动态页面的形式
作业1.预习什么是跨域?
1.JSONP 2.CORS方式