springCloud Eureka 高可用实现

新建一个统一管理工程

在这里插入图片描述

provider-user工程:
pom:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.yb</groupId>
    <artifactId>microservicedemo</artifactId>
    <version>1.0</version>
  </parent>
  <artifactId>provider-user</artifactId>
  <!-- <version>1.0</version> -->

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
 		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
         <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency> 
  </dependencies>

</project>

application配置:

eureka:
  client:
    service-url:
    #http://localhost:8761/eureka/(即注册到的服务地址)
#      defaultZone: http://localhost:10000/eureka
      defaultZone: http://localhost:8762/eureka

#  instance:
#  # 将自己的IP注册到Eureka Server。若不配置或设置为false,表示注册微服务所在操作系统的hostname到Eureka Server
#    prefer-ip-address: true 
server:
  port: 7900 #程序启动后的端口,也就是tomcat的端口,我们可以自己定义
spring:
  application:
  # 指定注册到Eureka Server上的应用名称
    name: provider-user 

Provider类:

package com.yb.provider;

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


@SpringBootApplication
@EnableEurekaClient//启用 eureka 客户端
public class Provider {
	public static void main(String[] args) {
		SpringApplication.run(Provider.class);
	}
}

eureka-ha工程:
pom:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.yb</groupId>
    <artifactId>microservicedemo</artifactId>
    <version>1.0</version>
  </parent>
  <artifactId>eureka-ha</artifactId>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
       <!-- eurekaserver 依赖 -->
       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <!-- <version>1.4.3.RELEASE</version> --> 
        </dependency>
        <!-- 安全依赖 -->
        <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
  </dependencies>
</project>

EurekaApp分别启动三次,对应三种配置文件:

package com.eureka.yb;

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

@SpringBootApplication
@EnableAutoConfiguration(exclude = {
        org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
})
@EnableEurekaServer // 声明这是一个Eureka Server
public class EurekaApp {
	public static void main(String[] args) {
		SpringApplication.run(EurekaApp.class);
	}
}

application.yml:

spring:
  application:
    name: EUREKA-HA
  profiles:
    active: peer3   
server:
  port: 8762  #注册服务中心的端口号
eureka:
  instance:
    hostname: localhost
#    prefer-ip-address: true     
  client:
#    register-with-eureka: false # 是否将自己注册到Eureka Server,默认为true(高可用模式配置方式)。由于当前应用就是Eureka Server,故而设为false
#    fetch-registry: false # 表示是否从Eureka Sever获取注册信息,默认为true(同步其他节点Eureka Server数据)。因为这一个单节点就是Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设为false
#    hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000 
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka/,http://127.0.0.1:8763/eureka/ # 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。 

application.yml:

spring:
  application:
    name: EUREKA-HA
  profiles:
    active: peer3   
server:
  port: 8763  #注册服务中心的端口号
eureka:
  instance:
    hostname: localhost
#    prefer-ip-address: true     
  client:
#    register-with-eureka: false # 是否将自己注册到Eureka Server,默认为true(高可用模式配置方式)。由于当前应用就是Eureka Server,故而设为false
#    fetch-registry: false # 表示是否从Eureka Sever获取注册信息,默认为true(同步其他节点Eureka Server数据)。因为这一个单节点就是Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设为false
#    hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000 
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka/,http://127.0.0.1:8762/eureka/ # 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。 
      

application.yml:

spring:
  application:
    name: EUREKA-HA
  profiles:
    active: peer3   
server:
  port: 8761  #注册服务中心的端口号
eureka:
  instance:
    hostname: localhost
#    prefer-ip-address: true     
  client:
#    register-with-eureka: false # 是否将自己注册到Eureka Server,默认为true(高可用模式配置方式)。由于当前应用就是Eureka Server,故而设为false
#    fetch-registry: false # 表示是否从Eureka Sever获取注册信息,默认为true(同步其他节点Eureka Server数据)。因为这一个单节点就是Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设为false
#    hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000 
    service-url:
      defaultZone: http://127.0.0.1:8762/eureka/,http://127.0.0.1:8763/eureka/ # 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。 

运行效果截图:

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

扫描二维码关注公众号,回复: 11384325 查看本文章

猜你喜欢

转载自blog.csdn.net/m0_46266503/article/details/105780961