spring cloud(Greenwich SR)- Eureka
2024-08-31 23:01:50
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
完整代码:
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
启动服务
最新文章
- Unity3D 模型导入Error
- Django调用JS、CSS、图片等静态文件
- des加密解密——java加密,php解密
- 【mysql】关于硬件方面的一些优化
- 提取Windows用户密钥文件cachedump
- 2.C#中泛型在方法Method上的实现
- bzoj1564: [NOI2009]二叉查找树
- C#Log4net日志记录组件的使用
- iOS在UITableViewController里使用UISearchDisplayController报错";[UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath:]";
- 工欲善其事必先利其器---SQL在线可视化模型设计,(还可学习拖拽知识)
- Windows XP密钥(共38枚)
- Asp.Net MVC路由调试好帮手RouteDebugger
- cookie笔记(一)
- day10 while else continue break
- [转]centos7指定yum安装软件路径
- 纪念L.A. Zadeh教授
- dubbo系列四、dubbo服务暴露过程源码解析
- windows下怎么生成github的ssh公钥
- [CF917D]Stranger Trees[矩阵树定理+解线性方程组]
- P2455 [SDOI2006]线性方程组(real gauss)
热门文章
- 【linux】【Python】python2.7安装pip9.0.1
- FPGA、GPU、CPU三者各自的优缺点是什么呢?
- spring基础学习01
- Python学习笔记整理总结【Django】:Form组件
- visual c++.net 技术内幕 第6版 附带的程序如何在vs2013中编译成功
- wordpress发送邮件
- R-forestplot包| HR结果绘制森林图
- JVM类加载过程与双亲委派模型
- .NET Core 微信公众号小程序6种获取UnionID方法,你知道哪几种?
- (未完)经典Web漏洞实战演练靶场笔记