[英]Run eureka service in a docker container
我想將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 && java -jar target/eureka-service-0.1.0.jar
來構建 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
in 17.873 seconds (JVM running for 19.066)
hello.EurekaServiceApplication : Started
EurekaServiceApplication
in 17.873 seconds (JVM running for 19.066)
2018-03-23 15:29:05.475 INFO 1 --- [a-EvictionTimer]
cneregistry.AbstractInstanceRegistry : Running the evict task with
compensationTime 0ms
cneregistry.AbstractInstanceRegistry : Running the evict task with
通知2018-03-23 15:31:05.476 INFO 1 --- [a-EvictionTimer] cneregistry.AbstractInstanceRegistry : Running the evict task with compensationTime 1ms
每分鍾返回bash一次。 如果我嘗試在瀏覽器中使用192.168.99.100:8080
調用eureka-server
,則說我無法訪問此頁面。 當我使用ctrl + c結束應用程序時,我可以查看所有正在運行的容器,bash告訴我容器microservice/eureka-service
仍在運行。 仍然無法訪問它。
server:
port: 8761
你在application.yml中有這個。 這意味着應用程序在端口8761
上運行而不是8080
你應該嘗試使用docker run -p 8761:8761 -t microservice/eureka-service
由於這是一些觀點: port forwarding
也是一種選擇。 由於港口是:
server:
port:8761
你也可以將它轉發到port 8080
,這是一個非常常見的教程和講座端口:
docker run -p 8761:8080 -t microservice/eureka-service
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.