前言
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文件
4.0.0
com.joychen
eurekajuqun
0.0.1-SNAPSHOT
jar
eurekajuqun
eureka ju qun
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
UTF-8
UTF-8
1.8
Finchley.M8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
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 {
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{
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 {
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