Eureka,作为spring cloud的服务发现与注册中心,在整个的微服务体系中,处于核心位置。单一的eureka服务,显然不能满足高可用的实际生产环境,这就要求我们配置一个能够应对各种突发情况,具有较强容灾能力的eureka服务。下面我将以一个较为简单的例子,来描述这种高可用服务的实现原理。
一、服务搭建
New->Project-> 选择spring initialir 如下图:
下一步->选择cloud discovery->eureka server,然后一直下一步就行了
创建完后的工程的pom.xml文件如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.example</groupId> 7 <artifactId>eureka</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>eureka</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>1.5.9.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 <spring-cloud.version>Edgware.RELEASE</spring-cloud.version> 26 </properties> 27 28 <dependencies> 29 <dependency> 30 <groupId>org.springframework.cloud</groupId> 31 <artifactId>spring-cloud-starter-eureka-server</artifactId> 32 </dependency> 33 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-starter-test</artifactId> 37 <scope>test</scope> 38 </dependency> 39 </dependencies> 40 41 <dependencyManagement> 42 <dependencies> 43 <dependency> 44 <groupId>org.springframework.cloud</groupId> 45 <artifactId>spring-cloud-dependencies</artifactId> 46 <version>${spring-cloud.version}</version> 47 <type>pom</type> 48 <scope>import</scope> 49 </dependency> 50 </dependencies> 51 </dependencyManagement> 52 53 <build> 54 <plugins> 55 <plugin> 56 <groupId>org.springframework.boot</groupId> 57 <artifactId>spring-boot-maven-plugin</artifactId> 58 </plugin> 59 </plugins> 60 </build> 61 62 63 </project>
创建服务注册中心,在Application类中添加注解,,只需要一个注解@EnableEurekaServer
1 package com.example.demo; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 7 @EnableEurekaServer 8 @SpringBootApplication 9 public class EurekaApplication { 10 11 public static void main(String[] args) { 12 SpringApplication.run(EurekaApplication.class, args); 13 } 14 }
修改application.properties配置文件
1 server.port=8761 2 spring.application.name=eureka-server 3 eureka.client.fetch-registry=false 4 eureka.client.register-with-eureka=false 5 eureka.client.serviceUrl.defaultZone=http://localhost:8762/eureka/
二、打包部署
通过maven进行打包,生成jar包
将jar包分别部署到两台服务器上,服务器1(192.168.12.52),服务器2(192.168.12.36),修改jar包的配置文件application.properties
服务器1:
1 server.port=8761 2 spring.application.name=eureka-server 3 eureka.client.fetch-registry=false 4 eureka.client.register-with-eureka=false 5 eureka.client.serviceUrl.defaultZone=http://192.168.12.36:8762/eureka/
服务器2:
1 server.port=8762 2 spring.application.name=eureka-server 3 eureka.client.fetch-registry=false 4 eureka.client.register-with-eureka=false 5 eureka.client.serviceUrl.defaultZone=http://192.168.12.52:8761/eureka/
进入dos下,执行以下命令
java -jar eureka-0.0.1-SNAPSHOT.jar
打开网址192.168.12.52:8761,192.168.12.26:8762(注意两台服务器之间要能够ping通)可以写一个简单的服务,向搭建好的eureka服务中心注册,进行测试,这里就不再进行赘述。
时间: 2024-11-09 12:46:35