微服務之springcloud eureka(一)

咖啡牧羊人發表於2019-03-19

前言

本文記錄怎麼搭建服務註冊中心eureka,然後打包成docker映象,用docker swarm部署eureka叢集

1、環境

環境版本及說明參考地址
dockerv1.13.1,Docker是一個能夠把開發的應用程式自動部署到容器的開源引擎http://www.cnblogs.com/520playboy/p/7910357.html
doker-composev1.11,Docker 官方編排(Orchestration)專案之一,負責快速在叢集中部署分散式應
docker swarmv1.13.1,Docker Engine 1.12或更高版本中內建了swarm(不用額外安裝)http://www.cnblogs.com/520playboy/p/7873903.html
docker registryregistry:latest,用於儲存docker映象的私有倉庫http://www.cnblogs.com/520playboy/p/7889647.html
spring boot1.5.1.RELEASE,是開箱即用,提供一系列大型專案常用的非功能性特徵的快速度開發工具spring boot官網
spring cloudCamden SR5,Spring Cloud 為開發者提供了在分散式系統(如配置管理、服務發現、斷路器、智慧路由、微代理、控制匯流排、一次性 Token、全域性鎖、決策競選、分散式會話和叢集狀態)操作的開發工具集spring cloud官網
開發工具jdk1.8/IntelliJ idea2016/maven3.3

2、建立父空專案 microservice-spring-cloud

微服務之springcloud eureka(一)

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

微服務之springcloud eureka(一)

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、打包

微服務之springcloud eureka(一)

雙擊install

9、在192.168.6.130機器上檢視映象

微服務之springcloud eureka(一)

表示microservice-discovery-eureka-ha映象上傳成功

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叢集部署成功

微服務之springcloud eureka(一)

說明:swarm叢集192.168.6.130;192.168.6.131;192.168.6.132;8761,8762,8763埠都可以訪問

喜歡本文的朋友,歡迎關注,本人的微信公眾號,“咖啡牧羊人”

微服務之springcloud eureka(一)


相關文章