在Docker容器中运行后,Eureka仪表板返回404。但它在localhost

时间:2019-01-13 19:02:22

标签: spring docker netflix-eureka discovery

Eureka仪表板在Docker容器中运行后返回404。但是它在本地主机上工作正常。我使用了最新的Spring Boot版本2.1.2和Spring云版本Greenwich RC2。我可以在本地看到Eureka仪表板。但是在创建容器映像并成功绑定端口号之后,我看不到eureka仪表板UI。它会抛出“ /错误”和404。

build.gradle

buildscript {
    ext {
        springBootVersion = '2.1.2.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath('se.transmode.gradle:gradle-docker:1.2') 
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'docker'

group = 'gcr.io/sc-cloud-rnd'


if(hasProperty('cmdGroup')) { 
    group = cmdGroup 
}


sourceCompatibility = '1.8'

project.version=getversion()

def getversion() {
    def p = new Properties()
        file("app_version.properties").withInputStream { p.load(it) }
        return p.getProperty("major")+"."+p.getProperty("minor")+"."+p.getProperty("release");
}


jar {
    baseName = 'discovery'
     gradle.taskGraph.whenReady{ taskGraph ->        
        manifest {
            attributes(
     "Implementation-Date": new java.util.Date().toString(),
     "Implementation-Version":project.version
            )
        }        
    }
}



repositories {
    mavenCentral()
    maven { url 'https://repo.spring.io/milestone' }
}

ext {
    set('springCloudVersion', 'Greenwich.RC2')
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    compile group: 'net.rakugakibox.spring.boot', name: 'logback-access-spring-boot-starter', version: '2.7.1'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

processResources {
  filesMatching('bootstrap.yml') { expand(project.properties) }
}


task copyJar(type: Copy) {
    dependsOn   'build'
    from        "build/libs/discovery-${project.version}.jar"
    into        'build/docker'
}

task buildDocker(type: Docker, dependsOn: copyJar) {
    push = false
    if(project.hasProperty('push2Repo')) {
    push = push2Repo
    }
    baseImage "openjdk:8u151"
    exposePort 8761
    runCommand("mkdir discovery")
    addFile("discovery-${project.version}.jar","discovery/")
    workingDir("/discovery")
    runCommand("unzip -q discovery-${project.version}.jar")
    entryPoint(["java","-XX:+UnlockExperimentalVMOptions","-XX:+UseCGroupMemoryLimitForHeap","-XX:MaxRAMFraction=1","-XshowSettings:vm","-cp", "/discovery-config:.","org.springframework.boot.loader.JarLauncher"])
}

application.yml

spring:
    main:
        show_banner: false
    application:
        name: discovery
server:
    port: 8761
eureka:
    instance:
        preferIpAddress: true                
    client:
        registerWithEureka: false
        fetchRegistry: false
        useDnsForFetchingServiceUrls: false
        serviceUrl:
            defaultZone: http://${CONFIG_DISCOVERY_HOST:discovery}:${CONFIG_DISCOVERY_PORT:8761}/eureka/
    server:
        waitTimeInMsWhenSyncEmpty: 0
        enable-self-preservation: false

主类文件

@SpringBootApplication
@EnableEurekaServer
public class DiscoveryApplication {

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

}

Gradle命令创建Docker映像

gradle buildDocker

docker run命令 docker run -d -p 8761:8761 --name =发现gcr.io/sc-cloud-rnd/discovery:1.0.1

0 个答案:

没有答案