SpringCloud 02 - 微服务架构编码 构建
1. Eureka 基础知识
1.1 什么是服务治理
Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务治理。
在传统的 rpc 远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,所以需要使用服务治理,管理服务与服务之间的依赖关系,可以实现服务调用、负载均衡、容错等,实现服务发现与注册。
1.2 什么是服务注册与发现
Eureka 采用了 CS 的设计架构,Eureka Server 作为服务注册功能的服务器,它是服务注册中心。而系统中的其他微服务,使用 Eureka 的客户端连接到 Eureka Server 并维持心跳连接。这样系统的维护人员就可以通过 Eureka Server 来监控系统中各个微服务是否正常运行。
在服务注册与发现中,有一个注册中心。当服务器启动的时候,会把当前自己服务器的信息 比如 服务地址通讯地址等以别名方式注册到注册中心上。另一方(消费者 | 服务提供者),以该别名的方式去注册中心上获取到实际的服务通讯地址,然后再实现本地 RPC 调用 RPC 远程调用框架。
核心设计思想:在于注册中心,因为使用注册中心管理每个服务与服务之间的一个依赖关系(服务治理概念)。在任何 rpc 远程框架中,都会有一个注册中心(存放服务地址相关信息(接口地址))。
1.3 Eureka 两个组件:Eureka Server 和 Eureka Client
① Eureka Server 提供服务注册服务
各个微服务节点通过配置启动后,会在 EurekaServer 中进行注册,这样 EurekaServer 中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观看到。
② EurekaClient 通过注册中心进行访问
是一个 Java 客户端,用于简化 Eureka Server 的交互,客户端同时也具备一个内置的、 使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向 Eureka Server 发送心跳(默认周期为30秒)。 如果 Eureka Server 在多个心跳周期内没有接收到某个节点的心跳,Eureka Server 将会从服务注册表中把这个服务节点移除(默认90秒)。
2. 单机 Eureka 构建步骤
2.1 生成 Eureka Server 端服务注册中心
① 建Module:cloud-eureka-server7001
② 改POM
<dependencies>
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>com.janet.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
③ 写YML
server:
port: 7001
eureka:
instance:
hostname: localhsot #eureka服务端实例名称
client:
# 表示不向注册中心注册自己
register-with-eureka: false
# false 表示自己就是注册中心,职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
# 设置与 eureka Server 交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
④ 主启动
package com.janet.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @Description TODO
* @Date 2020/4/30
* @Author Janet
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class, args);
}
}
⑤ 测试
启动服务。http://localhost:7001/
结果页面:No application available没有服务被发现,因为没有注册服务进来当前不可能有服务被发现。
2.2 EurekaClient 端 cloud-provider-payment8001 将注册进 Eureka Server 成为服务提供者provider
修改 cloud-provider-payment8001:
① 添加 POM
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
② 修改 YML:添加 Eureka 配置
server:
port: 8001 #服务端口
spring:
application:
name: cloud-payment-service #服务名
datasource:
type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型
driver-class-name: org.gjt.mm.mysql.Driver #数据库驱动包
url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 1234
eureka:
client:
# 表示是否将自己注册金 EurekaServer 默认为 true
register-with-eureka: true
# 是否从 EurekaServer 抓取已有的注册信息,默认为 true,单节点无所谓,集群必须设置为 true才能配合 ribbon 使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.janet.springcloud.entities #所有entity别名所在包
③ 修改主启动 PaymentMain8001 :添加 @EnableEurekaClient
package com.janet.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @Description PaymentMain8001 主启动类
* @Date 2020/4/29
* @Author Janet
*/
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
}
}
④ 测试
启动两个服务(先启动 EurekaServer),localhost:7001:
微服务注册名配置说明:
2.3 EurekaClient 端 cloud-consumer-order80 将注册进 Eureka Server 成为服务消费者 consumer
修改 cloud-consumer-order80:
① 添加 POM
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
② 修改 application.yml:添加 Eureka 配置
server:
port: 80
spring:
application:
name: cloud-order-service #服务名
eureka:
client:
# 表示是否将自己注册金 EurekaServer 默认为 true
register-with-eureka: true
# 是否从 EurekaServer 抓取已有的注册信息,默认为 true,单节点无所谓,集群必须设置为 true才能配合 ribbon 使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
如果不想把这个服务入驻进入 Eureka,把 register-with-eureka 设置为 false 即可。
③ 修改主启动类:添加注解
package com.janet.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author janet
* @date 2020/4/29
*/
@SpringBootApplication
@EnableEurekaClient
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class, args);
}
}
④ 测试
先启动 EurekaServer,7001 服务;再启动服务提供者 provider,8001 服务....
3. 集群 Eureka 构建步骤
3.1 Eureka 集群原理:
微服务 RPC 远程服务调用最核心的是高可用,如果注册中心只有一个的话,出故障会导致整个服务环境不可用,所以要搭建 Eureka 注册中心集群,实现负载均衡 + 故障容错。
3.2 Eureka 集群环境构建步骤
① 新建 cloud-eureka-server7002
② 添加POM(参照7001)
③ 修改映射配置
找到C:\Windows\System32\drivers\etc路径下的hosts文件
修改映射配置添加hosts文件
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
④ 修改YMl(以前单机)
7001 的 application.yml:
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端实例名称
client:
# 表示不向注册中心注册自己
register-with-eureka: false
# false 表示自己就是注册中心,职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
# 设置与 eureka Server 交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://eureka7002.com:7002/eureka/
7002 的 application.yml:
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #eureka服务端实例名称
client:
# 表示不向注册中心注册自己
register-with-eureka: false
# false 表示自己就是注册中心,职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
# 设置与 eureka Server 交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://eureka7001.com:7001/eureka/
⑤ 主启动
package com.janet.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author Janet
* @date 2020/5/2
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7002 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7002.class, args);
}
}
测试:
3.3 将支付服务 8001 微服务发布到上面 2 台 Eureka 集群配置中
修改 8001 module 的 yml 文件:
service-url:
# defaultZone: http://localhost:7001/eureka 单机版
# 集群版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
3.4 将订单服务 80 微服务发布到上面 2 台 Eureka 集群配置中
同上。
3.5 测试
- 先要启动 EurekaServer,7001/7002服务
- 再要启动服务提供者provider,8001
- 再要启动消费者,80
- http://localhost/consumer/payment/get/31
3.6 支付服务提供者8001集群环境搭建
参考cloud-provider-payment8001,新建
① 添加POM (和 8001 一致)
<dependencies>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--本项目的 common 模块-->
<dependency>
<groupId>com.janet.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--web启动器 和下面的监控几乎同时出现-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--SpringBoot整合 mybatis启动器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--jdbc依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<optional>true</optional>
</dependency>
<!--SpringBoot测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
② YML(和 8001 一样,只是改了端口)
server:
port: 8002 #服务端口
spring:
application:
name: cloud-payment-service #服务名
datasource:
type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型
driver-class-name: org.gjt.mm.mysql.Driver #数据库驱动包
url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 1234
eureka:
client:
# 表示是否将自己注册金 EurekaServer 默认为 true
register-with-eureka: true
# 是否从 EurekaServer 抓取已有的注册信息,默认为 true,单节点无所谓,集群必须设置为 true才能配合 ribbon 使用负载均衡
fetch-registry: true
service-url:
# defaultZone: http://localhost:7001/eureka 单机版
# 集群版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.janet.springcloud.entities #所有entity别名所在包
③ 主启动
package com.janet.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author Janet
* @date 2020/5/3
*/
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8002 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8002.class, args);
}
}
④ 业务类
- 直接从 8001 粘
- 修改 8001/8002 的 controller
package com.janet.springcloud.controller;
import com.janet.springcloud.entities.CommonResult;
import com.janet.springcloud.entities.Payment;
import com.janet.springcloud.service.PaymentService;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.DiscoveryClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* @Description TODO
* @Date 2020/4/29
* @Author Janet
*/
@RestController
@Slf4j
public class PaymentController {
@Autowired
private PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
// @Autowired
// private DiscoveryClient discoveryClient; //服务发现
@PostMapping(value = "/payment/create")
public CommonResult<Integer> create(@RequestBody Payment payment){
int result = paymentService.create(payment);
log.info("插入结果----"+result);
if(result > 0)
return new CommonResult(200,"插入数据成功,端口号:"+serverPort,result);
else
return new CommonResult(444,"插入数据失败",null);
}
@GetMapping(value = "/payment/get/{id}")
public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
Payment payment = paymentService.getPaymentById(id);
log.info("查询结果----"+payment);
if(payment != null)
return new CommonResult(200,"查询成功,端口号:"+serverPort,payment);
else
return new CommonResult(444,"没有对应字段,查询ID"+id,null);
}
@GetMapping(value = "/payment/lb")
public String getPaymentLB() {
return serverPort;
}
}
3.7 负载均衡
① bug:订单服务访问地址不能写死(cloud2020\cloud-consumer-order80)
② 使用 @LoadBalanced 注解赋予 RestTemplate 负载均衡的能力。ApplicationContextBean:
3.8 测试
4. Actuator 微服务信息完善
4.1 主机名称:服务名称修改
① 当前问题:
② 修改cloud-provoder-payment 8001 / 8002
yaml:
server:
port: 8001 #服务端口
spring:
application:
name: cloud-payment-service #服务名
datasource:
type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型
driver-class-name: org.gjt.mm.mysql.Driver #数据库驱动包
url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 1234
eureka:
client:
# 表示是否将自己注册金 EurekaServer 默认为 true
register-with-eureka: true
# 是否从 EurekaServer 抓取已有的注册信息,默认为 true,单节点无所谓,集群必须设置为 true才能配合 ribbon 使用负载均衡
fetch-registry: true
service-url:
# defaultZone: http://localhost:7001/eureka 单机版
# 集群版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
instance:
instance-id: payment8001
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.janet.springcloud.entities #所有entity别名所在包
4.2 访问信息有 IP 信息提示
① 当前问题:没有IP提示
② 修改cloud-provoder-payment8001 的 yml 文件
5. 服务发现 Discovery
① 对于注册 eureka 里面的微服务,可以通过服务发现来获得该服务的信息
② 修改cloud-provider-payment8001的 Controller
③ 在主启动类上加注解:@EnableDiscoveryClient
④ 测试:http://localhost:8001/payment/discovery
6. Eureka 自我保护
6.1 故障现象
保护模式主要用于一组客户端和 Eureka Server 之间存在网络分区场景下的保护,一旦进入保护模式,Eureka Server 将会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据,也就是不会注销任何微服务。
一句话:某时刻一个微服务不可用了,Eureka 不会立刻清理,依旧会对该服务的信息进行保存。
属于 CAP 里面的 AP 分支
如果在 Eureka Server 的首页看到以下这段提示,则说明 Eureka 进入了保护模式:
6.1 导致原因
① 为什么会产生Eureka自我保护机制?
为了防止 EurekaClient 可以正常运行,但是 与 EurekaServer 网络不通情况下,EurekaServer 不会立刻将 EurekaClient 服务剔除。
② 什么是自我保护模式?
默认情况下,如果 EurekaServer 在一 定时间内没有接收到某个微服务实例的心跳,EurekaServer 将会注销该实例 (默认90秒)。但是当网络分区故障发生(延时、卡顿、拥挤)时,微服务与 EurekaServer 之间无法正常通信,以上行为可能变得非常危险了——因为微服务本身其实是健康的,此时本不应该注销这个微服务。Eureka 通过 “自我保护模式” 来解决这个问题——当 EurekaServer 节点在短时间内丢失过多客户端时(可能发生了网络分区故障),那么这个节点就会进入自我保护模式。
在自我保护模式中,Eureka Server 会保护服务注册表中的信息,不再注销任何服务实例。
它的设计哲学就是宁可保留错误的服务注册信息,也不盲目注销任何可能健康的服务实例。
综上,自我保护模式是一种应对网络异常的安全保护措施。 它的架构哲学是宁可同时保留所有微服务(健康的微服务和不健康的微服务都会保留)也不盲目注销任何健康的微服务。使用自我保护模式,可以让 Eureka 集群更加的健壮、 稳定。
6.3 怎么禁止自我保护
① 注册中心eurekaServer端7001
出产默认,自我保护机制是开启的:eureka.server.enable-self-preservation=true
使用 eureka.server.enable-self-preservation=false 可以禁用自我保护模式
关闭效果:
在eurekaServer端7001处设置关闭自我保护机制
② 生产者客户端eurekaClient端8001
默认:
- eureka.instance.lease-renewal-interval-in-seconds=30:单位为秒(默认是30秒)
- eureka.instance.lease-expiration-duration-in-seconds=90:单位为秒(默认是390秒)
配置:
测试:
7001和8001都配置成功,先启动7001再启动8001:
关闭 8001 后,马上被删除了
Eureka 停更:https://github.com/Netflix/eureka/wiki
SpringCloud 04 - Zookeeper