参考imooc【Spring Cloud微服务实战】
我的代码:note_springcloud
第1章 微服务介绍
微服务架构
Spring Framework > Spring Boot > Spring Cloud
什么是微服务
- 微服务是一种架构风格(restful是一种架构风格)
- 一系列微小服务,跑在自己的进程里
- 每个服务为独立的业务开发,独立部署
- 分布式管理
架构演进
ORM–MVC–RPC–SOA
单体架构
优点:
缺点:
- 开发效率低
- 代码维护困难
- 部署不灵活(构建时间长)
- 稳定性不高
- 扩展性不够(不满足高并发业务需求)
微服务架构
- 服务注册发现(微服务内部相互调用与通信)
- 服务网关(Service Gateway)(外界访问,如手机,浏览器)
- 对外屏蔽后台服务细节
- 路由:将外部请求反向路由到内部某个微服务
- 限流、容错
- 监控、日志
- 安全性:用户认证、授权、反爬虫
- 后端通用服务(中间层服务)
- 前端服务(边缘服务)
- 通过查询注册表发现、调用后端服务
- 对后端服务做必要的聚合、裁剪后暴露给外部不同设备
- 聚合:对多个api调用进行聚合,从而减少客户端请求数
- 裁剪:pc端和手机端,需要的数据可能不同(pc通常需要更为详细的数据)
微服务架构图

微服务内部的“配方”
阿里系:
- Dubbo:服务化治理
- Zookeeper:服务注册中心
- SpringMVC or SpringBoot
Spring Cloud全家桶:
Spring Cloud是什么
第2章 服务注册与发现
Spring Cloud Eureka
基于Netflix Eureka做了二次封装
两个组件组成:
版本
spring boot:2.0.7.RELEASE
spring cloud:Finchley.SR2
Eureka Server
\pom
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.7.RELEASE</version> <relativePath/> </parent>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.SR2</spring-cloud.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
<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> </dependencies> </dependencyManagement>
|
\application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13
| server: port: 8761 spring: application: name: eureka
eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ register-with-eureka: false server: enable-self-preservation: false
|
\boot
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package com.mxx.eureka;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer public class EurekaApplication {
public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
|
Eureka Client
\pom
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
|
\application.yml
1 2 3 4 5 6 7 8 9 10
| spring: application: name: client
eureka: client: service-url: defaultZone: http://localhost:8761/eureka/
|
\boot
1 2 3 4 5 6 7 8 9
| @SpringBootApplication
@EnableDiscoveryClient public class ClientApplication {
public static void main(String[] args) { SpringApplication.run(ClientApplication.class, args); } }
|
Eureka的高可用
将2个Eureka互相注册,注册信息会同步

将client注册到每一个server中,保证每次注册都成功(某个server可能挂掉)
3台eureka两两注册

1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
defaultZone: http://localhost:8762/eureka/,http://localhost:8763/eureka/
defaultZone: http://localhost:8761/eureka/,http://localhost:8763/eureka/
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
|
小结
- 心跳检测、健康检查、负载均衡
- Eureka的高可用,建议在生产中至少2台
分布式下服务注册
分布式系统为什么需要服务发现?
根据流量大小调整服务数量

引入注册中心:

注册中心是分布式系统的基础
A如何调用B?
客户端发现:轮询、随机、hash ,也就是负载均衡机制
服务端发现:代理帮a挑出一个B
- b和注册中心对a透明,a只需要找代理
- Nginx(http反向代理服务器\负载均衡器,服务发现负载均衡器)
- zookeeper
- kubernetes

微服务特点:异构
Spring Cloud的服务调用方式
- REST(Eureka) or RPC
- Node.js的eureka-js-client