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 控制台



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值