微服务之springCloud和docker-Eureka(一)

前言

   本文记录怎么搭建服务注册中心eureka,然后打包成docker镜像,用docker swarm部署eureka集群

1、环境

环境版本及说明参考地址
docker v1.13.1,Docker是一个能够把开发的应用程序自动部署到容器的开源引擎 http://www.cnblogs.com/520playboy/p/7910357.html
doker-compose v1.11,Docker 官方编排(Orchestration)项目之一,负责快速在集群中部署分布式应  
docker swarm v1.13.1,Docker Engine 1.12或更高版本中内置了swarm(不用额外安装) http://www.cnblogs.com/520playboy/p/7873903.html
docker registry registry:latest,用于存储docker镜像的私有仓库 http://www.cnblogs.com/520playboy/p/7889647.html
spring boot 1.5.1.RELEASE,是开箱即用,提供一系列大型项目常用的非功能性特征的快速度开发工具 spring boot官网
spring cloud Camden SR5,Spring Cloud 为开发者提供了在分布式系统(如配置管理、服务发现、断路器、智能路由、微代理、控制总线、一次性 Token、全局锁、决策竞选、分布式会话和集群状态)操作的开发工具集 spring cloud官网
开发工具 jdk1.8/IntelliJ idea2016/maven3.3

2、创建父空项目 microservice-spring-cloud

3、父项目的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">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.jacky</groupId>
    <artifactId>microservice-spring-cloud</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>microservice-discovery-eureka-ha</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <docker.image.prefix>jacky</docker.image.prefix><!--配置镜像仓库的属性-->
        <docker.repostory>192.168.6.132:5000</docker.repostory><!--配置镜像仓库的对应的地址与端口-->
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR3</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>
        <!--添加利用maven插件构建docker镜像的插件依赖-->
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>com.spotify</groupId>
                    <artifactId>docker-maven-plugin</artifactId>
                    <version>0.4.13</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

4、创建子项目microservice-discovery-eureka-ha

5、microservice-discovery-eureka-ha工程的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">
    <parent>
        <artifactId>microservice-spring-cloud</artifactId>
        <groupId>com.jacky</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>microservice-discovery-eureka-ha</artifactId>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <executions>
                    <!--设置在执行maven 的install时构建镜像-->
                    <execution>
                        <id>build-image</id>
                        <phase>install</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--安装了docker的主机,并且打开了api remote接口设置-->
                    <dockerHost>http://192.168.6.130:5678</dockerHost>
                    <pushImage>true</pushImage><!--设置上传镜像到私有仓库,需要docker设置指定私有仓库地址-->
                    <!--镜像名称-->
                    <imageName>${docker.repostory}/${docker.image.prefix}/${project.artifactId}:${project.version}</imageName>
                    <!--镜像的基础版本-->
                    <baseImage>java:openjdk-8-jdk-alpine</baseImage>
                    <!--镜像启动参数-->
                    <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

5、microservice-discovery-eureka-ha工程的application.yml文件

spring:
  application:
    name: EUREKA-HA
security:
  basic:
    enabled: true
  user:
    name: jacky
    password: admin
---
server:
  port: 8761
spring:
  profiles: peer1
eureka:
  instance:
    hostname: peer1        #Eureka实例的主机名
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
  client:
    serviceUrl:
      defaultZone: http://jacky:admin@peer2:8762/eureka/,http://jacky:admin@peer3:8763/eureka/  #Eureka节点相互注册
      register-with-eureka: true
---
server:
  port: 8762
spring:
  profiles: peer2
eureka:
  instance:
    hostname: peer2      #Eureka实例的主机名
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
  client:
    serviceUrl:
      defaultZone: http://jacky:admin@peer1:8761/eureka/,http://jacky:admin@peer3:8763/eureka/
      register-with-eureka: true
---
server:
  port: 8763
spring:
  profiles: peer3
