Broadleaf Commerce Embedded Solr 无法在 root 用户下运行。

huangapple go评论142阅读模式
英文:

Broadleaf Commerce Embedded Solr cannot run with root user

问题

以下是翻译好的部分:

我下载了一个新的 6.1 版本的 broadleaf-commerce,并且在我的本地机器上通过以下命令成功运行(在我的 macbook 上):java -javaagent:./admin/target/agents/spring-instrument.jar -jar admin/target/admin.jar。但是在我的 centos 7 上,我运行以下命令时出现了错误:

2020-10-12 13:20:10.838  INFO 2481 --- [           main] c.b.solr.autoconfigure.SolrServer        : 正在同步 solr 配置文件: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 至:/tmp/solr-7.7.2/solr-7.7.2/server/solr/configsets/fulfillment_order/conf/solrconfig.xml
*** [警告] ***  您的最大进程限制当前为 62383。
 应将其设置为 65000 以避免操作中断。
 如果您不再希望看到此警告,请在您的配置文件或 solr.in.sh 中将 SOLR_ULIMIT_CHECKS 设置为 false。
警告:以 root 用户身份启动 Solr 存在安全风险,不被视为最佳实践。正在退出。
         请查阅参考指南。要覆盖此检查,请使用参数 -force 启动
2020-10-12 13:20:11.021 ERROR 2481 --- [           main] c.b.solr.autoconfigure.SolrServer        : 启动 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;

/**
 * 
 * 
 * 作者: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();
    }
    
}
英文:

I download a fresh 6.1 broadleaf-commerce and run my local machine via java -javaagent:./admin/target/agents/spring-instrument.jar -jar admin/target/admin.jar successfully on mine macbook. But in my centos 7 I run sudo java -javaagent:./admin/target/agents/spring-instrument.jar -jar admin/target/admin.jar with following error

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

Here is the source code of solr configuration, I believe it is the place to change the configuration to run with the argument -force in programming way.

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();
    }
    
}

答案1

得分: 1

这里有一个先行声明,建议您最好不要以“root”身份启动应用程序。如果您在Docker中,可以使用USER命令切换到非root用户。

在Broadleaf Community中,Solr服务器的启动是通过broadleaf-boot-starter-solr依赖在程序中完成的。这是将Solr与Spring生命周期关联起来的包装器。所有真正的魔法发生在com.broadleafcommerce.solr.autoconfigure.SolrServer类中。

在那个类中,您会看到一个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类来覆盖由SolrAutoConfiguration创建的blAutoSolrServer bean(请注意对于org.broadleafoverrides.config的特定包要求):

package org.broadleafoverrides.config;

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

Let me preface this by saying you would be better off simply not starting the application as root. If you are in Docker, you can use the USER command to switch to a non-root user.

The Solr server startup in Broadleaf Community is done programmatically via the broadleaf-boot-starter-solr dependency. This is the wrapper around Solr that ties it to the Spring lifecycle. All of the real magic happens in the com.broadleafcommerce.solr.autoconfigure.SolrServer class.

In that class, you will see a startSolr() method. This method is what adds startup arguments to Solr.

In your case, you will need to mostly copy this method wholesale and use cmdLine.addArgument(...) to add additional arguments. Example:

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);
                }
            }
        }
    }
}

Then create an @Configuration class to override the blAutoSolrServer bean created by SolrAutoConfiguration (note the specific package requirement for org.broadleafoverrides.config):

package org.broadleafoverrides.config;

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

huangapple
  • 本文由 发表于 2020年10月12日 13:21:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64312094.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定