SpringCloud 项目搭建 --Eureka 集群

一:Eureka高可用的作用

之所以进行eureka集群的搭建,在于我们平时的生产环境中,很难保证单节点的eureka服务能提供百分百不间断的服务,如果eureka无响应了,整个项目应用都会出现问题,因此要保证eureka随时都能提供服务的情况下,最好的方式就是采用eureka的集群模式,也就是搭建eureka的高可用,在eureka的集群模式下,多个eureka server之间可以同步注册服务,因此,在一个eureka宕掉的情况下,仍然可以提供服务注册和服务发现的能力,从而达到注册中心的高可用。

 

 

二:搭建高可用集群

继续按之前创建的maven项目下创建

https://www.cnblogs.com/Runawayprogrammer/p/13929355.html

新建springcloud-eureka-7001 springcloud-eureka-7002 springcloud-eureka-7003  三个maven子项目

修改映射配置

修改C:\Windows\System32\drivers\etc路径下的hosts文件

127.0.0.1     eureka7001.com

 

 

 

1.项目结构

 

2.引入pom依赖

三个子项目都引入主要 --eureka依赖

 1  <!--eureka导依赖-->
 2     <dependencies>
 3         <dependency>
 4             <groupId>org.springframework.cloud</groupId>
 5             <artifactId>spring-cloud-starter-eureka-server</artifactId>
 6             <version>1.4.6.RELEASE</version>
 7         </dependency>
 8         <!--<dependency>
 9             <groupId>org.springframework.boot</groupId>
10             <artifactId>spring-boot-devtools</artifactId>
11         </dependency>-->
12     </dependencies>

2.配置三个项目的application.yml 文件

 1 server:
 2   port: 7001
 3 
 4 #eureka
 5 eureka:
 6   instance:
 7     hostname: eureka7001.com #eureka 服务端实例名字
 8   client:
 9     register-with-eureka: false #表示是否向eureka注册中心注册自己
10     fetch-registry: false #如果为false 表示自己是注册中心
11     serviceUrl:
12       defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
13       #单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
14       #集群关联
15       #defaultZone: http://7002的服务器ip地址:7002/eureka/,http://7003的服务器ip地址:7003/eureka/
16       #(7001——关联7002.7003 注册中心集群)
 1 server:
 2   port: 7002  #端口号
 3 
 4 #eureka
 5 eureka:
 6   instance:
 7     hostname: eureka7002.com #eureka 服务端实例名字
 8   client:
 9     register-with-eureka: false #表示是否向eureka注册中心注册自己
10     fetch-registry: false #如果为false 表示自己是注册中心
11     serviceUrl:
12       defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
13       #单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
14       #集群关联
15       #defaultZone: http://7001的服务器ip地址:7001/eureka/,http://7003的服务器ip地址:7003/eureka/
16       #(7002——关联7001.7003 注册中心集群)
server:
  port: 7003

#eureka
eureka:
  instance:
    hostname: eureka7003.com #eureka 服务端实例名字
  client:
    register-with-eureka: false #表示是否向eureka注册中心注册自己
    fetch-registry: false #如果为false 表示自己是注册中心
    serviceUrl:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
      #单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      #集群关联
      #defaultZone: http://7001的服务器ip地址:7001/eureka/,http://7002的服务器ip地址:7002/eureka/
      #(7003——关联7002.7001 注册中心集群)

3.三个eureka子项目 启动类添加 注解

 1 package com.puyu.springcloud;
 2 
 3 
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 7 
 8 @SpringBootApplication
 9 //EnableEurekaServer 服务端启动类,可以接受别人注册进来
10 @EnableEurekaServer
11 public class EurekaService_7001 {
12 
13     public static void main(String[] args) {
14         SpringApplication.run(EurekaService_7001.class,args);
15     }
16 }

provider 8002 的application.yml 配置

 1 server:
 2   port: 8002
 3 
 4   #mybatis 配置
 5 mybatis:
 6   type-aliases-package: com.puyu.springcloud.pojo
 7   config-location: classpath:mybatis/mybatis-config.xml
 8   mapper-locations: classpath:mybatis/mapper/*.xml
 9 
10   #spring配置
11 spring:
12   application:
13     name: springcloud-provider-dept # 3个服务名字一致
14   datasource:
15     type: com.alibaba.druid.pool.DruidDataSource #阿里巴巴德鲁伊
16     driver-class-name: org.gjt.mm.mysql.Driver
17     url: jdbc:mysql://localhost:3306/db02?useUnicode=true&characterEncoding=utf-8
18     username: root
19     password: root
20 
21 #eureka 配置   #defaultZone ---发布到那个注册中心
22 eureka:
23   client:
24     serviceUrl:
25       defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
26   instance:
27     instance-id: springcloud-provider-dept8002 #修改eureka 上的默认描述信息
28 
29 #info 自定义配置信息
30 info:
31   app.name: huangpuyu-springcloud # 显示与springcloud-provider-dept8001 默认描述信息  点击后项目描述
32   company.name: big.pig.com

详细springcloud-provider-dept-8001 具体业务 请看

https://www.cnblogs.com/Runawayprogrammer/p/13929355.html

 

 

 eureka 集群 7001 访问结果

 

 

 

 至此 简单的eureka项目集群搭建完毕,再从consumer 层去访问provider业务层时,三个eureka注册中心都 有8001的注册。如果三个集群有意外宕机,存活的注册中心还能继续完成业务功能。

 

posted @ 2020-11-14 17:00  RunawayProgrammer  阅读(193)  评论(0编辑  收藏  举报