springboot项目练习五 整合redis-页面静态化

版权声明: https://blog.csdn.net/Master_chaoAndQi/article/details/86027768

1 静态化:根据请求链接生成html文件完成访问页面的静态化

2 静态化借用nosql数据库redis完成文件存储判断

  1. 用户发起请求,接收用户请求,判断是否已经生成html静态文件,有则直接将生成的HTML文件地址return返回。
  2. 未生成html文件,通过Http发起网络请求,生成html文件,将文件名称以key-value的方式存储进redis缓存中。
  •  第一步项目中添加redis的依赖
  • 第二步项目中配置redis的端口号访问密码等配置文件
  • 编写测试类
<!-- redis -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
spring.redis.database=0 
spring.redis.host=192.168.34.3 //redis服务地址
spring.redis.port=6379 // 端口号
spring.redis.password=123456 //密码 没有配置的可以省略
spring.redis.timeout=3000 // 连接超时
spring.redis.pool.max-idle=200
spring.redis.pool.min-idle=200
spring.redis.pool.max-active=2000
spring.redis.pool.max-wait=1000

修改newController.java类增加如下方法 

    @Autowired
	private StringRedisTemplate  stringRedisTemplate; 

    @RequestMapping("redis")//测试
	@ResponseBody
	public String testRedis(){
		stringRedisTemplate.opsForValue().set("test", "zhangsan");//存储key为test value为张三的值
		String string = stringRedisTemplate.opsForValue().get("test"); // 获取该值
		return string;
		
	}

springboot 官网整合redis的链接地址,很多配置信息可以从官网上找到 

https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-connecting-to-redis

3 文件静态化

  • 编写前台jq脚本发起请求
  • 接收用户请求生成html文件
  • 以新闻的id为key将数据存储在缓存数据库redis中。
function opterate(rowData){
	var str ="";
	str +="<a href='javascript:void(0)' onclick='del("+rowData.id+")'>删除</a>";
	str += "&nbsp;&nbsp;";
	str += "<a href='javascript:void(0)' onclick=showNews('"+rowData.id+"','"+rowData.url_3w+"')>查看</a>";
	return  str;
}
function showNews(id,url){
	var newUrl ="/news/getNews/"+id; 
	$.post(newUrl,{url:url},function(data){
		if(data.code==200){ // 接收服务端返回的html文件名称
			window.location.href=data.data+".html";// 请求对应的地址
		}
	});
}

配置HTML生成目录在application.properties中添加文件路径

html.rootDir = F:/springboot/springboot_solr/src/main/webapp/

在newController.java中获取html生成的根路径新增生成html文件的方法和请求

    @Value("${html.rootDir}")
	private String htmlDir; //从配置文件中获取配置的html文件根路径

    @RequestMapping("getNews/{id}")
	@ResponseBody
	public ResultData getNews(@PathVariable("id") String  id,@RequestParam("url") String url){
		/**
		 * 1 判断是否已经存在html文件
		 */
		String htmlName = stringRedisTemplate.opsForValue().get("news"+id);
		if(!StringUtils.isEmpty(htmlName)){ //已经存在直接return
			return new ResultData("200",htmlName,""); //将文件名称作为data数据返回 前台js接收到路径地址后,通过window.location.href="xxxx"+".html"完成文件的访问
		}else{ // 不存在生成在项目目录下生成html文件
			gcreateHtmlFile(id,url);
			stringRedisTemplate.opsForValue().set("news"+id, id);
		}
		
		return new ResultData("200",id,"");
		
	}

    /*
    生成html文件
    通过openConnection 方法发起请求获取inputStream流 将流指向输出文件
    这里使用org.apache.commons.io.FileUtils 工具类完成输入流向输出流文件的拷贝生成html文件

    */
	public void gcreateHtmlFile(String id, String url) {
		// 通过配置文件获取存放静态文件的文件夹
		try {
			String path = htmlDir;
			File rootDir = new File(path);
			if (!rootDir.isDirectory()) {
				rootDir.mkdirs();
			}
			URL newUrl = new URL(url);
			URLConnection openConnection = newUrl.openConnection();
			InputStream in = openConnection.getInputStream();
			FileUtils.copyInputStreamToFile(in, new File(path + "/" + id + ".html"));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

在项目目录下生成的html文件如下

扫描二维码关注公众号,回复: 4886811 查看本文章

 

简单版的静态化页面就完成了。其他的部分慢慢增加

增加采集功能,采取网易新闻,增加数据量 

猜你喜欢

转载自blog.csdn.net/Master_chaoAndQi/article/details/86027768