微服务(一种架构风格,而不是一个组件或者一种框架)
- 一系列微小的服务共同组成
- 跑在自己的进程里
- 每个服务为独立的业务开发
- 独立部署
- 分布式的管理
单体架构(所有功能都打包到一个war包中,在一个web容器中,公用一个DB)
优点:
- 容易测试
- 容易部署
缺点:
- 开发效率低
- 代码维护难
- 部署不灵活(构建时间长)
- 稳定性不高
- 扩展性不高
单体应用(Monolithic) VS 微服务
微服务必然是分布式的
分布式定义
支持应用程序和服务的开发,可以利用物理架构 由多个自治的处理元素(分工互不干扰的节点),不共享主内存,但通过网络发送消息合作.
微服务架构的基础框架/组件
- 服务注册发现
- 服务网关(连接内外的大门 暴露给前台所需要的服务 所有的请求都会经过网关 对外屏蔽后台服务的细节)
- 后端通用服务(也称中间层服务) 注册到服务注册表,被前端服务调用
- 前端服务(也称边缘服务) 对请求和响应进行聚合和裁剪
Spring cloud 是一个开发工具集,简化了分布式开发.
Spring cloud 和 Spring Boot的版本对应
https://projects.spring.io/spring-cloud/
Spring Cloud Eureka
两个组件组成:Eureka Server 注册中心
Eureka Client 服务注册
配置
Eureka_Server
导入Eureka Server包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
application.yml
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
启动类加@EnableEurekaServer注解
Eureka_Client
导入Web包,Eureka Discorvery包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8763
spring:
application:
name: service-hi
启动类加入@EnableEurekaClient 注解
Eureka的高可用
两个eureka_server相互注册
eureka_client注册两个eureka_server
两个以上eureka_server两两相互注册