Eureka--學(xué)習(xí)筆記(1)
目錄
Spring Cloud簡(jiǎn)介
--Eureka
操作步驟
--Eureka Server
----1、新建一個(gè)工程作為Eureka Server
----2、如何運(yùn)行eurekaServer
--Eureka Client
----1蕊温、建一個(gè)工程作為Eureka Client近速。
----2粘招、如何運(yùn)行eureka client藻雌,并注冊(cè)到eureka server
狀態(tài)頁(yè)面與健康顯示
Eureka的健康檢測(cè)
如何改變eureka實(shí)例ID
Spring Cloud簡(jiǎn)介
Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn)怀喉,斷路器,智能路由船响,微代理躬拢,控制總線)躲履。分布式系統(tǒng)的協(xié)調(diào)導(dǎo)致了樣板模式, 使用Spring Cloud開發(fā)人員可以快速地支持實(shí)現(xiàn)這些模式的服務(wù)和應(yīng)用程序。他們將在任何分布式環(huán)境中運(yùn)行良好聊闯,包括開發(fā)人員自己的筆記本電腦工猜,裸機(jī)數(shù)據(jù)中心,以及Cloud Foundry等托管平臺(tái)菱蔬。
-
Eureka
操作步驟
Eureka Server
1篷帅、新建一個(gè)工程作為Eureka Server
創(chuàng)建完成后的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.maozi.springcloud</groupId>
<artifactId>eureka-demo-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-demo-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.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.RC2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
2、如何運(yùn)行eurekaServer
eureka server的例子:
啟動(dòng)類配置:
@SpringBootApplication
@EnableEurekaServer
public class EurekaDemoServerApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(EurekaDemoServerApplication.class).web(true).run(args);
}
}
eureka server有一個(gè)帶有網(wǎng)頁(yè)的鏈接地址
yml文件的配置:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
healthcheck:
enable: true
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaulZone: http://${eureka.instance.hostname}:{server.port}/eureka
啟動(dòng)以后汗销,在瀏覽器中輸入http://localhost:8761/就能看的如下界面
Eureka Client
1犹褒、接下來,再建一個(gè)工程作為Eureka Client弛针。
創(chuàng)建完成后的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.maozi</groupId>
<artifactId>eureka-demo-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-demo-client</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.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.RC2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
2叠骑、如何運(yùn)行eureka client,并注冊(cè)到eureka server
當(dāng)客戶端用Eureka注冊(cè)的時(shí)候削茁,它提供自身的元數(shù)據(jù)宙枷,比如主機(jī)、端口茧跋、url慰丛、主頁(yè)地址等等。Eureka接收某個(gè)服務(wù)下所有實(shí)例的心跳消息瘾杭。如果心跳檢測(cè)失斪绮 (超過配置的超時(shí)時(shí)間)了,那么這個(gè)實(shí)例通常會(huì)被從注冊(cè)列表中刪除粥烁。
eureka client例子
啟動(dòng)類:
@SpringBootApplication
@RestController
public class EurekaDemoClientApplication {
@RequestMapping("/")
public String home(){
return "Hello world";
}
public static void main(String[] args) {
new SpringApplicationBuilder(EurekaDemoClientApplication.class).web(true).run(args);
}
}
yml的配置:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8081
spring:
application:
name: eureka-demo-client-1
啟動(dòng)server和client端后,找開http://localhost:8761/
在Application中就可以看到注冊(cè)上去的client
啟動(dòng)后,觀察兩邊的啟動(dòng)日志
狀態(tài)頁(yè)面與健康顯示
Eureka實(shí)例的狀態(tài)頁(yè)面和健康指示器默認(rèn)情況下分別用"/info"和"/health"代表贤笆。當(dāng)然這個(gè)是可以改的。默認(rèn)就像下面這樣
yml文件的配置:
eureka:
instance:
statusPageUrlPath:${management.context-path}/info
healthCheckUrlPath:${management.context-path}/health
Eureka的健康檢測(cè)
默認(rèn)情況下讨阻,Eureka用客戶端心跳檢測(cè)來判斷一個(gè)客戶端是否活著芥永。除非有特殊情況,否則這個(gè)客戶端不會(huì)傳播這個(gè)應(yīng)用的健康檢查狀態(tài)給每個(gè)Spring Boot監(jiān)控器钝吮。這就意味著在注冊(cè)成功以后埋涧,將總是宣稱這個(gè)應(yīng)用是"UP"狀態(tài)。這個(gè)行為可以被Eureka健康檢查改變奇瘦,這種改變的結(jié)果就是傳播應(yīng)用的狀態(tài)給Eureka
yml文件的配置:
eureka:
client:
healthcheck:
enabled: true
如何改變eureka實(shí)例ID
默認(rèn)的實(shí)例ID的格式是這樣的:
${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}}}
當(dāng)然棘催,可以使用eureka.instance.instanceId覆蓋這種默認(rèn)的設(shè)置