使用docker-compose编排spring-cloud微服务

本例使用spring-cloud做微服务,分为discovery服务和config服务,
discovery微服务的pom.xml如下,使用mavan-docker插件做镜像并发布到docker宿主机上,使用此插件需要配置环境变量DOCKER-HOST:宿主机IP地址:2375

<?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</groupId>
    <artifactId>discovery</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.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>
        <java.version>1.8</java.version>
        <spring-cloud.version>Dalston.SR4</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </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>
                    <imageName>${project.name}:${project.version}</imageName>
                    <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
                    <skipDockerBuild>false</skipDockerBuild>
                    <resources>
                        <resource><!--将打包文件放到dockerDirectory指定的位置-->
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

DiscoveryApplication.java添加@EnableEurekaServer注解旨在表明其是一个服务发现

package com.example.demo;

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

@SpringBootApplication
@EnableEurekaServer
public class DiscoveryApplication {

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

修改resources/bootstrap.yml

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false            #不注册到服务发现,因为自己就是服务发现
    fetch-registry: false                      #不从服务发现上获取注册信息

在src/main下创建docker文件夹,在其中创建runboot.sh

java -server -jar /app/app.jar

在docker文件夹下继续创建Dockerfile

FROM java:8
VOLUME /tmp
WORKDIR /app
ADD discovery-0.0.1-SNAPSHOT.jar app.jar
ADD runboot.sh /
RUN chmod a+x /runboot.sh
EXPOSE 8761
CMD /runboot.sh

至此服务发现已创建完毕,使用mvn clean package docker:build -DskipTests命令即可在打包的同时创建docker镜像至宿主机中,在宿主机中使用docker images查看镜像如下


2.png

继续创建config服务,config就是一个普通的微服务,只需在ConfigApplication类上加入注解@EnableConfigServer和@EnableEurekaClient指定其为config注册中心即可。其他的类似服务发现,此处不再赘述,仅贴出bootstrap.yml配置

spring:
  application:
    name: config
  profiles:
    active: native
eureka:
  instance:
    non-secure-port: ${server.port:8888}
    metadata-map:
      instanceId: ${spring.application.name}:${random.value}
  client:
    service-url:
      defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/

按照之前创建服务发现镜像的方法继续创建config镜像。

接下来就是要使用docker compose了,由于使用curl拉docker-compose很慢,所以直接使用yum安装即可

sudo yum install docker-compose

使用docker-compose --version查看版本信息,如有,即是安装成功。
接下来开始写docker-compose.yml

sudo mkdir testcompose
touch docker-compose.yml
vim docker-compose.yml

docker-compose.yml如下

config:
    image: "config:0.0.1-SNAPSHOT"
    hostname: config
    links:
      - discovery            #指定连接服务发现
    environment:
        EUREKA_HOST: discovery
        EUREKA_POST: 8761
    ports:
      - "8888:8888"
    
discovery:
    image: "discovery:0.0.1-SNAPSHOT"
    hostname: discovery
    ports:
        - "8761:8761"

在testcompose文件下使用docker-compose up -d ,启动完毕后,使用docker-compose ps查看容器情况:


3.png

看到两个容器已经启动后,打开浏览器IP:8761打开
5.png

至此使用docker-compose编排springcloud 微服务的小练习已结束。

ps:在执行docker-compse up命令时可能会出现No Chain/target/match by that name的错误。这是由于防火墙没有没有配置nat规则

sudo vi /etc/sysconfig/iptables

增加nat规则即可

*nat  
:PREROUTING ACCEPT [27:11935]  
:INPUT ACCEPT [0:0]  
:OUTPUT ACCEPT [0:0]  
:POSTROUTING ACCEPT [0:0]  
:DOCKER -[0:0]  
-A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER  
-A OUTPUT !-d 127.0.0.0/8-m addrtype --dst-type LOCAL -j DOCKER  
-A POSTROUTING -s 172.17.0.0/16!-o docker0 -j MASQUERADE  
COMMIT  

推荐阅读更多精彩内容