Spring Cloud 初初初初步学习笔记(一)

Spring Cloud 初初初初步学习笔记 – Eureka(一)

初初初初步了解

Spring Cloud基于Spring Boot,为微服务提供了一整套解决方案,所以在了解Spring Cloud之前,先去了解了一下微服务架构。

什么是微服务?

了解完什么是微服务以后,就可以开始我初步的Spring Cloud学习。

首先,Spring Cloud不是一个框架,而是一系列与微服务有关框架的集合。

这个集合包含了微服务中必备的基础架构。

  • 服务治理
  • 配置管理中心
  • 网关
  • 服务容错
  • 服务消费者
  • 消息中间件
  • 分布式服务追踪
  • 安全
  • 微服务测试

附上大佬的图:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9ZVfWM1J-1591081252920)(https://img2018.cnblogs.com/blog/1607781/202001/1607781-20200105173519482-2027371257.png)]

Eureka 服务注册中心

服务注册中心是微服务架构非常重要的一个组件,在微服务架构里主要起到了协调者的一个作用。

服务注册中心的主要作用有服务发现,服务配置,服务健康监测等。

Eureka是Netflix开源的一款提供服务注册和发现的产品,它提供了完整的Service Registry和Service Discovery实现。也是springcloud体系中最重要最核心的组件之一。

服务注册:服务实例将自身服务信息注册到注册中心,这部分服务信息包括服务所在主机IP和提供服务的Port,以及暴露服务自身状态以及访问协议等信息。

Service Provider

  • 服务提供方
  • 将自身服务注册到Eureka,从而使服务消费方能够找到

Service Consumer

  • 服务消费方
  • 从Eureka获取注册服务列表,从而能够消费服务

Eureka 简单 HelloWorld实现

首先初始化一个Spring Boot项目并加上依赖

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pCro69Ek-1591081252922)(C:\Users\Y7000P\AppData\Roaming\Typora\typora-user-images\image-20200602120016637.png)]

创建完成后的目录如下图所示

然后在启动类上加上@EnableEurekaServer注解

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UXT9euN8-1591081252923)(C:\Users\Y7000P\AppData\Roaming\Typora\typora-user-images\image-20200602120346956.png)]
这里使用Eureka的独立模式运行。

介绍copy官网的:

The combination of the two caches (client and server) and the heartbeats make a standalone Eureka server fairly resilient to failure, as long as there is some sort of monitor or elastic runtime (such as Cloud Foundry) keeping it alive. In standalone mode, you might prefer to switch off the client side behavior so that it does not keep trying and failing to reach its peers.

两个缓存(客户端和服务器)和心跳的组合使一个独立的Eureka服务器对故障具有相当大的弹性,只要有某种监视器或弹性运行时(如云计算)使其保持活动状态。在独立模式下,您可能更愿意关闭客户端行为,这样它就不会一直尝试,也不会失败。

再打开配置文件,加上如下配置

spring:
  application:
    name: spring-cloud-eureka

##配置端口号
server:
 port: 8000
eureka:
  client:
    ##独立模式
   register-with-eureka: false
   fetch-registry: false
   serviceUrl:
      defaultZone: http://localhost:${server.port}/eureka/


配置完成后,启动,访问

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XJTwUgUX-1591081252927)(C:\Users\Y7000P\AppData\Roaming\Typora\typora-user-images\image-20200602143439590.png)]
HelloWorld!

简单的独立模式下的Eureka完成了。

同行模式测试

从官网copy一下

Eureka can be made even more resilient and available by running multiple instances and asking them to register with each other. In fact, this is the default behavior, so all you need to do to make it work is add a valid serviceUrl to a peer.

通过运行多个实例并要求它们彼此注册,Eureka可以变得更有弹性和可用性。实际上,这是默认的行为,所以要使它工作,您所需要做的就是向对等节点添加一个有效的serviceUrl

继续从官网copy示例

然后稍微做做配置,如下:

spring:
  application:
    name: spring-cloud-eureka

##配置端口号
server:
 port: 8000
eureka:
  client:
   ##register-with-eureka: false
   ##fetch-registry: false
   serviceUrl:
      defaultZone: http://localhost:${server.port}/eureka/

---
spring:
  profiles: peer1
eureka:
  instance:
    hostname: peer1
  client:
    serviceUrl:
      defaultZone: https://peer2/eureka/

---
spring:
  profiles: peer2
eureka:
  instance:
    hostname: peer2
  client:
    serviceUrl:
      defaultZone: https://peer1/eureka/

运行

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6OdtRT1p-1591081252929)(C:\Users\Y7000P\AppData\Roaming\Typora\typora-user-images\image-20200602145940909.png)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值