Spring Cloud 的 Eureka HA

一、Eureka Server HA

     在Windows或Linux的hosts文件進行如下修改, 如兩臺電腦, 進行物理IP設置
127.0.0.1 peer1  
127.0.0.1 peer2 


     複製上一章節的 eureka-server工程, 改名爲 eureka-server-ha。

     1) 對yml文件進行如下配置:

spring:
  application:  
    name: microservice-eureka-server-ha1
  # 指定 profile=peer1
  profiles: peer1
server:
  port: 8761                                   # 註冊服務的端口號  
eureka:
  instance:
    hostname: peer1                            # 指定當profile=peer1時,主機名  
  client:
    service-url:
      defaultZone: http://peer2:8762/eureka/   # 將自己註冊到peer2這個Eureka上面去
      
---
spring:
  application:  
    name: microservice-eureka-server-ha2
  # 指定 profile=peer2
  profiles: peer2
server:
  port: 8762
eureka:
  instance:
    hostname: peer1
  client:
    #register-with-eureka: false   #表示是否將自己註冊到Eureka Server中,默認爲true, 由於當前應用就是 Eureka Server, 故而設置爲false
    #fetch-registry: false         #表示是否從 Eureka Server中獲取註冊信息, 默認爲true, 因爲這是一個單點的 Eureka Server, 不需要同步其它的 Eureka Server 節點的數據, 故而設置爲 false
    service-url:
      defaultZone: http://peer1:8761/eureka/  # 設置與Eureka Server交互的地址, 查詢服務和註冊服務都需要依賴這個地址. 默認http://localhost:8761/eureka; 多個地址可以使用","分隔
       

     2) pom.xml Maven文件配置

<?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.itmuch.cloud</groupId>
	<artifactId>microservice-eureka-server-ha</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>eureka-server-ha</name>
	<description>Simple for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.6.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.SR3</spring-cloud.version>
	</properties>

	<dependencies>
		<!-- Eureka 服務  -->
		<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>
		</plugins>
	</build>


</project>

       3) 主程序 

package com.itmuch.cloud;

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

@SpringBootApplication
@EnableEurekaServer    // 開啓這是一個 Eureka Server 程序
public class EurekaServerApplication {

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

   

       4) Maven 打包

mvn clean package 


      5) 在DOS中運行

java -jar springcloud-eureka-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1  
java -jar springcloud-eureka-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2  


       

二、Eureka Client 的YML文件defaultZone指向配置

        只要配置 defaultZone 的指向

server:
  port: 8081
  session:
    timeout: 30
  tomcat:
    max-threads: 0
    uri-encoding: UTF-8
    
spring:
  application:
    #表示註冊到Eureka上的應用名稱
    name: cloud-service
  datasource:
    url: jdbc:mysql://localhost:3306/boot
    username: root
    password: xshdb
    driver-class-name: com.mysql.jdbc.Driver
    
  jpa:
    # Specify the DBMS
    database: MYSQL
    # Show or not log for each sql query
    show-sql: true
    # Hibernate dll auto (create, create-drop, update)
    hibernate:
       ddl-auto: update

eureka:
  client:
    service-url:
      #將服務註冊到集羣上
      defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/
  instance:
    prefer-ip-address: true     # 表示將自己的IP註冊到Eureka Serverk, 如果不配置該屬性或將它改爲false,則表示註冊微服務所在操作系統的hostname到Eureka Server
    

三、訪問 Eureka Web 控制檯



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章