利用一个镜像去创建多个微服务实例,实现扩展服务的目的,主要难点是利用配置文件分别启动不同的实例,刚开始没有搞清楚配置文件和容器启动的关系,被坑了很长时间

代码其实很简单,就是一个注册服务中心,以开发环境配置文件创建服务注册中心,以测试环境配置服务注册中心,使两个服务注册中心都 可以用,只是端口不一样,另外如果不其它东西,仅仅是改端口,其实也可以直接启动容器的时候把端口号指定出来,-d  -p  8080:8080 等这种方式也是成立的,想改变更多的内容就要用配置文件来启动了,

第一  启动服务注册的类

@SpringBootApplication
@EnableEurekaServer
@EnableDiscoveryClient
public class TesteurakaApplication {


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

第二,两个配置文件application-dev.yml,和 application.yml

application-dev.yml如下

server:
  port: 8787
spring:
  application:
    name: dev
eureka:
  client:
    registerWithEureka: true
    fetchRegistry: true
    server:
      enable-self-preservation: false
    serviceUrl:
      defaultZone: http://peer1:5011/eureka
  instance:
    hostname: peer2
#peer1、peer2和peer3需要在hosts文件中设置为127.0.0.1,在生产环境中可以写真实的域名
    metadataMap:
      instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}}

application.yml 如下

server:
  port: 8711
spring:
  application:
    name: test-dev
eureka:
  client:
    registerWithEureka: true
    fetchRegistry: true
    server:
      enable-self-preservation: false
    serviceUrl:
      defaultZone: http://peer2:5012/eureka
  instance:
    hostname: peer1
#peer1、peer2和peer3需要在hosts文件中设置为127.0.0.1,在生产环境中可以写真实的域名
    metadataMap:
      instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}}
#三个横杠表示一个新的文件环境配置,同一文件写多个文件配置环境必须写spring:application:name=dev属性名称
---
spring:
  profiles:
    active: dev
#用这种方式启动容器加载配置文件,多实例同一个镜像启动多个容器不同的端口号,达到高可用的目的#docker run -d -p 8787:8787 ca7f456ae49c java -jar app.jar --spring.profiles.active=test-server

Dockerfile 文件这是容器的生成文件

FROM java:8
VOLUME /tmp
RUN mkdir /app
ADD testeuraka-1.0.0.jar /app/app.jar
ADD runboot.sh /app/
RUN bash -c 'touch /app/app.jar'
WORKDIR /app
RUN chmod a+x runboot.sh
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ 
    echo 'Asia/Shanghai' >/etc/timezone
EXPOSE 5010CMD /app/runboot.sh

还有个 runboot.sh 脚本  这个网上应该能找到,我也是下载的

下面是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>


 <groupId>com.example.euraka</groupId>
 <artifactId>testeuraka</artifactId>
 <version>1.0.0</version>
 <packaging>jar</packaging>


 <name>testeuraka</name>
 <description>Demo project for Spring Boot</description>


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


 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 <version.jackson>2.9.2</version.jackson>
 <java.version>1.8</java.version>
 <docker.url>http://192.168.11.233:2375</docker.url> 
 <spring-cloud.version>Finchley.RELEASE</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>
 <plugin>
 <groupId>com.spotify</groupId>
 <artifactId>docker-maven-plugin</artifactId>
 <configuration>
 <dockerHost>${docker.url}</dockerHost>
 <imageName>${project.name}:${project.version}</imageName>
 <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
 <skipDockerBuild>false</skipDockerBuild>
 <resources>
 <resource>
 <targetPath>/</targetPath>
 <directory>${project.build.directory}</directory>
 <include>${project.build.finalName}.jar</include>
 </resource>
 <!-- 强制docker在每次新的构建上覆盖镜像tags -->
 <!--  <forceTags>true</forceTags> -->
 </resources>
 </configuration>
 </plugin>
 </plugins>
 </build>
</project>

上面有直接打包容器到哪个服务器的IP,根本自己的IP改就行

整体项目是这样

docker 启动两个命令 docker 启动多个服务_eureka

最重要的还是这种方式启动了

#docker run -d -p 8787:8787 ca7f456ae49c java -jar app.jar --spring.profiles.active=test-server

之前尝试了很多,都 没有成功,这样就在同一镜像里创建两个容器实例,创建容器并不难,最开始可能没有认真看文档,也是被坑了好多次,

docker 启动两个命令 docker 启动多个服务_spring_02