Springboot使用插件sitemapgen4j自动生成最大限度50000个url的sitemap文件,并自动创建sitemap的index索引型文件

31 篇文章 0 订阅
1 篇文章 0 订阅

Index

如果超出了 5 万条需要写入另外一个 sitemap 当中,这个功能 sitemapgen4j 已经替我们实现了,无需担心。如果超出,生成的文件就像是这样的:

- q1.xml

- q2.xml

- q3.xml

- q4.xml

这样的话,在站长平台这里,如果新增了文件就要新增一条sitemap 网址记录,很麻烦。

 

但是各个搜索引擎的站长平台已经想到可以提交索引文件的方法,而这个插件也是可以自动生成索引文件的。

 

我们把每次生成的 sitemap 文件的地址,添加到一个 sitemap 索引文件当中去,这样,我们只需要向平台提交一个 sitemap 索引文件的地址即可。

 

生成 sitemap 文件

首先引入 sitemapgen4j 依赖

<dependency>
    <groupId>com.github.dfabulich</groupId>
    <artifactId>sitemapgen4j</artifactId>
    <version>1.1.1</version>
</dependency>


创建java生成sitemap的方法

public void generateSitemapForLimit50000() {
    String tempPath = "/你存放sitemap文件的路径";
    File file = new File(tempPath);
    if (!file.exists()) {
        file.mkdirs();
    }
    String domain = "https://www.你的主域名.com";
    try {
        WebSitemapGenerator g1 = WebSitemapGenerator.builder(domain, file)
                .fileNamePrefix("xxxx").build();
        Date date = new Date();
        for (int i = 1; i < 21; i++) {
            WebSitemapUrl url = new WebSitemapUrl.Options(domain + "/xxxx/" + i ).lastMod(date).build();
            g1.addUrl(url);
        }

 
        List<String> fileNames = new ArrayList<>();
 
        // 生成 sitemap 文件
        List<File> articleFiles = g1.write();
        articleFiles.forEach(e -> fileNames.add(e.getName()));
 
        // 构造 sitemap_index 生成器
        W3CDateFormat dateFormat = new W3CDateFormat(W3CDateFormat.Pattern.DAY);
        SitemapIndexGenerator sitemapIndexGenerator = new SitemapIndexGenerator
                .Options(domain, new File(tempPath + "sitemap_index.xml"))
                .dateFormat(dateFormat)
                .autoValidate(true)
                .build();
 
        fileNames.forEach(e -> {
            try {
                // 组装 sitemap 文件 URL 地址
                sitemapIndexGenerator.addUrl(tempPath + e);
            } catch (MalformedURLException e1) {
                e1.printStackTrace();
            }
        });
 
        // 生成 sitemap_index 文件
        sitemapIndexGenerator.write();
 
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
}

生成成功后,把sitemap文件和sitemap的索引index文件都存放到服务器上,并配置springboot的yml如下:(让域名的根路径能访问到sitemap文件)

spring: 

    resources:
        static-locations: classpath:templates/,classpath:static/,file:/xxx/xx/x/你在服务器上存放sitemap的路径(这样域名的根路径就可以直接访问到sitemap文件了)

或者如果你想分担主服务器的带宽和硬盘空间,可以设置代理,把sitemap文件放在另外一个服务器上(访问链接的时候也还是可以反向代理到另外一个服务器上的)

 

SpringBoot 实现反向代理

引入依赖(maven)

<dependency>
        <groupId>org.mitre.dsmiley.httpproxy</groupId>    
        <artifactId>smiley-http-proxy-servlet</artifactId>
        <version>1.7</version>  
</dependency>
        <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>18.0</version>
</dependency>

在yml配置一下:

# 自定义代理相关配置
# 代理的本地路由
proxy
    servlet_url: /sitemap/*
# 要代理的地址
    proxt.target_url: http://另外一个服务器的ip地址(域名也是可以的)

此处的另外一个服务器就是作为静态资源存取的作用,所以不用在另外一个服务器上启动你的springboot项目,用ip地址然后加上/sitemap/sitemap_index.xml即可访问,但是:需要在另外一个服务器上安装好Apache,然后再Apache的httpd.conf文件中加入

DocumentRoot "/存放sitemap的第一级/存放sitemap的第二级路径(这个随便你的)"

然后保存,重启Apache服务器,然后你在浏览器输入另外一个服务器的ip地址,后面加上/sitemap/sitemap_index.xml即可访问你的sitemap索引文件了(就是用来分担主服务器的压力的,这一步随便你搞不搞)

然后编写声明proxy的servlet,并对其进行配置即可(Java方法):

package 你的包路径;
 
 
import com.google.common.collect.ImmutableMap;
import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
 
import javax.servlet.Servlet;
import java.util.Map;
 
/**
 * @Description: 实现代理配置
 * @Parameters:
 * @Return:
 * @CreateDate: 2012/10/08 9:52
 * @Version: V1.00
 * @Author: 一心精通Java的靓仔程序员
 */
@Configuration
public class MyProxyServletConfiguration {
 
    // 读取yml配置文件中路由设置
    @Value("${proxy.servlet_url}")
    private String servlet_url;
    // 读取yml配置中代理目标地址
    @Value("${proxy.target_url}")
    private String target_url;
 
 
 
    @Bean
    public Servlet createProxyServlet(){
        // 创建新的ProxyServlet
        return new ProxyServlet();
    }
    @Bean
    public ServletRegistrationBean proxyServletRegistration(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(createProxyServlet(), servlet_url);
        //设置网址以及参数
        Map<String, String> params = ImmutableMap.of(
                "targetUri", target_url,
                "log", "true");
        registrationBean.setInitParameters(params);
        return registrationBean;
    }
}

重启springboot的项目,自动注入这个conf即可。

访问https://www.你的主域名.com/sitemap/sitemap_index.xml请求(测试:你不要把sitemap_index.xml放到主服务器中,放到另一个服务器中,如果能出现此文件即代表反向代理成功!可以这样分担主服务器的爬虫带宽和磁盘的压力)

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值