Objective
This document records how to build a service registry Eureka, then package it into a Docker image and deploy the Eureka cluster with Docker swarm
1. Environment
Spring Boot 1.5.1.RELEASE, a fast-speed development tool that provides a wide range of non-functional features commonly used in large projects, is out-of-the-box spring Boot official website
Spring Cloud Camden sr5,spring Cloud provides developers with distributed systems such as configuration management, service discovery, circuit breakers, intelligent routing, micro-proxies, control buses, one-time tokens, global locks, decision-making campaigns, Distributed session and cluster State) operation Development Toolset Spring Cloud website
Development tools Jdk1.8/intellij idea2016/maven3.3
2. Create a parent Empty project Microservice-spring-cloud
3. pom.xml file for parent project
<?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> <artif Actid>microservice-spring-cloud</artifactid> <packaging>pom</packaging> <version>1.0- snapshot</version> <modules> <module>microservice-discovery-eureka-ha</module> </m Odules> <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><!--Configuring the properties of a mirrored warehouse-<docker.repostory> 192.168.6.132:5000</docker.repostory><!--Configure the corresponding address and port of the mirrored warehouse-</properties> < dependencymanagement> <dependencies> <dependency> <groupid>org.sprin Gframework.cloud</groupid> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR3</version> <type>pom</type> <scope>impo rt</scope> </dependency> </dependencies> </dependencyManagement> <build > <plugins> <plugin> <GROUPID>ORG.SPRINGFRAMEWORK.BOOT</GROUPID&G T <artifactid>spring-boot-maven-plugin&lT;/artifactid> </plugin> </plugins> <!--adding plug-ins that build Docker images with Maven plugins-- <pluginManagement> <plugins> <plugin> <GROUPID>COM.S Potify</groupid> <artifactId>docker-maven-plugin</artifactId> < version>0.4.13</version> </plugin> </plugins> </pluginmanagement& Gt </build></project>
4. Create sub-project Microservice-discovery-eureka-ha
5. pom.xml File of Microservice-discovery-eureka-ha project
<?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</ARTIFAC tid> <groupId>com.jacky</groupId> <version>1.0-SNAPSHOT</version> </parent& Gt <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-ser Ver</artifactid> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId> spring-boot-starter-security</artifactid> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>com.spotify</groupId> <a Rtifactid>docker-maven-plugin</artifactid> <executions> <!--set up in the Execute MAVEN Build image at install-<execution> <id>build-image</id> <phase>install</phase> <goals> <goal >build</goal> </goals> </execution> </exe Cutions> <configuration> <!--installed a Docker host and opened the API remote interface settings- <dockerhost>http://192.168.6.130:5678</dockerhost> <pushImage>true</pushImage><!--Set upload image to Private warehouse, requires Docker settings specify private warehouse address--<!--mirror name--<imagename>${docker.repostory} /${docker.image.prefix}/${project.artifactid}:${project.version}</imagename> <!--The base version of the image-- <baseImage>java:openjdk-8-jdk-alpine</baseImage> <!--Mirroring Startup Parameters-- <entrypoint>["Java", "-jar", "/${project.build.finalname}.jar"]</entrypoint> <resources> <resource> <targetpath>/</targetpath > <directory>${project.build.directory}</directory> & Lt;include>${project.build.finalname}.jar</include> </resource> & Lt;/resources> </configuration> </plugin> </plugins> </build></project>
5, application.yml file for Microservice-discovery-eureka-ha project
Spring:application:name:eureka-hasecurity:basic:enabled:true user:name:jacky password:admin---Serv Er:port:8761spring:profiles:peer1eureka:instance:hostname:peer1 #Eureka实例的主机名 Prefer-ip-address:tr UE Instance-id: ${spring.application.name}:${spring.cloud.client.ipaddress}:${spring.application.instance_id:${ Server.port}} Client:serviceurl:defaultzone:http://jacky:[email protected]:8762/eureka/,http://jacky:[ema il protected]:8763/eureka/#Eureka节点相互注册 register-with-eureka:true---server:port:8762spring:profiles:pee R2eureka: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:defa ultzone:http://jacky:[email protected]:8761/eureka/,http://jacky:[email protected]:8763/eureka/ Register-with-eureka:true---Server:port: 8763spring:profiles:peer3eureka:instance:hostname:peer3 #Eureka实例的主机名 prefer-ip-address:true Instanc E-id: ${spring.application.name}:${spring.cloud.client.ipaddress}:${spring.application.instance_id:${ Server.port}} Client:serviceurl:defaultzone:http://jacky:[email protected]:8761/eureka/,http://jacky:[ema Il protected]:8762/eureka/register-with-eureka:true
6. Create a Eurekahaapplication.java file
package com.jacky.cloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServerpublic class EurekaHaApplication { public static void main(String[] args) { SpringApplication.run(EurekaHaApplication.class, args); }}
7. Create the Docker-compose.yml file in the parent directory
Version: "3" Services:peer1: # By default, other services can connect to the service using the service name. Therefore, for EUREKASERVICE1 nodes, it needs to connect http://eurekaService2/3:951X/eureka/, so the name of the service that needs to be configured is EurekaService1. Image:192.168.6.132:5000/jacky/microservice-discovery-eureka-ha:1.0-snapshot deploy:replicas:1 #定义 Replicated Number of copies of the service for the mode update_config:parallelism:1 #每次更新复本数量 delay:2s #每次更新间隔 restart_policy: Condition:on-failure #定义服务的重启条件 Networks:-Eureka-net Ports:-"8761:8761" Environment: -Spring.profiles.active=peer1 Peer2: # Highly available Eureka Register Node 2 Image:192.168.6.132:5000/jacky/microservice-discovery-eurek A-ha:1.0-snapshot Deploy:replicas:1 #定义 The number of replicas for a service in replicated mode Update_config:parallelism:1 #每次 Update the number of replicas Delay:2s #每次更新间隔 restart_policy:condition:on-failure #定义服务的重启条件 Networks:- Eureka-net ports:-"8762:8762" Environment:-spring.profiles.active=peer2 Peer3: # High AvailabilityEureka Register Node 3 image:192.168.6.132:5000/jacky/microservice-discovery-eureka-ha:1.0-snapshot deploy:replicas:1 Number of replicas of services #定义 replicated mode update_config:parallelism:1 #每次更新复本数量 delay:2s #每次更新间隔 Rest Art_policy:condition:on-failure #定义服务的重启条件 Networks:-Eureka-net Ports:-"8763:8763" env Ironment:-spring.profiles.active=peer3networks:eureka-net: #网络名称 driver:overlay
8. Packing
Double-click Install
9. View the image on the 192.168.6.130 machine
Indicates successful upload
10. Upload the Docker-compose.yml file to the owning swarm environment, execute the command as follows
[[email protected] docker-compose]# docker stack deploy -c docker-compose.yml eurekaCreating service eureka_peer2Creating service eureka_peer3Creating service eureka_peer1[[email protected] docker-compose]# docker stack ps eurekaID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTSnjhk2gkh7b6r 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
Description Eureka Cluster Deployment succeeded
Springcloud and Docker-eureka of MicroServices (i)