作用
当注册中心扛不住高并发时,就需要集群了
新建Module
新建两个Module,一个名称为:microservice-eureka-server-2002,另一个名称为:microservice-eureka-server-2003
继承依赖
在新建的两个Module的pom.xml中添加:
1 2 3 4 5 6
| <parent> <groupId>org.example</groupId> <artifactId>microservice</artifactId> <version>1.0-SNAPSHOT</version> </parent>
|
添加依赖
在新建的两个Module的pom.xml中添加:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency>
|
添加启动类
在新建的两个Module都添加启动类,名称分别为:EurekaServerApplication_2002和EurekaServerApplication_2003,可以直接复制EurekaServerApplication_2001.java的内容然后修改名称即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.ledao;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication @EnableEurekaServer public class EurekaApplication_2001 {
public static void main(String[] args) { SpringApplication.run(EurekaApplication_2001.class, args); } }
|
配置本机host
前面单机的时候 eureka注册中心实例名称 是localhost,现在是集群,不能三个实例都是localhost,这里复杂的办法是搞三个虚拟机,比较麻烦,这里有简单办法,直接配置本机hosts,来实现本机域名映射;
找到 C:\Windows\System32\drivers\etc 打开hosts,加配置:
1 2 3
| 127.0.0.1 eureka2001.ledao.com 127.0.0.1 eureka2002.ledao.com 127.0.0.1 eureka2003.ledao.com
|
修改application.yml
修改三个项目的application.yml文件,主要是修改 hostname和defaultZone
microservice-eureka-server-2001的application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| server: port: 2001 context-path: /
eureka: instance: hostname: eureka2001.ledao.com client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://eureka2002.ledao.com:2002/eureka/,http://eureka2003.ledao.com:2003/eureka/
|
microservice-eureka-server-2002的application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| server: port: 2002 context-path: /
eureka: instance: hostname: eureka2002.ledao.com client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://eureka2001.ledao.com:2001/eureka/,http://eureka2003.ledao.com:2003/eureka/
|
microservice-eureka-server-2003的application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| server: port: 2003 context-path: /
eureka: instance: hostname: eureka2003.ledao.com client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://eureka2002.ledao.com:2002/eureka/,http://eureka2003.ledao.com:2003/eureka/
|
服务提供者项目microservice-student-provider-1001的application.yml,主要修改eureka.client.service-url.defaultZone
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| server: port: 1001 context-path: /
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/db_springcloud?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC username: root password: 123456 jpa: hibernate: ddl-auto: update show-sql: true
eureka: instance: hostname: localhost appname: microservice-student instance-id: microservice-student:1001 prefer-ip-address: true client: service-url: defaultZone: http://eureka2001.ledao.com:2001/eureka/,http://eureka2002.ledao.com:2002/eureka/,http://eureka2003.ledao.com:2003/eureka/ info: current.groupId: @project.groupId@ current.artifactId: @project.artifactId@ current.version: @project.version@ parent.artifactId: @project.parent.artifactId@ 负责人: 张三 联系电话: 110
|
测试
运行microservice-eureka-server-2001、microservice-eureka-server-2002、microservice-eureka-server-2003和microservice-student-provider-1001四个项目后开始测试
下面三个链接都可以测试
http://eureka2001.ledao.com:2001/
http://eureka2002.ledao.com:2002/
http://eureka2003.ledao.com:2003/
结果