前言
spring-cloud为基础的微服务架构,所有的微服务都需要注册到注册中心,如果这个注册中心阻塞或者崩了,那么整个系统都无法继续正常提供服务,所以,这里就需要对注册中心进行集群,换言之,高可用(HA)
一、在本地布署的机器上修改hosts文件
这个案例中主要实现两个注册中心的集群。所以在hosts中加上两个解析。
在hosts加上
127.0.0.1 eureka-server1
127.0.0.1 eureka-server2
如下修改
mac下修改:
sudo vim /etc/hosts
windows下修改
C:\Windows\System32\drivers\etc\hosts
用记事本打开在后面加上
127.0.0.1 eureka-server1
127.0.0.1 eureka-server2
二、这里用idea演示创建项目。
集群中项目代码都是一样的。只是配制文件不一样。
1.步骤一
2.步骤二
3.步骤三
4.步骤四
5.步骤五
三、实现Eureka项目代码.
项目结构
1、pom.xml文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.joychen</groupId>
<artifactId>eurekajuqun</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eurekajuqun</name>
<description>eureka ju qun</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.M8</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
2、EurekajuqunApplication.java启动项目文件
package com.joychen.eurekajuqun;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekajuqunApplication {
public static void main(String[] args) {
SpringApplication.run(EurekajuqunApplication.class, args);
}
}
3、三个监听文件之1
1.心跳检测注册中心是否挂了
package com.joychen.eurekajuqun.listener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRenewedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
/**
* joychen
*/
@Configuration
public class InstanceRenewListener implements ApplicationListener<EurekaInstanceRenewedEvent> {
private static final Logger LOGGER = LoggerFactory.getLogger(InstanceRenewListener.class);
@Override
public void onApplicationEvent(EurekaInstanceRenewedEvent event) {
LOGGER.info("心跳检测服务:{}serverid="+event.getServerId() ,event.getInstanceInfo().getAppName());
}
}
4、三个监听文件之2
2.监控是否有服务注册过来
package com.joychen.eurekajuqun.listener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRegisteredEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
/**
* joychen
*/
@Configuration
public class InstanceRegisterListener implements ApplicationListener<EurekaInstanceRegisteredEvent>{
private static final Logger LOGGER = LoggerFactory.getLogger(InstanceRegisterListener.class);
@Override
public void onApplicationEvent(EurekaInstanceRegisteredEvent eurekaInstanceRegisteredEvent) {
LOGGER.info("服务:,注册成功了appname="+eurekaInstanceRegisteredEvent.getInstanceInfo().getAppName(),eurekaInstanceRegisteredEvent.getInstanceInfo().getAppName());
}
}
5、三个监听文件之3
3.监控是否有服务挂了
package com.joychen.eurekajuqun.listener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
/**
* joychen
*/
@Configuration
public class InstanceCancelListener implements ApplicationListener<EurekaInstanceCanceledEvent> {
private static final Logger LOGGER = LoggerFactory.getLogger(InstanceCancelListener.class);
@Override
public void onApplicationEvent(EurekaInstanceCanceledEvent event) {
LOGGER.info("服务:挂了serverid:"+event.getServerId(),event.getAppName());
}
}
6、配制文件
这里有三个配制文件:
第一个applicaiotn.yml
用java启动 jar 时,不带任何参数时调用这个
server:
port: 9010
spring:
application:
name: eureka-server
eureka:
instance:
hostname: eureka-server1
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
#### 清理间隔(单位毫秒,默认是60*1000),开发环境设置如下可快速移除不可用的服务
eviction-interval-timer-in-ms: 5000
enable-self-preservation: false
第二个applicaiotn-server1.yml
用java启动 jar 时,带参数如下行进会调用这个文件
java -Xmx1024m -Xms1024m -jar target/eurekajuqun-0.0.1-SNAPSHOT.jar --spring.profiles.active=server1
server:
port: 9010
spring:
application:
##name必须一样,不然高可用会导致unavailable-replicas
name: eureka-server
eureka:
instance:
hostname: eureka-server1
client:
##这里要覆盖application的设置,不然会读取application文件导致unavailable-replicas
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka-server2:9011/eureka/
第三个applicaiotn-server2.yml
用java启动 jar 时,带参数如下行进会调用这个文件
java -Xmx1024m -Xms1024m -jar target/eurekajuqun-0.0.1-SNAPSHOT.jar --spring.profiles.active=server2
server:
port: 9011
spring:
application:
##name必须一样,不然高可用会导致unavailable-replicas
name: eureka-server
eureka:
instance:
hostname: eureka-server2
client:
##这里要覆盖application的设置,不然会读取application文件导致unavailable-replicas
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka-server1:9010/eureka/
到这整个项目就完了。
对于启动项目,如果在linux跟mac我写了两个启动文件
把两个文件给上执行权限:
apple$ sudo chmod +x *.sh
执行
./server1.sh
./server2.sh
最后上代码:
链接:https://pan.baidu.com/s/1_QLZRPhEKpq-PDVlsWgj3A 密码:3b0w