Eureka服務注冊中心
一、Eureka Server
Eureka Server是服務的注冊中心,這是分布式服務的基礎,我們看看這一部分如何搭建。
首先,Spring Cloud是基於Spring Boot的,所以我們的項目都是Spring Boot項目。需要引入最基礎的Spring Boot的jar包,如下:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
然后,再引入Eureka Server的jar包:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
接下來我們編寫啟動類:
@SpringBootApplication @EnableEurekaServer public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
其中@EnableEurekaServer
標志着這個服務是一個Eureka Server。
下面就是最重要的application.yml
的配置,我們先從最簡單的單例模式說起。
單例模式
這種模式下,Eureka Server是一個單點,如果發生故障,則整個注冊中心不可用,只適用於測試環境,具體配置如下:
spring: profiles: standalone eureka: instance: hostname: localhost client: service-url: defaultZone: http://localhost:8761 fetch-registry: false register-with-eureka: false server: port: 8761
在單例模式下,我們關掉客戶端的行為。其中fetch-registry
是抓取注冊的服務,register-with-eureka
是將自己本身向其他的Eureka Server 注冊。 這兩項在集群配置時是必須打開的,這樣才能使注冊的服務同步到其他節點上。
在單例模式下,eureka.instance.hostname
必須是localhost,而且defaultZone
不能使用ip,要使用eureka.instance.hostname
且走域名解析才可以。 這里我們配置的是localhost,不需要修改hosts文件。這塊不知道為什么Spring Cloud要這么設計,小編在搭建集群時折騰了好長時間。 我們啟動服務,訪問http://localhost:8761,可以看到Eureka的管理頁面。
集群模式
我們可以通過創建多個Eureka Server實例,並讓他們相互注冊來實現集群。相互注冊就是我們前面提到的fetch-registry
和register-with-eureka
, 它們默認都是true,所以不需要配置,我們需要制定其他節點的url就可以了,我們以3個節點為例:
spring: application: name: eureka-server --- spring: profiles: peer1 eureka: instance: hostname: peer1 client: service-url: defaultZone: http://peer2:9200/eureka/,http://peer3:9300/eureka/ server: port: 9100 --- spring: profiles: peer2 eureka: instance: hostname: peer2 client: service-url: defaultZone: http://peer1:9100/eureka/,http://peer3:9300/eureka/ server: port: 9200 --- spring: profiles: peer3 eureka: instance: hostname: peer3 client: service-url: defaultZone: http://peer1:9100/eureka/,http://peer2:9200/eureka/ server: port: 9300
我們在一台機器上起3個實例,peer1、peer2、peer3,端口分別為:9100、9200、9300。 這里我們還是要注意一下instance.hostname
和service-url
- 3個實例的instance.hostname不能重復,否則集群搭建失敗
- service-url不能使用ip+端口直接訪問,否則也會失敗
在單機情況下,我們需要配置hosts來解析peer1、peer2、peer3
127.0.0.1 peer1 127.0.0.1 peer2 127.0.0.1 peer3
集群搭建成功:
DS Replicas顯示兩個副本節點,available-replicas顯示兩個可用的副本節點。
如果在真實的物理機上,我們可以不通過配置hosts的方式也是可以的,記住這是 真實的物理機的情況下, 每台機器的ip都不一樣。配置如下:
spring: application: name: eureka-server --- spring: profiles: peer1 eureka: instance: prefer-ip-address: true client: service-url: defaultZone: http://ip2:9200/eureka/,http://ip3:9300/eureka/ server: port: 9100 --- spring: profiles: peer2 eureka: instance: hostname: peer2prefer-ip-address: true client: service-url: defaultZone: http://ip1:9100/eureka/,http://ip3:9300/eureka/ server: port: 9200 --- spring: profiles: peer3 eureka: instance: prefer-ip-address: true client: service-url: defaultZone: http://ip1:9100/eureka/,http://ip2:9200/eureka/ server: port: 9300
實例的名稱可以使用ip去注冊,當然每個ip不一樣,也不會重復,不會導致失敗。
至此,我們的Eureka Server就搭建完了,具體參照GitHub地址:https://github.com/liubo-tech/spring-cloud-eureka