spring cloud study

本次学习基于spring cloud Greenwich SR1 版本

学习要点:

Spring Boot/Spring Cloud应用开发套路

  • 加依赖
  • 加注解
  • 写配置

Eureka (服务注册与发现)

Eureka是Netflix开源的服务发现组件,本身是一个基于REST的服务,包含Server和Client两部分,Spring Cloud将它集成在子项目Spring Cloud Netflix中

Eureka Server (快速入门)

遵循开发套路

  • 添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
  • 添加注解 @EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication { public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
server:
port: 8001 spring:
application:
name: microservice-discovery-eureka eureka:
client:
service-url:
#erueka server的地址,记住/eureka不要掉了
defaultZone: http://localhost:8001/eureka
# 是否从eureka server注册,这里我们选择false
fetch-registry: false
# 是否从eureka server 中拉取服务
register-with-eureka: false

启动项目,访问http://localhost:8001/

Eureka Client

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
@SpringBootApplication
@EnableDiscoveryClient
public class ProvideApplication { public static void main(String[] args) {
SpringApplication.run(ProvideApplication.class, args);
}
}

在Greenwich SR1版本中可以省略@EnableEurekaClient和@EnableDiscoveryClient注解,但为了养成好习惯,建议加上相应注解

@EnableDiscoveryClient: 可以配合不同的服务发现server 使用
@EnableEurekaClient: 只能配合 Eureka server 使用
server:
port: 9001
spring:
application:
name: microservice-provide-user
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/cloud-study?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&allowMultiQueries=true
username: root
password: root
jpa:
hibernate:
ddl-auto: update
show-sql: true eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
instance:
# 是否显示ip,如果不设置那么就会显示主机名,默认false
prefer-ip-address: true

启动项目,可以发现项目已经被注册进eureka

完整代码:

microservice-discovery-eureka

microservice-provide-user

eureka 深入

Eureka包含两个组件:Eureka Server 和 Eureka Client:

  • Eureka Server负责提供服务发现的能力,各个微服务启动时,会向Eureka Server注册自己的信息(例如IP、端口、微服务名称等)
  • Eureka Client是一个Java客户端,可以与EurekaServer交互
  • client启动后,会周期性的像server发送心跳,默认情况下 30s,如果server在一定时间内没有收到client的心跳,那么server会注销实例90s
  • Eureka Server遵循CAP原则,符合AP。eureka集群中每个节点之间都是平等状态。如果一个节点宕机,不会进行选举。因此可以很有效的保证可用性

搭建erueka集群

在host中添加

127.0.0.1 peer1 peer2

可以再microservice-discovery-eureka 的基础上修改application.yml

spring:
application:
name: microservice-discovery-eureka-cluster
eureka:
client:
serviceUrl:
defaultZone: http://peer2:8002/eureka/,http://peer1:8003/eureka/
---
spring:
profiles: peer1
server:
port: 8002
eureka:
instance:
hostname: peer1
---
spring:
profiles: peer2
server:
port: 8003
eureka:
instance:
hostname: peer2

修改microservice-provide-user的application.yml

server:
port: 9002
spring:
application:
name: microservice-provide-user
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/cloud-study?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&allowMultiQueries=true
username: root
password: root
jpa:
show-sql: true
eureka:
client:
service-url:
defaultZone: http://peer1:8002/eureka/,http://peer2:8003/eureka/
instance:
prefer-ip-address: true

启动服务

spring cloud(Greenwich SR)- Eureka的更多相关文章

  1. spring cloud(学习笔记)高可用注册中心(Eureka)的实现(二)

    绪论 前几天我用一种方式实现了spring cloud的高可用,达到两个注册中心,详情见spring cloud(学习笔记)高可用注册中心(Eureka)的实现(一),今天我意外发现,注册中心可以无限 ...

  2. Spring Cloud(十二):分布式链路跟踪 Sleuth 与 Zipkin【Finchley 版】

    Spring Cloud(十二):分布式链路跟踪 Sleuth 与 Zipkin[Finchley 版]  发表于 2018-04-24 |  随着业务发展,系统拆分导致系统调用链路愈发复杂一个前端请 ...

  3. 跟我学Spring Cloud(Finchley版)-20-Spring Cloud Config-Git仓库配置详解 原

    在跟我学Spring Cloud(Finchley版)-19-配置中心-Spring Cloud Config 一节中,已实现使用Git仓库作为Config Server的后端存储,本节详细探讨如何配 ...

  4. spring cloud(学习笔记)高可用注册中心(Eureka)的实现(一)

    最近在学习的时候,发现微服务架构中,假如只有一个注册中心,那这个注册中心挂了可怎么办,这样的系统,既不安全,稳定性也不好,网上和书上找了一会,发现这个spring cloud早就想到了,并帮我们解决了 ...

  5. Spring Cloud(十四)Config 配置中心与客户端的使用与详细

    前言 在上一篇 文章 中我们直接用了本应在本文中配置的Config Server,对Config也有了一个基本的认识,即 Spring Cloud Config 是一种用来动态获取Git.SVN.本地 ...

  6. spring cloud(学习笔记)微服务启动错误(1)

    今天下午在启动spring cloud微服务的时候,报了这个错误: Error starting ApplicationContext. To display the auto-configurati ...

  7. Spring Cloud (十五)Stream 入门、主要概念与自定义消息发送与接收

    前言 不写随笔的日子仿佛就是什么都没有产出一般--上节说到要学Spring Cloud Bus,这里发现按照官方文档的顺序反而会更好些,因为不必去后边的章节去为当前章节去打基础,所以我们先学习Spri ...

  8. Spring Cloud(十二)声名式服务调用:Feign 的使用(下)

    前言 本文是对上一篇博文的扩充,很多平时用不到的特性就开始简略一写,Spring Cloud各版本之间的差距很大的,用不到的可能下一个版本就被kill掉了.由于笔者写本文开始的时候误解了Feign的继 ...

  9. spring cloud(学习笔记) Enreka服务治理

    服务治理是微服务架构最为核心和基础的模块,主要用来实现各个微服务实例的自动化注册和发现. 记录一下服务注册中心的搭建以及高可用注册中心的实现 1.首先创建两个基础 的spring boot工程,spr ...

随机推荐

  1. thinkphp 多条件模糊搜索结果,按照最佳匹配度排序,使用LOCATE函数

    //获取筛选参数 $params = Request()->only(['keywords','brand_id', 'cat_id']); $where = "brand_id = ...

  2. ps查看图层大小快捷键

    1.图层大小尺寸的: ctrl+alt+c 2.图片大小尺寸.像素大小: ctrl+alt+i

  3. Angular template ng-template/container/content

    1. ng-template 形式:<ng-template>...</ng-template> 默认ng-template中的内容会隐藏; 可通过[ngIf]来控制内容显示隐 ...

  4. logrotate 不生效

    登录服务器查看,发现日志没有自动切割.去查看micros配置文件: [root@ecs-11-151 ~]# cat /etc/logrotate.d/micros /data/logs/*/*.lo ...

  5. python2和3区别

    核心类差异 Python3对Unicode字符的原生支持 Python2中使用 ASCII 码作为默认编码方式导致string有两种类型str和unicode,Python3只支持unicode的st ...

  6. 完美实现保存和加载easyui datagrid自定义调整列宽位置隐藏属性功能

    需求&场景 例表查询是业务系统中使用最多也是最基础功能,但也是调整最平凡,不同的用户对数据的要求也不一样,所以在系统正式使用后,做为开发恨不得坐在业务边上,根据他们的要求进行调整,需要调整最多 ...

  7. Java 学习笔记之 Sleep停止线程

    Sleep停止线程: 在Sleep状态下被interrupt,interrupted 状态会被擦除,返回false. 线程在Sleep状态下被interrupt: public class Sleep ...

  8. IDEA 学习笔记之 安装和基本配置

    安装和基本配置: 下载:https://www.jetbrains.com/idea/download/#section=windows 下载Zip安装包: 基础知识: Eclipse的工作区=IDE ...

  9. Spring Boot 2.X(二):集成 MyBatis 数据层开发

    MyBatis 简介 概述 MyBatis 是一款优秀的持久层框架,支持定制化 SQL.存储过程以及高级映射.它采用面向对象编程的方式对数据库进行 CRUD 的操作,使程序中对关系数据库的操作更方便简 ...

  10. ef core实现无感知软删除

    很多web程序一般的偶不会设计真的物理删除了. 基本上都是在在数据库加一个标记,就得当作已经删除了.同时在查询的时候,过滤已经标记删除的数据 ef core实现软删除是非常简单的,直接在OnModel ...