# SpringCloudStudy-Practical
本项目是SpringCloud实战学习,集成了eureka、fegin、zuul、分布式事务tx-lcn、swagger、Spring Security、SpringBoot Admin、Oauth2(redis和mysql两种存储方式)、SpringCloud Config、SpringCloud Bus
项目分为两部分,服务端springcloudserver和客户端springcloudclient
------
GitHub地址: [https://github.com/wangliu1102/SpringCloudStudy-Practical](https://github.com/wangliu1102/SpringCloudStudy-Practical.git)
#
# 具体配置文件,参考项目源码
服务端和客户端分开
# 父类
最开始导入如下依赖:
```
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
<spring-boot.version>2.1.6.RELEASE</spring-boot.version>
</properties>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<!--finalName填入父项目名称 springcloudserver/springcloudclient-->
<finalName>springcloudserver</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
```
# Eureka
Eureka是什么:Eureka是Netflix的一个子模块,也是核心模块之一。Eureka是一个基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移。Netflix在设计Eureka时遵守的就是AP原则。服务注册与发现对于微服务架构来说是非常重要的,有了服务发现与注册,只需要使用服务的标识符,就可以访问到服务,而不需要修改服务调用的配置文件了。功能类似于dubbo的注册中心,比如Zookeeper。
Eureka的基本架构:
- Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务注册和发现(请对比Zookeeper)。
- Eureka 采用了 C-S 的设计架构。Eureka Server 作为服务注册功能的服务器,它是服务注册中心。
- 而系统中的其他微服务,使用 Eureka 的客户端连接到 Eureka Server并维持心跳连接。这样系统的维护人员就可以通过 Eureka Server 来监控系统中各个微服务是否正常运行。SpringCloud 的一些其他模块(比如Zuul)就可以通过 Eureka Server 来发现系统中的其他微服务,并执行相关的逻辑。
请注意和Dubbo的架构对比
![img](2c2154f6-4be6-4bb8-9275-0c4fe7e11497/128/index_files/31f40d44-80d8-43a2-ae97-dade08bf1dce.jpg)![img](2c2154f6-4be6-4bb8-9275-0c4fe7e11497/128/index_files/98639013-b79b-4811-a640-d48e4f6c8f2e.jpg)Eureka包含两个组件:Eureka Server和Eureka Client
Eureka Server提供服务注册服务,各个节点启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。
Eureka Client是一个Java客户端,用于简化Eureka Server的交互,客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳(默认周期为30秒)。如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,EurekaServer将会从服务注册表中把这个服务节点移除(默认90秒)。
三大角色:
- Eureka Server 提供服务注册和发现;
- Service Provider服务提供方将自身服务注册到Eureka,从而使服务消费方能够找到;
- Service Consumer服务消费方从Eureka获取注册服务列表,从而能够消费服务。
**eureka自我保护**
故障现象:
![img]( 2c2154f6-4be6-4bb8-9275-0c4fe7e11497/128/index_files/22536531-7db0-48a2-af56-9246f8545920.png)
![img](2c2154f6-4be6-4bb8-9275-0c4fe7e11497/128/index_files/1cbc4511-a3fa-4ad9-a1d2-6f52d69d0a28.jpg)
导致原因:一句话:某时刻某一个微服务不可用了,eureka不会立刻清理,依旧会对该微服务的信息进行保存。
自我保护模式:默认情况下,如果EurekaServer在一定时间内没有接收到某个微服务实例的心跳,EurekaServer将会注销该实例(默认90秒)。但是当网络分区故障发生时,微服务与EurekaServer之间无法正常通信,以上行为可能变得非常危险了——因为微服务本身其实是健康的,此时本不应该注销这个微服务。Eureka通过“自我保护模式”来解决这个问题——当EurekaServer节点在短时间内丢失过多客户端时(可能发生了网络分区故障),那么这个节点就会进入自我保护模式。一旦进入该模式,EurekaServer就会保护服务注册表中的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务)。当网络故障恢复后,该Eureka Server节点会自动退出自我保护模式。
在自我保护模式中,Eureka Server会保护服务注册表中的信息,不再注销任何服务实例。当它收到的心跳数重新恢复到阈值以上时,该Eureka Server节点就会自动退出自我保护模式。它的设计哲学就是宁可保留错误的服务注册信息,也不盲目注销任何可能健康的服务实例。一句话讲解:好死不如赖活着。
综上,自我保护模式是一种应对网络异常的安全保护措施。它的架构哲学是宁可同时保留所有微服务(健康的微服务和不健康的微服务都会保留),也不盲目注销任何健康的微服务。使用自我保护模式,可以让Eureka集群更加的健壮、稳定。
在Spring Cloud中,可以使用eureka.server.enable-self-preservation = false 禁用自我保护模式。
## 1、服务端
父项目下新建子Module,名称为eureka-server
导入相关依赖
```
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
```
开启注解
```
@EnableEurekaServer
```
![img](/a6a8e6ec-bf20-4a66-9d7b-4fbd37fb1d26/128/index_files/27c09413-3aa8-4938-8aee-ee718121f6cc.png)
使用多个配置文件启动多个eureka服务,需要设置配置文件名称,把共有部分放在application.yml中,独有部分放在自己文件中,并在启动参数Program arguments中设置
```
--spring.profiles.active=eureka7002
```
![img](/a6a8e6ec-bf20-4a66-9d7b-4fbd37fb1d26/128/index_files/aac61653-c86a-40a1-9b37-cd5a0d10d9f1.png)
新建配置文件application.yml、application-eureka7001、application-eureka7002.yml
这里我需要两个eureka服务端,所
没有合适的资源?快使用搜索试试~ 我知道了~
SpringCloud实战学习,本项目是SpringCloud实战学习,集成了eureka、fegin、zuul.zip
共1133个文件
java:342个
png:144个
js:122个
需积分: 5 0 下载量 195 浏览量
2024-02-23
12:33:07
上传
评论
收藏 9.96MB ZIP 举报
温馨提示
SpringCloud实战学习,本项目是SpringCloud实战学习,集成了eureka、fegin、zuul
资源推荐
资源详情
资源评论
收起资源包目录
SpringCloud实战学习,本项目是SpringCloud实战学习,集成了eureka、fegin、zuul.zip (1133个子文件)
assumedExternalFilesCache 4B
StudentServcieImpl.class 3KB
StudentController.class 3KB
Student.class 3KB
Course.class 2KB
CourseServiceImpl.class 2KB
SwaggerConfig.class 2KB
CourseController.class 2KB
CorsConfig.class 2KB
CourseFeginFallbackFactory.class 1KB
CourseFeginFallbackFactory$1.class 1KB
WebFilter.class 1011B
StudentApplication.class 1004B
CourseFeginFallback.class 940B
CourseApplication.class 870B
ZuulApplication.class 856B
EurekaServerApplication.class 806B
LcnTmApplication.class 779B
EurekaClientApplication.class 731B
CourseFegin.class 568B
IStudentService.class 433B
ICourseService.class 381B
StudentMapper.class 312B
CourseMapper.class 309B
style.css 134KB
bootstrap.min.css 118KB
bootstrap.min.css 115KB
layui.css 87KB
notice.css 50KB
font-awesome.css 37KB
font-awesome.css 32KB
bootstrap-rtl.css 31KB
font-awesome.min.css 30KB
font-awesome.min.css 26KB
style2.0.css 25KB
formSelects-v4.css 18KB
admin.css 18KB
common.css 15KB
layer.css 14KB
layer.css 14KB
chosen.css 12KB
dropdown.css 10KB
layui.mobile.css 10KB
laydate.css 8KB
laydate.css 7KB
bootstrap-table.css 7KB
zTreeStyle.css 6KB
bootstrap-table.min.css 6KB
theme-white.css 5KB
layer.css 5KB
theme-red-white.css 5KB
theme-red.css 5KB
theme-red-dark.css 5KB
theme-green-dark.css 5KB
theme-green.css 5KB
theme-cyan.css 5KB
theme-blue.css 5KB
theme-blue-white.css 5KB
theme-blue-side.css 5KB
dtreefont.css 4KB
comm.css 4KB
city-picker.css 3KB
sign_in.css 3KB
dtree.css 3KB
theme-black.css 2KB
demo.css 2KB
login.css 2KB
stylelogin.css 1KB
custom.css 1KB
step.css 1KB
code.css 1KB
_fstyle.css 975B
bootstrap-treetable.css 923B
bootstrapValidator.min.css 694B
pagination.css 656B
webuploader.css 554B
webuploader.css 515B
treetable.css 294B
modelManage.css 246B
variablesAndContainers.dat 129B
course-8003.mv.db 28KB
student-8002.mv.db 24KB
microservicecloud-config-atguigu-dev-8002.mv.db 20KB
index.db 0B
fontawesome-webfont.eot 162KB
fontawesome-webfont.eot 67KB
iconfont.eot 40KB
glyphicons-halflings-regular.eot 20KB
dtreefont.eot 19KB
iconfont.eot 2KB
externalFilesCache 4B
ic_loading.gif 14KB
boy.gif 13KB
59.gif 10KB
22.gif 10KB
girl.gif 9KB
24.gif 8KB
13.gif 7KB
16.gif 7KB
39.gif 6KB
共 1133 条
- 1
- 2
- 3
- 4
- 5
- 6
- 12
资源评论
Kwan的解忧杂货铺
- 粉丝: 1w+
- 资源: 3651
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功