连接拒绝尝试连接 spring docker 中的引导应用程序

Connection refused trying to connect spring boot application in docker

我尝试 运行 spring 在 Docker 中启动应用程序,圈出 CI。请检查我的 .circleci/config.yml 文件的样子:

version: 2.1

jobs:

  test:
    docker:
      - image: maven:3.8.5-openjdk-17-slim

    environment:
      CHROMEDRIVER_VERSION: 100.0.4896.60
      CHROMEDRIVER_DIR: /tmp/chromedriver/
      WIKI_ENV: CIRCLE_CI

    steps:
      - checkout
      - run:
          name: "Install gnupg2, wget, xvfb and unzip"
          command: |
            apt-get update
            apt-get install -y gnupg2
            apt-get install -y wget xvfb unzip
      - run:
          name: "Install Chrome Browser"
          command: |
            wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
            echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list
            apt-get update -y
            apt-get install -y google-chrome-stable
      - run:
          name: "Setup chromedriver"
          command: |
            mkdir -p $CHROMEDRIVER_DIR
            wget -q --continue -P $CHROMEDRIVER_DIR "http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip"
            unzip $CHROMEDRIVER_DIR/chromedriver* -d $CHROMEDRIVER_DIR
            export PATH=$CHROMEDRIVER_DIR:$PATH
      - run:
          name: "Build jar"
          command: mvn clean package spring-boot:repackage
      - run:
          name: "Start"
          command: mvn spring-boot:start
      - run:
          name: "Check"
          command: curl http://localhost:8080

      - store_test_results:
          path: target/surefire-reports/junitreports

      - store_artifacts:
          path: target/surefire-reports/junitreports

      - store_artifacts:
          path: target/screenshots

workflows:
  default:
    jobs:
      - test

管道在“检查”步骤失败并出现 curl: (7) Failed to connect to localhost port 8080: Connection refused 错误。

这是“开始”步骤的输出:

[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for vznd:selenium:jar:1.0
[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.testng:testng:jar -> duplicate declaration of version ${testng.version} @ line 54, column 21
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] 
[INFO] ---------------------------< vznd:selenium >----------------------------
[INFO] Building selenium 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.6.3:start (default-cli) @ selenium ---
[INFO] Attaching agents: []
18:36:09.878 [Thread-0] DEBUG org.springframework.boot.devtools.restart.classloader.RestartClassLoader - Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader@2c5fa29b

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.3)

2022-05-05 18:36:10.165  INFO 4520 --- [  restartedMain] vznd.selenium.SeleniumApp                : Starting SeleniumApp using Java 17.0.2 on aeadb1d9da7e with PID 4520 (/root/project/target/classes started by root in /root/project)
2022-05-05 18:36:10.166  INFO 4520 --- [  restartedMain] vznd.selenium.SeleniumApp                : No active profile set, falling back to default profiles: default
2022-05-05 18:36:10.239  INFO 4520 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2022-05-05 18:36:10.239  INFO 4520 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2022-05-05 18:36:11.021  INFO 4520 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-05-05 18:36:11.034  INFO 4520 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-05-05 18:36:11.035  INFO 4520 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-05-05 18:36:11.086  INFO 4520 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-05-05 18:36:11.086  INFO 4520 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 846 ms
2022-05-05 18:36:11.320  INFO 4520 --- [  restartedMain] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: class path resource [static/index.html]
2022-05-05 18:36:11.418  INFO 4520 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2022-05-05 18:36:11.459  INFO 4520 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-05-05 18:36:11.468  INFO 4520 --- [  restartedMain] vznd.selenium.SeleniumApp                : Started SeleniumApp in 1.58 seconds (JVM running for 2.136)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.129 s
[INFO] Finished at: 2022-05-05T18:36:11Z
[INFO] ------------------------------------------------------------------------

这是我的 pom.xml:

<?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>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>vznd</groupId>
    <artifactId>selenium</artifactId>
    <version>1.0</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <maven-surefire-plugin.version>3.0.0-M6</maven-surefire-plugin.version>
        <testng.version>7.5</testng.version>
        <selenium-java.version>3.141.59</selenium-java.version>
        <commons-io.version>2.11.0</commons-io.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>${testng.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>${testng.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium-java.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons-io.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin.version}</version>
                <configuration>
                    <systemPropertyVariables>
                        <webdriver.chrome.driver>/tmp/chromedriver/chromedriver</webdriver.chrome.driver>
                    </systemPropertyVariables>
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

当我 运行 在本地执行此命令时 - 它有效。当它在 CI 上执行时 - 它失败了。我试图解决的问题:

  1. 运行 java -jar target/selenium.jar & “开始”步骤上的命令
  2. 运行 mvn spring-boot:run & 命令而不是“Build jar”和“Start”步骤
  3. 运行 nohup mvn spring-boot:run & 命令而不是“Build jar”和“Start”步骤
  4. 在“开始”步骤后添加 sleep 120 步骤

你能告诉我我做错了什么吗?

很确定您在服务器启动之前就开始卷曲了,所以请尝试等待服务器启动

- run:
    name: wait
    command: sleep 10

移动到 gitlab CI,问题不再是实际的了。