Broadleaf Commerce Embedded Solr 不能 运行 root 用户

Broadleaf Commerce Embedded Solr cannot run with root user

我通过 java -javaagent:./admin/target/agents/spring-instrument.jar -jar admin/target/admin.jar 在我的 macbook 上成功下载了新的 6.1 broadleaf-commerce 和 运行 我的本地计算机。但是在我的 centos 7 我 运行 sudo java -javaagent:./admin/target/agents/spring-instrument.jar -jar admin/target/admin.jar 有以下错误

2020-10-12 13:20:10.838  INFO 2481 --- [           main] c.b.solr.autoconfigure.SolrServer        : Syncing solr config file: jar:file:/home/mynewuser/seafood-broadleaf/admin/target/admin.jar!/BOOT-INF/lib/broadleaf-boot-starter-solr-2.2.1-GA.jar!/solr/standalone/solrhome/configsets/fulfillment_order/conf/solrconfig.xml to: /tmp/solr-7.7.2/solr-7.7.2/server/solr/configsets/fulfillment_order/conf/solrconfig.xml
*** [WARN] ***  Your Max Processes Limit is currently 62383.
 It should be set to 65000 to avoid operational disruption.
 If you no longer wish to see this warning, set SOLR_ULIMIT_CHECKS to false in your profile or solr.in.sh
WARNING: Starting Solr as the root user is a security risk and not considered best practice. Exiting.
         Please consult the Reference Guide. To override this check, start with argument '-force'
2020-10-12 13:20:11.021 ERROR 2481 --- [           main] c.b.solr.autoconfigure.SolrServer        : Problem starting Solr

这里是solr配置的源代码,我相信这是在编程方式中使用参数-force将配置更改为运行的地方。

package com.community.core.config;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.broadleafcommerce.core.search.service.SearchService;
import org.broadleafcommerce.core.search.service.solr.SolrConfiguration;
import org.broadleafcommerce.core.search.service.solr.SolrSearchServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * 
 * 
 * @author Phillip Verheyden (phillipuniverse)
 */
@Component
public class ApplicationSolrConfiguration {

    @Value("${solr.url.primary}")
    protected String primaryCatalogSolrUrl;
    
    @Value("${solr.url.reindex}")
    protected String reindexCatalogSolrUrl;
    
    @Value("${solr.url.admin}")
    protected String adminCatalogSolrUrl;

    @Bean
    public SolrClient primaryCatalogSolrClient() {
        return new HttpSolrClient.Builder(primaryCatalogSolrUrl).build();
    }
    
    @Bean
    public SolrClient reindexCatalogSolrClient() {
        return new HttpSolrClient.Builder(reindexCatalogSolrUrl).build();
    }
    
    @Bean
    public SolrClient adminCatalogSolrClient() {
        return new HttpSolrClient.Builder(adminCatalogSolrUrl).build();
    }

    @Bean
    public SolrConfiguration blCatalogSolrConfiguration() throws IllegalStateException {
        return new SolrConfiguration(primaryCatalogSolrClient(), reindexCatalogSolrClient(), adminCatalogSolrClient());
    }

    @Bean
    protected SearchService blSearchService() {
        return new SolrSearchServiceImpl();
    }
    
}

首先让我说你最好不要以 root 的身份启动应用程序。如果你在Docker,你可以使用USER命令切换到non-root用户。

Broadleaf 社区中的 Solr 服务器启动是通过 broadleaf-boot-starter-solr 依赖项以编程方式完成的。这是 Solr 的包装器,将其与 Spring 生命周期联系起来。所有真正的魔法都发生在 com.broadleafcommerce.solr.autoconfigure.SolrServer class.

在那class中,你会看到一个startSolr()方法。此方法是向 Solr 添加启动参数的方法。

在您的情况下,您将需要大量复制此方法并使用 cmdLine.addArgument(...) 添加其他参数。示例:

class ForceStartupSolrServer extends SolrServer {

    public ForceStartupSolrServer(SolrProperties props) {
        super(props);
    }

    protected void startSolr() {
        if (!isRunning()) {
            if (!downloadSolrIfApplicable()) {
                throw new IllegalStateException("Could not download or expand Solr, see previous logs for more information");
            }
            stopSolr();
            synchConfig();
            {
                CommandLine cmdLine = new CommandLine(getSolrCommand());
                cmdLine.addArgument("start");
                cmdLine.addArgument("-p");
                cmdLine.addArgument(Integer.toString(props.getPort()));

                // START MODIFICATION
                cmdLine.addArgument("-force");
                // END MODIFICATION

                Executor executor = new DefaultExecutor();
                PumpStreamHandler streamHandler = new PumpStreamHandler(System.out);
                streamHandler.setStopTimeout(1000);
                executor.setStreamHandler(streamHandler);
                try {
                    executor.execute(cmdLine);
                    created = true;
                    checkCoreStatus();
                } catch (IOException e) {
                    LOG.error("Problem starting Solr", e);
                }
            }
        }
    }
}

然后创建一个 @Configuration class 来覆盖 SolrAutoConfiguration 创建的 blAutoSolrServer bean(注意 specific package requirement for org.broadleafoverrides.config):

package org.broadleafoverrides.config;

public class OverrideConfiguration {
    
    @Bean
    public ForceStartupSolrServer blAutoSolrServer(SolrProperties props) {
        return new ForceStartupSolrServer(props);
    }
}