一、 Eureka概述
1.创立 eureka server 我的项目:sp05-eureka
增加依赖:
2.增加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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cn.tedu</groupId> <artifactId>sp05-eureka</artifactId> <version>0.0.1-SNAPSHOT</version> <name>sp05-eureka</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR9</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> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </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></project>
3.批改application.yml
spring: application: name: eureka-server server: port: 2001 eureka: server: enable-self-preservation: false instance: hostname: eureka1 client: register-with-eureka: false fetch-registry: false
参数介绍:
eureka 集群服务器之间,通过 hostname
来辨别
eureka.server.enable-self-preservation
eureka 的自我爱护状态:心跳失败的比例,在15分钟内是否超过85%,如果呈现了超过的状况,Eureka Server会将以后的实例注册信息爱护起来,同时提醒一个正告,一旦进入保护模式,Eureka Server将会尝试爱护其服务注册表中的信息,不再删除服务注册表中的数据。也就是不会登记任何微服务
eureka.client.register-with-eureka=false
不向本身注册
eureka.client.fetch-registry=false
不从本身拉取注册信息
eureka.instance.lease-expiration-duration-in-seconds
最初一次心跳后,距离多久认定微服务不可用,默认90
4.主程序增加 @EnableEurekaServer
package cn.sp05;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublic class Sp05EurekaApplication { public static void main(String[] args) { SpringApplication.run(Sp05EurekaApplication.class, args); }}
5.批改 hosts 文件,增加 eureka 域名映射
C:\Windows\System32\drivers\etc\hosts
增加内容:
127.0.0.1 eureka1127.0.0.1 eureka2
6.启动,并拜访测试
http://eureka1:2001
二、service provider 服务提供者
批改 item-service、user-service、order-service,把微服务注册到 eureka 服务器
①:pom.xml 增加eureka依赖
②:application.yml 增加eureka注册配置
③:主程序启用eureka客户端
④:启动服务,在eureka中查看注册信息
1.pom.xml 增加 eureka 客户端依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>
<projrct>内增加springcloud版本号
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR9</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
2.application.yml 增加 eureka注册配置
eureka: client: service-url: defaultZone: http://eureka1:2001/eureka
eureka.instance.lease-renewal-interval-in-seconds
心跳间隔时间,默认 30 秒
defaultZone,默认地位,能够批改为具体地理位置,比方:beiJing, shangHai, shenZhen 等,示意 eureka 服务器的部署地位, 须要云服务器提供
eureka.client.registry-fetch-interval-seconds
拉取注册信息间隔时间,默认 30 秒
3.主程序启用服务注册发现客户端
批改 item-service、user-service 和 order-service,
主程序增加 @EnableDiscoveryClient
注解
4.启动,并拜访 eureka 查看注册信息
http://eureka1:2001
三、eureka 和 “服务提供者”的高可用
1.item-service 高可用
启动参数 --server.port 能够笼罩yml中的端口配置--server.port=8001
idea如何更换端口号启动同一个服务:
2.eureka 高可用
2.1增加两个服务器的 profile 配置文件
application-eureka1.yml:
eureka: instance: hostname: eureka1 client: register-with-eureka: true #profile的配置会笼罩专用配置 fetch-registry: true #profile的配置会笼罩专用配置 service-url: defaultZone: http://eureka2:2002/eureka #eureka1启动时向eureka2注册
application-eureka2.yml
eureka: instance: hostname: eureka2 client: register-with-eureka: true #profile的配置会笼罩专用配置 fetch-registry: true #profile的配置会笼罩专用配置 service-url: defaultZone: http://eureka1:2001/eureka #eureka2启动时向eureka1注册
2.2配置启动参数 --spring.profiles.active 和 --server.port
eureka1 启动参数:
--spring.profiles.active=eureka1 --server.port=2001
eureka2 启动参数:
--spring.profiles.active=eureka2 --server.port=2002
2.3拜访 eureka 服务器,查看注册信息
http://eureka1:2001/
http://eureka2:2002/
3.eureka客户端注册时,向两个服务器注册
批改以下微服务:
sp02-itemservicesp03-userservicesp04-orderservice
eureka: client: service-url: defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
当一个 eureka 服务宕机时,仍能够连贯另一个 eureka 服务