天天看点

五分钟带你玩转springcloudNetflix(二)eureka搭建与配置

首先上代码 

EurekaServerApplication类:关键注解@EnableEurekaServer 声明为注册中心      
1. package com.cloud;
2. 
3. import org.springframework.boot.SpringApplication;
4. import org.springframework.boot.autoconfigure.SpringBootApplication;
5. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
6. 
7. 
8. @SpringBootApplication
9. @EnableEurekaServer
10. public class EurekaServerApplication {
11. 
12.     public static void main(String[] args) {
13.         SpringApplication.run(EurekaServerApplication.class, args);
14.     }
15. }      

然后pom文件:一定要看好版本,现在版本真的是乱七八糟,而且有的注解被干掉了。

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.2.5.RELEASE</version>
9. <relativePath/> <!-- lookup parent from repository -->
10. </parent>
11. <groupId>com.baocl</groupId>
12. <artifactId>eureka-consumer-feign</artifactId>
13. <version>0.0.1-SNAPSHOT</version>
14. <name>eureka-consumer-feign</name>
15. <description>Demo project for Spring Boot</description>
16. 
17. <properties>
18. <java.version>1.8</java.version>
19. <spring-cloud.version>Hoxton.SR1</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. 
28. <dependency>
29. <groupId>org.springframework.cloud</groupId>
30. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
31. </dependency>
32. 
33. <dependency>
34. <groupId>org.springframework.boot</groupId>
35. <artifactId>spring-boot-starter-test</artifactId>
36. <scope>test</scope>
37. <exclusions>
38. <exclusion>
39. <groupId>org.junit.vintage</groupId>
40. <artifactId>junit-vintage-engine</artifactId>
41. </exclusion>
42. </exclusions>
43. </dependency>
44. </dependencies>
45. 
46. <dependencyManagement>
47. <dependencies>
48. <dependency>
49. <groupId>org.springframework.cloud</groupId>
50. <artifactId>spring-cloud-dependencies</artifactId>
51. <version>${spring-cloud.version}</version>
52. <type>pom</type>
53. <scope>import</scope>
54. </dependency>
55. </dependencies>
56. </dependencyManagement>
57. 
58. <build>
59. <plugins>
60. <plugin>
61. <groupId>org.springframework.boot</groupId>
62. <artifactId>spring-boot-maven-plugin</artifactId>
63. </plugin>
64. </plugins>
65. </build>
66. 
67. </project>      

最后是最重要的配置文件application.properties 全网差不多最全的常用配置

1. spring.application.name=eureka-server
2. eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
3. server.port=1001
4. eureka.instance.hostname=localhost
5. #是否向服务注册中心注册自己
6. eureka.client.register-with-eureka=false
7. #是否检索服务   ---表示是否从注册中心拉取服务列表
8. eureka.client.fetch-registry=false
9. 
10. #是否开启自我保护模式,默认为true。
11. 
12. #默认情况下,如果Eureka Server在一定时间内没有接收到某个微服务实例的心跳,Eureka Server将会注销该实例(默认90秒)。
13. #但是当网络分区故障发生时,微服务与Eureka Server之间无法正常通信,以上行为可能变得非常危险了——因为微服务本身其实是健康的,此时本不应该注销这个微服务。
14. #Eureka通过“自我保护模式”来解决这个问题——当Eureka Server节点在短时间内丢失过多客户端时(可能发生了网络分区故障),
15. #那么这个节点就会进入自我保护模式。一旦进入该模式,Eureka Server就会保护服务注册表中的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务)。
16. #当网络故障恢复后,该Eureka Server节点会自动退出自我保护模式。
17. #综上,自我保护模式是一种应对网络异常的安全保护措施。它的架构哲学是宁可同时保留所有微服务(健康的微服务和不健康的微服务都会保留),
18. #也不盲目注销任何健康的微服务。使用自我保护模式,可以让Eureka集群更加的健壮、稳定。
19. #eureka.server.enable-self-preservation=false
20. 
21. #设置清理无效节点的时间间隔,默认60000,即是60s
22. #eureka.server.eviction-interval-timer-in-ms=5000
23. #自我保护续约百分比阀值因子。如果实际续约数小于续约数阀值,则开启自我保护
24. #eureka.server.renewalPercentThreshold = 0.85
25. #节点间连接的超时时间。
26. #eureka.server.peerNodeConnectTimeoutMs=200
27. #节点间读取信息的超时时间。
28. #eureka.server.peerNodeReadTimeoutMs=200
29. 
30. #每隔x秒更新自身缓存 作用于client从server获取缓存(1001页面跳过缓存不会生效)
31. #eureka.server.response-cache-update-interval-ms= 2000      

然后启动服务 访问配置的端口号 然后访问就ok了,出现这个界面,就代表ok啦

五分钟带你玩转springcloudNetflix(二)eureka搭建与配置

大致原理

五分钟带你玩转springcloudNetflix(二)eureka搭建与配置

spring cloud通信主要是以http请求形式进行,当服务启动后向Eureka注册,Eureka Server会将注册信息向其他Eureka Server进行同步,当服务消费者要调用服务提供者,则向服务注册中心获取服务提供者地址,然后会将服务提供者地址缓存在本地,下次再调用时,则直接从本地缓存中取,完成一次调用。 当服务注册中心Eureka Server检测到服务提供者因为宕机、网络原因不可用时,则在服务注册中心将服务置为DOWN状态,并把当前服务提供者状态向订阅者发布,订阅过的服务消费者更新本地缓存。 服务提供者在启动后,周期性(默认30秒)向Eureka Server发送心跳,以证明当前服务是可用状态。Eureka Server在一定的时间(默认90秒)未收到客户端的心跳,则认为服务宕机,注销该实例。