项目结构

 1.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 https://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.3.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.it</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>


    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
    </properties>

    <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-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <!--依赖管理  只是管理版本号以及子模块的依赖 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

</project>

2.EurekaServerApplication主函数类

package com.it;

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

@SpringBootApplication
@EnableEurekaServer //开启eureka注册中心的功能
public class EurekaServerApplication {

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

}

3.application.yml配置文件

#单机
server:
  port: 8761 #eureka��Ĭ�϶˿�
spring:
  application:
    name: eureka-server #Ӧ������
#eureka的配置分为3类,server  client 实例的  eureka-server既是服务端又是客户端
eureka:
  server:
    eviction-interval-timer-in-ms: 1000 #服务端间隔多少毫秒做定期删除的操作
    renewal-percent-threshold: 0.85 #续约百分比,超过85%的应用没有和你续约,那么eureka会保护服务,不会提出任何一个
  instance: #实例的配置
    instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port} #主机名称:应用名称:端口号
    hostname: localhost #主机名称或则服务的ip
    prefer-ip-address: true #以ip的形式显示具体的服务信息
    lease-renewal-interval-in-seconds: 5 #服务实例的续约时间间隔
  client:
    registry-with-eureka: ${REGISTER_WITH_EUREKA:true} #先将server自己注册自己的开关关掉
    service-url:
      defaultZone: ${EUREKA_SERVER_URL:http://localhost:8761/eureka}
    fetch-registry: true
#集群
#server:
#  port: 8761
#spring:
#  application:
#    name: eureka-server
#eureka:
#  client:
#    service-url:
#      defaultZone: http://peer2:8762/eureka,http://peer3:8763/eureka
#  instance:
#    instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
#    hostname: peer1
#    prefer-ip-address: true
#    lease-renewal-interval-in-seconds: 5

#集群的终极方案
#server:
#  port: 8761
#spring:
#  application:
#    name: eureka-server
#eureka:
#  client:
#    service-url:
#      defaultZone: http://peer1:8761/eureka,http://peer2:8762/eureka,http://peer3:8763/eureka
#  instance:
#    instance-id: ${spring.application.name}:${server.port}
#    prefer-ip-address: true
#    lease-renewal-interval-in-seconds: 5

4.选择进行打jar包

5.打开linux虚拟机,启动docker镜像,并创建目录为了方便编写自定义镜像

 6.创建docker目录,把jar包粘贴过来,并重命名(为了后面调用时方便)

选择新建文件,编写DockerFile文件

选择新建文件,编写run.sh脚本

7.把项目中docker目录下的三个文件上传到linux虚拟机

给run.sh脚本赋予权限

 执行脚本

发现出现如下错误

unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /root/springcloud/eureka-server/Dockerfile: no such file or directory
检查发现Dockerfile写成了DockerFile,导致运行出错

解决办法:修改项目中的文件,并重新上传该文件到虚拟机

 现在,再次执行脚本,运行成功 

查看docker镜像

8.运行镜像

 docker run --name eureka-server -p 8761:8761  -e REGISTER_WITH_EUREKA=false -d eureka-server:1.0

9.开启日志

 docker logs eureka-server

 

 10.通过浏览器访问

本地主机ip+8761

成功进入说明部署成功 

补充:docker删除容器

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