在微服务架构中,注册中心是一个必不可少的组件
非集群注册中心只适合本地开发使用,在生产环境必须搭建一个集群来保证高可用
Eureka的集群搭建很简单,每一台Eureka都需要在配置中指定另外N个Eureka的地址就可以
maven依赖
- springboot版本:2.0.2.RELEASE
- springcloud版本:Finchley.BUILD-SNAPSHOT
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
###项目结构
EurekaApplication.java
package zzq.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication{
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
application.yml
spring:
application:
name: eureka-server
profiles:
active: server1,server2,server3
application-server1.yml
server:
port: 8764
eureka:
instance:
hostname: server1
client:
service-url:
defaultZone: http://server2:8762/eureka/,http://server3:8763/eureka/
application-server2.yml
server:
port: 8762
eureka:
instance:
hostname: server2
client:
service-url:
defaultZone: http://server1:8764/eureka/,http://server3:8763/eureka/
application-server3.yml
server:
port: 8763
eureka:
instance:
hostname: server3
client:
service-url:
defaultZone: http://server1:8764/eureka/,http://server2:8762/eureka/
Hosts绑定
- 在同一个主机上运行多个Eureka服务器有一个问题。Netflix
code(com.netflix.eureka.cluster.PeerEurekaNodes.isThisMyUrl)过滤出同一主机上的对等网址。为了防止服务器注册为自己的对等体,但是由于他们不检查端口,因此除非Eureka主机名eureka.client.serviceUrl.defaultZone不同,否则对等意识不起作用。这种情况最有效的解决方法是为每一个服务定义唯一的主机名,然后将它们在/etc/hosts文件(或它的Windows等价物)映射到127.0.0.1
windows+R直接打开C:\WINDOWS\system32\drivers\etc目录修改hosts文件,添加固定的DNS解析
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 server1 server2 server3
项目运行
通过编译器配置三个服务,动态改变加载的文件配置
–spring.profiles.active={配置名称} 来启动不同的配置。
例如–spring.profiles.active=server1 加载application-server1.yml配置
通过运行jar文件的形式来运行多个服务
注:要使用maven的springboot插件打jar包,不能使用编译器自带的进行打jar包
java -jar discovery-cluster-0.0.1-SNAPSHOT.jar --spring.profiles.active=server1
java -jar discovery-cluster-0.0.1-SNAPSHOT.jar --spring.profiles.active=server2
运行结果
server1
server2
server3
Eureka原理:https://blog.csdn.net/qq_33594101/article/details/80431894
Eureka参数配置项详解:https://blog.csdn.net/qq_33594101/article/details/80430947
源码地址:https://github.com/philzq/Springboot-Architecture