springCloud(二)Erueka实现高可用
使用两个eureka注册中心实现注册中心
搭建
erueka 注册中心一 : eruekaServer
搭建工程,
项目目录
引入POM文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.16.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.jm</groupId> <artifactId>server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eruekaServer</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-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>
配置文件
8801 为第一个端口, 8802 为第二个端口
spring:
application:
name: service-erueka-register
server:
port: 8801
eureka:
instance:
hostname: 127.0.0.1
prefer-ip-address: true
leaseExpirationDurationInSeconds: 15
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://127.0.0.1:8802/eureka/
启动文件
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class EruekaServerApplication { public static void main(String[] args) { SpringApplication.run(EruekaServerApplication.class, args); } }
erueka 注册中心二 : eruekaServer2
配置文件
spring:
application:
name: service-erueka-register
server:
port: 8802
eureka:
instance:
hostname: 127.0.0.1
prefer-ip-address: true
leaseExpirationDurationInSeconds: 15
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://127.0.0.1:8801/eureka/
#http://${eureka.instance.hostname}:${server.port}/eureka/
客户端
配置文件
eureka:
client:
serviceUrl: #注册中心的注册地址
defaultZone: http://127.0.0.1:8801/eureka/
#也可以两个都注册,一个够了会相互注册,如 http://127.0.0.1:8801/eureka/, http://127.0.0.1:8802/eureka/
server:
port: 8804 #服务端口号
spring:
application:
name: service-provider #服务名称--调用的时候根据名称来调用该服务的方法
启动
启动两个注册中心
8801, 8802 端口都如下 , 显示两个都注册成功
再启动 客户端
github: https://github.com/szjomin/eruekaDemo
参考:
两个:https://www.cnblogs.com/Dev0ps/p/9899621.html
三个:https://blog.csdn.net/weixiaohuai/article/details/82499345