Eureka組件
- server 提供服務(wù)發(fā)現(xiàn)的能力它會(huì)儲(chǔ)存各個(gè)微服務(wù)啟動(dòng)時(shí)向自己注冊(cè)的信息(ip,port,微服務(wù)名稱)。如果在一定時(shí)間內(nèi)沒(méi)有收到某個(gè)微服務(wù)的心跳,則會(huì)注銷(xiāo)該服務(wù)(默認(rèn)90s),如果由于網(wǎng)絡(luò)原因大部分服務(wù)同時(shí)宕掉,server則會(huì)啟動(dòng)自我保護(hù)機(jī)制,不注銷(xiāo),從而保證高可用是一種應(yīng)對(duì)網(wǎng)絡(luò)異常的保護(hù)措施。默認(rèn)情況下server也是client,多個(gè)實(shí)例之間互相復(fù)制注冊(cè),保證每個(gè)server的數(shù)據(jù)同步
- client 是一個(gè)java客戶端,服務(wù)啟動(dòng)后會(huì)周期性向server發(fā)送心跳(默認(rèn)30s)
-
架構(gòu)圖image.png
編寫(xiě)server
- 默認(rèn)使用spring boot2.1 spring cloud Finchley.RC1 版本 導(dǎo)入maven依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
- @EnableEurekaServer 聲明這是個(gè)Eureka Server
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
- application.yml 配置文件
server:
port: 8000
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
- 打開(kāi)瀏覽器輸入http://localhost:8000/ 查看 系統(tǒng)狀態(tài),注冊(cè)的實(shí)例
編寫(xiě)client服務(wù)把微服務(wù)注冊(cè)到 server
- 依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- @EnableDiscoveryClient 支持更多的服務(wù)組件 如zookeeper 等 也可以換成@EnableEurkaClient
@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class SpringcloudClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudClientApplication.class, args);
}
@RequestMapping("/hello")
public String hello(){
return "hello client";
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String home(@RequestParam String name) {
return "hi "+name+",i am from port:" +port;
}
}
- 配置文件
#自己的服務(wù)中心地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8000/eureka/
server:
port: 9000
spring:
application:
name: service-hi
- 刷新server 的頁(yè)面可以看到注冊(cè)service-hi 服務(wù)名端口
Eureka 的集群
構(gòu)建一個(gè)雙節(jié)點(diǎn) peer1,peer2 eureka server集群
server
- 復(fù)制第一個(gè)server 修改 ArtifactId 為eureka2 或者別的區(qū)分
- 配置系統(tǒng)的host windows 路徑為c:\windows\system32\drivers\etc\host; linux MacOS 在 /etc/hosts
windows修改方法
127.0.0.1 peer1 peer2
- 修改兩個(gè)server的配置文件,讓兩個(gè)節(jié)點(diǎn)的server 互相注冊(cè)
spring:
application:
name: eureka-server2
# 三段式以上為集群的共有部分
---
#以下只要兩兩互相注冊(cè), 多個(gè)server就可以數(shù)據(jù)同步
spring:
#指定profiles 設(shè)置peer1為第一個(gè)節(jié)點(diǎn)
profiles: peer1
server:
port: 8001
eureka:
instance:
#指定profiles=peer1 時(shí),主機(jī)名就為peer1了
hostname: peer1
client:
serviceUrl:
#把自己注冊(cè)到 peer2 上
defaultZone: http://peer2:8002/eureka/
---
spring:
profiles: peer2
server:
port: 8002
eureka:
instance:
hostname: peer2
client:
serviceUrl:
#自己注冊(cè)到peer1 上
defaultZone: http://peer1:8001/eureka/
-
啟動(dòng)配置如圖,peer1,peer2修改啟動(dòng)參數(shù)對(duì)應(yīng)就可以了image.png
-
輸入peer1:8001image.png
當(dāng)客戶端注冊(cè)時(shí),serviceUrl可以指定其中一個(gè)所有集群節(jié)點(diǎn)就可以同步注冊(cè)信息,也可以用逗號(hào)隔開(kāi) 都寫(xiě)上
為 eureka server 添加用戶認(rèn)證
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
- application.yml
spring:
application:
name: eureka-server2
security:
user:
name: admin
password: admin
- 在認(rèn)證的server注冊(cè)client時(shí)這樣serviceUrl.defaultZone=http://user:pwd@host:port/eureka
- 元數(shù)據(jù)又分為兩種,標(biāo)準(zhǔn)和自定義;標(biāo)準(zhǔn)元數(shù)據(jù)指主機(jī)名,ip,端口,狀態(tài)頁(yè),健康檢查等信息,自定義元數(shù)據(jù)就需要eureka.instance.metadata-map配置,這些元數(shù)據(jù)可以在遠(yuǎn)程客戶端訪問(wèn),但一般不會(huì)改變客戶端行為,除非它知道元數(shù)據(jù)含義。


