高可用注册中心Eureka搭建

一、基础环境

    注:本次开发环境 为win10 ,部署环境均为centos7

    1)jdk 1.8 (开发环境)

   

    2)maven 3.5.2  (开发环境)


    3)搭建私有registry (部署环境)

参考笔者前一篇blog 《docker搭建私有registry及打开docker镜像编译服务器远程访问权限》

本次镜像编译服务器使用 

http://192.168.8.238:5678

registry中心使用

    

192.168.8.238:5000


4)注册中心拟采用双节点部署,分别为 http://192.168.8.235:8761/ 和http://192.168.8.237:8761/ 节点

二、搭建 

1、新建一个maven项目 (开发环境)

    a)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.wy</groupId>
	<artifactId>eureka_center</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>EurekaCenter</name>
	<description>Eureka Center for Spring Boot</description>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.2.RELEASE</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<docker.repostory>192.168.8.238:5000</docker.repostory><!--配置镜像仓库的对应的地址与端口-->
		<docker.image.prefix>wy</docker.image.prefix><!--配置镜像仓库的属性-->
		<docker.build>http://192.168.8.238:5678</docker.build><!--配置编译服务器地址和端口-->
	</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>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<!--docker编译插件 -->
			<plugin>
				<groupId>com.spotify</groupId>
				<artifactId>docker-maven-plugin</artifactId>
				<version>0.4.13</version>
				<executions>
					<!--设置在执行maven 的install时构建镜像-->
					<execution>
						<id>build-image</id>
						<phase>install</phase>
						<goals>
							<goal>build</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<!--安装了docker的主机,并且打开了api remote接口设置-->
					<dockerHost>${docker.build}</dockerHost>
					<pushImage>true</pushImage><!--设置上传镜像到私有仓库,需要docker设置指定私有仓库地址-->
					<!--镜像名称-->
					<imageName>${docker.repostory}/${docker.image.prefix}/${project.artifactId}:${project.version}</imageName>
					<!--镜像的基础版本-->
					<baseImage>java:openjdk-8-jdk-alpine</baseImage>
					<!--镜像启动参数-->
					<entryPoint>["sh", "-c", "java $PARAMS  -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>

b)在src/main/java的某包下创建springboot程序入口类

package com.wy.eureka;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaCenterApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaCenterApplication.class, args);
	}
}
c)在src/main/resources下创建192.168.8.235:8761 节点的配置文件,名为 application-peer1.yml 
server:
  port: 8761
spring:
  application:
    name: EUREKA-CENTER
security:
  basic:
    enabled: true
  user:
    name: abc
    password: 123
eureka:
  instance:
    hostname: EurekaCenter1        #Eureka实例的主机名
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${eureka.instance.hostname}:${spring.application.instance_id:${server.port}}
    lease-renewal-interval-in-seconds: 30  #默认服务续约时间为30S
    lease-expiration-duration-in-seconds: 90 #默认服务失效时间为90S ,eureka默认每隔60秒,会将清单内超时的(默认90秒)剔除掉。
    ip-address: 192.168.8.235 #指定本服务ip
  client:
    serviceUrl:
      defaultZone: http://abc:123@192.168.8.237:8761/eureka/ #Eureka节点相互注册
    register-with-eureka: true #向eureka注册自己
    fetch-registry: true #从Eureka获取注册信息
    registry-fetch-interval-seconds: 30 #默认客户端清单更新时间为30秒(每个客户端如不自行配置,默认为30秒)
    eureka-service-url-poll-interval-seconds: 60 #轮询Eureka服务端地址变更的时间间隔,默认为300秒,与配置中心结合动态刷新serviceUrl时,需要调整该参数

  server:
    enable-self-preservation: false #不使用自我保护(自我保护默认开启,Eureka服务器15分钟内心跳成功比例低于85%,则触发自我保护机制,不剔除任何注册的服务信息,直至心跳成功数大于85%,本地频繁调试,故而关闭该功能)
    peer-eureka-nodes-update-interval-ms: 60000 #集群里eureka节点的变化信息更新的时间间隔
d)在src/main/resources下创建192.168.8.237:8761 节点的配置文件,名为 application-peer2.yml 
server:
  port: 8761
spring:
  application:
    name: EUREKA-CENTER
security:
  basic:
    enabled: true
  user:
    name: abc
    password: 123
eureka:
  instance:
    hostname: EurekaCenter2        #Eureka实例的主机名
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${eureka.instance.hostname}:${spring.application.instance_id:${server.port}}
    lease-renewal-interval-in-seconds: 30  #默认服务续约时间为30S
    lease-expiration-duration-in-seconds: 90 #默认服务失效时间为90S ,eureka默认每隔60秒,会将清单内超时的(默认90秒)剔除掉。
    ip-address: 192.168.8.237
  client:
    serviceUrl:
      defaultZone: http://abc:123@192.168.8.235:8761/eureka/ #Eureka节点相互注册
    register-with-eureka: true #向eureka注册自己
    fetch-registry: true #从Eureka获取注册信息
    registry-fetch-interval-seconds: 30 #默认客户端清单更新时间为30秒(每个客户端如不自行配置,默认为30秒)
    eureka-service-url-poll-interval-seconds: 60 #轮询Eureka服务端地址变更的时间间隔,默认为300秒,与配置中心结合动态刷新serviceUrl时,需要调整该参数

  server:
    enable-self-preservation: false #不使用自我保护(自我保护默认开启,Eureka服务器15分钟内心跳成功比例低于85%,则触发自我保护机制,不剔除任何注册的服务信息,直至心跳成功数大于85%,本地频繁调试,故而关闭该功能)
    peer-eureka-nodes-update-interval-ms: 60000 #集群里eureka节点的变化信息更新的时间间隔


注:笔者采用直接指定了ip地址的方式,有兴趣的小伙伴亦可以使用其他方式(如忽略指定名称的网卡、正则表达式指定使用网络地址等)来确定多网卡下docker的IP选择问题。


2、在项目路径下执行编译命令 (开发环境)

    

mvn install spring-boot:repackage
3、部署 (部署环境)
    在192.168.8.235执行:
        docker run -e PARAMS='-Dspring.profiles.active=peer1' -d -p 8761:8761 --restart=always 192.168.8.238:5000/wy/eureka_center:0.0.1-SNAPSHOT
    在192.168.8.237执行:
        docker run -e PARAMS='-Dspring.profiles.active=peer2' -d -p 8761:8761 --restart=always 192.168.8.238:5000/wy/eureka_center:0.0.1-SNAPSHOT


三、验证

    分别访问 http://192.168.8.235:8761/ 和http://192.168.8.237:8761/ ,登陆后可看到如下信息:



可以看到,注册中心已经相互注册。(没有相互注册成功的话,检查client.serviceUrl.defaultZone的地址对不对,然后防火墙有没有拉下或者配置开放端口。)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值