eureka:
  instance:
    hostname: peer3    #Eureka实例的主机名
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
  client:
    serviceUrl:
      defaultZone: http://jacky:admin@peer1:8761/eureka/,http://jacky:admin@peer2:8762/eureka/
      register-with-eureka: true

6、创建EurekaHaApplication.java文件

package com.jacky.cloud;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaHaApplication {
  public static void main(String[] args) {
    SpringApplication.run(EurekaHaApplication.class, args);
  }
}

7、在父目录下创建docker-compose.yml文件

version: "3"
services:
  peer1:      # 默认情况下,其他服务可以使用服务名称连接到该服务。因此,对于eurekaService1的节点,它需要连接http://eurekaService2/3:951X/eureka/,因此需要配置该服务的名称是eurekaService1。
    image: 192.168.6.132:5000/jacky/microservice-discovery-eureka-ha:1.0-SNAPSHOT
    deploy:
      replicas: 1   #定义 replicated 模式的服务的复本数量
      update_config:
        parallelism: 1    #每次更新复本数量
        delay: 2s       #每次更新间隔
      restart_policy:
        condition: on-failure     #定义服务的重启条件
    networks:
      - eureka-net
    ports:
      - "8761:8761"
    environment:
      - spring.profiles.active=peer1
  peer2:    # 高可用eureka注册节点2
    image: 192.168.6.132:5000/jacky/microservice-discovery-eureka-ha:1.0-SNAPSHOT
    deploy:
      replicas: 1   #定义 replicated 模式的服务的复本数量
      update_config:
        parallelism: 1    #每次更新复本数量
        delay: 2s       #每次更新间隔
      restart_policy:
        condition: on-failure     #定义服务的重启条件
    networks:
      - eureka-net
    ports:
      - "8762:8762"
    environment:
      - spring.profiles.active=peer2
  peer3:    # 高可用eureka注册节点3
    image: 192.168.6.132:5000/jacky/microservice-discovery-eureka-ha:1.0-SNAPSHOT
    deploy:
      replicas: 1   #定义 replicated 模式的服务的复本数量
      update_config:
        parallelism: 1    #每次更新复本数量
        delay: 2s       #每次更新间隔
      restart_policy:
        condition: on-failure     #定义服务的重启条件
    networks:
      - eureka-net
    ports:
      - "8763:8763"
    environment:
      - spring.profiles.active=peer3
networks:
  eureka-net:            #网络名称
    driver: overlay

8、打包

 

双击install

9、在192.168.6.130机器上查看镜像

 

表示上传成功

10、把的docker-compose.yml文件上传到拥有swarm环境下,执行命令如下

[root@node1 docker-compose]# docker stack deploy -c docker-compose.yml eureka
Creating service eureka_peer2
Creating service eureka_peer3
Creating service eureka_peer1
[root@node1 docker-compose]# docker stack ps eureka
ID            NAME            IMAGE                                                                   NODE             DESIRED STATE  CURRENT STATE           ERROR  PORTS
njhk2gkh7b6r  eureka_peer1.1  192.168.6.132:5000/jacky/microservice-discovery-eureka-ha:1.0-SNAPSHOT  node3.jacky.com  Running        Running 55 seconds ago         
y5xwabq42zx4  eureka_peer3.1  192.168.6.132:5000/jacky/microservice-discovery-eureka-ha:1.0-SNAPSHOT  node3.jacky.com  Running        Running 55 seconds ago         
468xoiu6lv6l  eureka_peer2.1  192.168.6.132:5000/jacky/microservice-discovery-eureka-ha:1.0-SNAPSHOT  node2.jacky.com  Running        Running 57 seconds ago 

说明eureka集群部署成功

 

 说明:swarm集群192.168.6.130;192.168.6.131;192.168.6.132;8761,8762,8763端口都可以访问

 喜欢本文的朋友,欢迎关注,本人的微信公众号,“咖啡牧羊人”

 

posted @ 2017-11-30 16:07  坏~牧羊人  阅读(15621)  评论(1编辑  收藏  举报