Eureka--学习笔记(1)
目录
Spring Cloud简介
--Eureka
操作步骤
--Eureka Server
----1、新建一个工程作为Eureka Server
----2、如何运行eurekaServer
--Eureka Client
----1、建一个工程作为Eureka Client。
----2、如何运行eureka client,并注册到eureka server
状态页面与健康显示
Eureka的健康检测
如何改变eureka实例ID
Spring Cloud简介
Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线)。分布式系统的协调导致了样板模式, 使用Spring Cloud开发人员可以快速地支持实现这些模式的服务和应用程序。他们将在任何分布式环境中运行良好,包括开发人员自己的笔记本电脑,裸机数据中心,以及Cloud Foundry等托管平台。
-
Eureka
操作步骤
Eureka Server
1、新建一个工程作为Eureka Server
创建完成后的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、如何运行eurekaServer
eureka server的例子:
启动类配置:
@SpringBootApplication
@EnableEurekaServer
public class EurekaDemoServerApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(EurekaDemoServerApplication.class).web(true).run(args);
}
}
eureka server有一个带有网页的链接地址
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
启动以后,在浏览器中输入http://localhost:8761/就能看的如下界面
Eureka Client
1、接下来,再建一个工程作为Eureka Client。
创建完成后的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、如何运行eureka client,并注册到eureka server
当客户端用Eureka注册的时候,它提供自身的元数据,比如主机、端口、url、主页地址等等。Eureka接收某个服务下所有实例的心跳消息。如果心跳检测失败(超过配置的超时时间)了,那么这个实例通常会被从注册列表中删除。
eureka client例子
启动类:
@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
启动server和client端后,找开http://localhost:8761/
在Application中就可以看到注册上去的client
启动后,观察两边的启动日志
状态页面与健康显示
Eureka实例的状态页面和健康指示器默认情况下分别用"/info"和"/health"代表。当然这个是可以改的。默认就像下面这样
yml文件的配置:
eureka:
instance:
statusPageUrlPath:${management.context-path}/info
healthCheckUrlPath:${management.context-path}/health
Eureka的健康检测
默认情况下,Eureka用客户端心跳检测来判断一个客户端是否活着。除非有特殊情况,否则这个客户端不会传播这个应用的健康检查状态给每个Spring Boot监控器。这就意味着在注册成功以后,将总是宣称这个应用是"UP"状态。这个行为可以被Eureka健康检查改变,这种改变的结果就是传播应用的状态给Eureka
yml文件的配置:
eureka:
client:
healthcheck:
enabled: true
如何改变eureka实例ID
默认的实例ID的格式是这样的:
${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}}}
当然,可以使用eureka.instance.instanceId覆盖这种默认的设置