标签:eureka relative client ping 实例 pre mave zone integer
一、环境搭建
pom.xml文件分析:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.1.9.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.angei</groupId> 12 <artifactId>eurekaserver</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>eurekaserver</name> 15 <description>Demo project for Spring Boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 <spring-cloud.version>Greenwich.SR3</spring-cloud.version> 20 </properties> 21 22 <dependencies> 23 <dependency> 24 <groupId>org.springframework.cloud</groupId> 25 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 26 </dependency> 27 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-test</artifactId> 31 <scope>test</scope> 32 </dependency> 33 </dependencies> 34 35 <dependencyManagement> 36 <dependencies> 37 <dependency> 38 <groupId>org.springframework.cloud</groupId> 39 <artifactId>spring-cloud-dependencies</artifactId> 40 <version>${spring-cloud.version}</version> 41 <type>pom</type> 42 <scope>import</scope> 43 </dependency> 44 </dependencies> 45 </dependencyManagement> 46 47 <build> 48 <plugins> 49 <plugin> 50 <groupId>org.springframework.boot</groupId> 51 <artifactId>spring-boot-maven-plugin</artifactId> 52 </plugin> 53 </plugins> 54 </build> 55 56 </project>
如果下载失败,可以在pom.xml中添加如下配置,使其从国内阿里云镜像中下载相关内容。
1 <repositories> 2 <repository> 3 <id>aliyun</id> 4 <name>aliyun</name> 5 <url>https://maven.aliyun.com/repository/public</url> 6 </repository> 7 </repositories>
添加application.yml
1 server: 2 port: 8761 3 eureka: 4 instance: 5 appname: provider-service 6 hostname: localhost 7 client: 8 service-url: 9 defaultZone: 10 http://localhost:8761/eureka/ 11 register-with-eureka: false 12 fetch-registry: false
在EurekaserverApplication.java中添加如下标签:@EnableEurekaServer
运行:
Eureka客户端开发要点
◆maven 依赖 spring-cloud-starter-netflix-eureka-client
◆application.yml 配置 eureka.client.service-url.defaultZone
◆入口类増加 @EnableEurekaClient
pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.1.9.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.angei</groupId> 12 <artifactId>eureka-client</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>eureka-client</name> 15 <description>Demo project for Spring Boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 <spring-cloud.version>Greenwich.SR3</spring-cloud.version> 20 </properties> 21 22 <dependencies> 23 <dependency> 24 <groupId>org.springframework.boot</groupId> 25 <artifactId>spring-boot-starter-web</artifactId> 26 </dependency> 27 <dependency> 28 <groupId>org.springframework.cloud</groupId> 29 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 30 </dependency> 31 32 <dependency> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-starter-test</artifactId> 35 <scope>test</scope> 36 </dependency> 37 </dependencies> 38 39 <dependencyManagement> 40 <dependencies> 41 <dependency> 42 <groupId>org.springframework.cloud</groupId> 43 <artifactId>spring-cloud-dependencies</artifactId> 44 <version>${spring-cloud.version}</version> 45 <type>pom</type> 46 <scope>import</scope> 47 </dependency> 48 </dependencies> 49 </dependencyManagement> 50 51 <build> 52 <plugins> 53 <plugin> 54 <groupId>org.springframework.boot</groupId> 55 <artifactId>spring-boot-maven-plugin</artifactId> 56 </plugin> 57 </plugins> 58 </build> 59 60 </project>
添加application.xml
1 server: 2 port: 80 3 spring: 4 application: 5 name: order-service 6 eureka: 7 client: 8 service-url: 9 defaultZone: 10 http://localhost:8761/eureka/
在EurekaClientApplication.java中添加标签:@EnableEurekaClient
写一个测试:
1 package com.angei.eurekaclient.Controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.GetMapping; 5 import org.springframework.web.bind.annotation.PathVariable; 6 import org.springframework.web.bind.annotation.ResponseBody; 7 8 @Controller 9 public class orderController { 10 11 @GetMapping("/order/{id}") 12 @ResponseBody 13 public String findById(@PathVariable("id") Integer orderId){ 14 if(orderId==2019){ 15 return "{\"Id\":1,\"Title\":\"饿了么订单\"}"; 16 }else{ 17 return null; 18 } 19 } 20 }
先启动服务器,再启动客户端:
Eureka名词概念
◆Register -服务注册,向Eureka进行注册登记
◆Renew -服务续约,30秒/次心跳包健康检查.90秒未收到剔除服务
◆Fetch Registries -获取服务注册列表,获取其他微服务地址
◆Cancel -服务下线,某个微服务通知注册中心暂停服务
◆Eviction -服务剔除,90秒未续约,从服务注册表进行剔除
Eureka自我保护机制
◆Eureka在运行期去统计心跳失败率在15分钟之内是否低于85%
◆如果低于85%,会将这些实例保护起来,让这些实例不会被剔除
◆关闭自我保护:eureka.server.enable-self-preservation: false
◆PS:如非网络特别不稳定,建议关闭
测试:
关闭客服端,90秒后:
标签:eureka relative client ping 实例 pre mave zone integer
原文地址:https://www.cnblogs.com/tubeWang/p/11627853.html