【问题标题】:Run eureka service in a docker container在 docker 容器中运行 eureka 服务
【发布时间】:2018-09-02 08:25:33
【问题描述】:

我想将eureka-server 作为容器运行,并希望稍后让其他微服务注册到该容器。但是我遇到了一些问题,让它作为容器运行并访问它。该应用程序在 STS 中运行没有问题。当我在 STS 中执行它时,我可以使用 localhost:8761 访问 eureka-server

application.java:

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServiceApplication.class, args);
    }
}

application.yml:

server:
  port: 8761

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false

bootstrap.yml:

spring:
  application:
    name: eureka-service

Dockerfile:

FROM java:8 
VOLUME /tmp
ARG JAR_FILE
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","- 
jar","/app.jar"]

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>

    <properties>
        <docker.image.prefix>microservice</docker.image.prefix>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <groupId>com.example</groupId>
    <artifactId>eureka-service</artifactId>
    <version>0.1.0</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.3.6</version>
                <configuration>
                    <repository>${docker.image.prefix}/${project.artifactId} 
 </repository>
                    <buildArgs>

 <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                    <finalName>${project.build.finalName}</finalName>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka- 
server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.M8</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

What am I doing:

  • 运行mvn package &amp;&amp; java -jar target/eureka-service-0.1.0.jar 构建
    罐子。在此期间,弹簧启动应用程序,我必须停止这个 使用 ctrl+c。图像已在 target 文件夹中创建。
  • 使用mvn install dockerfile:build 构建映像。图像已构建。
  • 使用docker run -p 8080:8080 -t microservice/eureka- service运行创建的镜像
  • 这是 docker bash 的响应:

    2018-03-23 15:28:05.618 INFO 1 --- [ main] hello.EurekaServiceApplication : Started EurekaServiceApplication in 17.873 seconds (JVM running for 19.066) 2018-03-23 15:29:05.475 INFO 1 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0ms

通知2018-03-23 15:31:05.476 INFO 1 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 1ms 大约每分钟由 bash 返回一次。 如果我尝试在浏览器中使用192.168.99.100:8080 调用eureka-server,它会说我无法访问此页面。 当我使用 ctrl+c 结束应用程序时,我可以查看所有正在运行的容器,并且 bash 告诉我容器 microservice/eureka-service 仍在运行。仍然无法访问它。

【问题讨论】:

  • 如果我想将 eureka 服务的多个副本作为容器运行以实现高可用性怎么办?有文档解释吗?

标签: java spring docker netflix-eureka netflix


【解决方案1】:
server:
  port: 8761

你的 application.yml 中有这个。这意味着应用程序在端口 8761 而不是 8080 上运行

你应该试试docker run -p 8761:8761 -t microservice/eureka-service

【讨论】:

    【解决方案2】:

    因为这得到了一些意见: port forwarding 也是一个选项。 由于端口是:

    server:    
        port:8761
    

    您也可以转发到port 8080,这是一个非常常见的教程和讲座端口:

    docker run -p 8761:8080 -t microservice/eureka-service
    

    【讨论】:

      猜你喜欢
      • 2019-07-24
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多