上一遍文章完成了,聚合父工程、消费模块、支付模块、公共模块的搭建、RestTemplate调用支付模块
该篇项目创建gitv2.0的管理分子完成学习
1、Eureka基础知识
1.1、 什么是服务治理
Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务治理
在传统的RPC远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,管理比较复杂,所以需要使用服务治理,管理服务于服务之间依赖关系,可以实现服务调用、负载均衡、容错等,实现服务发现与注册。
1.2、什么是服务注册与发现
Eureka采用了CS的设计架构,Eureka Server 作为服务注册功能的服务器,它是服务注册中心。而系统中的其他微服务,使用 Eureka的客户端连接到 Eureka Server并维持心跳连接。这样系统的维护人员就可以通过 Eureka Server 来监控系统中各个微服务是否正常运行
在服务注册与发现中,有一个注册中心。当服务器启动的时候,会把当前自己服务器的信息 比如 服务地址通讯地址等以别名方式注册到注册中心上。另一方(消费者|服务提供者),以该别名的方式去注册中心上获取到实际的服务通讯地址,然后再实现本地RPC调用RPC远程调用框架核心设计思想:在于注册中心,因为使用注册中心管理每个服务与服务之间的一个依赖关系(服务治理概念)。在任何rpc远程框架中,都会有一个注册中心(存放服务地址相关信息(接口地址))
下左图是Eureka系统架构,右图是Dubbo的架构
1.3、Eureka的两个组件
Eureka Server提供服务注册服务
各个微服务节点通过配置启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观看到。
EurekaClient通过注册中心进行访问
是一个Java客户端,用于简化Eureka Server的交互,客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳(默认周期为30秒)。如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,EurekaServer将会从服务注册表中把这个服务节点移除(默认90秒)
2、单机Eureka构建步骤
根据上左图可以将我们上篇文件搭建的项目《消费模块》与《支付模块》进行带入了解
- ServiceConsumer看作是消费模块
- ServiceProvider看作是支付模块
- 可以将IDEA看作EurekaServer的业务中心
2.1、创建cloud-eureka-server7001模块
1、修改pom.xml文件
该文件也会使用到我们公共的模块内容,需要添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud2022</artifactId>
<groupId>com.zcl.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-eureka-server7001</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.zcl.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--boot web actuator-->
<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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
2、编写YAML配置文件
下面配置里面的${xxx.xxx}表示的是获取当前配置配置文件上的配置
server:
port: 7001
eureka:
instance:
hostname: localhost # eureka服务端的实例名称
client:
register-with-eureka: false # false表示不向注册中心注册自己
fetch-registry: false # false表示自己端就是注册中心,维护服务实例
service-url:
# 设置与EurekaServer交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3、创建Eureka服务启动类
Eureak是没有业务层的,但必须使用
@EnableEurekaServer
注解标明这是一个服务中心
package com.zcl.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* 描述:服务中心启动类
*
* @author zhong
* @date 2022-09-14 17:03
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class, args);
}
}
4、启动项目访问Eureka
访问地址:http://localhost:7001/
2.2、将支付模块8081注册到Eureka成为服务提供者
1、添加客户端依赖
<!--eureka-client客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2、修改YAML配置文件
server:
port: 8081
spring:
application:
name: cloud-payment-service # 指定程序名称
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 数据源操作类型
driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/cloud2022?useUnicode=true&characterEncoding=utf-8&useSSL=false # 使用5.0mysql数据库
username: root
password: 1234
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true。
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
defaultZone: http://localhost:7001/eureka
mybatis:
mapper-locations: classpath:mapper/*.xml # 映射文件
type-aliases-package: com.zcl.springcloud.entities # 所有实体所在的包
注意:http://localhost:7001/eureka是Eureka服务地址,在7001服务中通过defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
进行指定(eureka.instance.hostname:主机名称,server.port:启动的端口号)
3、添加Eureka客户端注解到启动类上
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* 描述:项目启动类
*
* @author zhong
* @date 2022-09-14 10:43
*/
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
}
}
4、启动项目
先启动7001EurekaServer服务端
再启动8081服务提供者客户端注册到Eureka
启动结果
注意:Eureka的实例名称必须与
服务提供者的yaml配置文件指定的程序名称一致
# 8081支付模块配置文件 spring: application: name: cloud-payment-service # 指定程序名称
2.3、将订单微服务80注册到Eureka
1、引入Eureka客户端依赖
与2.2的一样依赖
2、配置YAML文件
server:
port: 80
spring:
application:
name: cloud-order-service # 程序名称
eureka:
client:
register-with-eureka: true # 表示是否将自己注册进EurekaServer服务中心,默认是true
fetchRegistry: true # 是否从EurekaServer抓取已有的注册学习,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
service-url:
defaultZone: http://localhost:7001/eureka/ # eureka地址
3、启动类添加@EnableEurekaClient注解
4、启动80消费者项目
图中出现的红色字体并不是错误,而是Eureka的一种安全保护机制
3、集群Eureka构建步骤
3.1、Eureka集群原理【互相注册、相互守望】
知识回顾:
- 服务注册:就是将服务信息注册到Eureka注册中心
- 服务发现:从服务中心上获取服务信息(实质:存key服务,取val服务调用地址)
为什么需要集群部署:
上面的步骤是单机部署,如果只有一个注册中心,并且中途挂机了,那么整个Eureka就挂机了。如果是集群部署就可以有多个Eureka注册中心,那么一个挂机还可以有其他的服务中心支持,保证项目可以正常运行
微服务RPC远程服务调用最核心的是什么?高可用
解决办法:搭建Eureka集群,实现负载均衡+故障容错
- 先启动Eureka注册中心
- 启动服务提供者payment支付服务
- 支付服务启动后会吧自身信息(比如服务地址以别名方式注册进Eureka)
- 消费者order服务在需要调用接口时,使用服务别名进行注册中心获取实际的RPC远程调用地址
- 消费者获取到调用地址后,底层实际是利用
HttpClien
技术实现远程调用 - 消费者获取服务地址后会缓存在本地NVM内存中,默认间隔30秒更新一次服务调用地址
3.3、Eureka集群环境构建
1、创建一个cloud-eureka-server7002模块
该模块的内容与
cloud-eureka-server7001
几乎一样,将启动类改为【EurekaMain7002】
2、修改系统的映射配置
找到hosts文件通过编辑器修改
添加如下配置进入hosts文件
如果是部署三台机器就以此类推
127.0.0.1 eureka7001.com 127.0.0.1 eureka7002.com
3、修改集群YAML配置
以前的单机版YAML配置
server: port: 7001 eureka: instance: hostname: localhost # eureka服务端的实例名称 client: register-with-eureka: false # false表示不向注册中心注册自己 fetch-registry: false # false表示自己端就是注册中心,维护服务实例 service-url: # 设置与EurekaServer交互的地址查询服务和注册服务都需要依赖这个地址 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
集群版YAML配置
server: port: 7001 eureka: instance: hostname: eureka7001.com # eureka服务端的实例名称 client: register-with-eureka: false # false表示不向注册中心注册自己 fetch-registry: false # false表示自己端就是注册中心,维护服务实例 service-url: # 设置与EurekaServer交互的地址查询服务和注册服务都需要依赖这个地址 defaultZone: http://eureka7002.com:7002/eureka/
试想:7001需要包含
eureka7002.com
的服务信息,那么7002就需要包含eureka7001.com
的服务信息。那么7003就要包含1、2的信息…【相互注册,互相守望】
4、启动项目测试
启动7001
启动7002
通过原来的
http://localhost:7001/
是否能进入到Eureka服务中心通过域名映射访问
5、将微服务支付模块注册到Eureka集群中
修改YAML配置Eureka部署【由单机到集群的变化】
eureka:
client:
register-with-eureka: true # 表示是否将自己注册进EurekaServer服务中心,默认是true
fetchRegistry: true # 是否从EurekaServer抓取已有的注册学习,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
service-url:
# defaultZone: http://localhost:7001/eureka/ # 单机版eureka地址
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版
6、将订单模块注册到Eureka集群中
与上面的配置一样
7、启动项目测试
- 先启动7001、7002有了注册中心后面的服务才能被注册进来
- 启动8001支付模块
- 启动80消费模块
- 查看两个服务中心是否都有【支付实例】【订单实例】
- 通过postman测试原来的80服务查询及插入是否由问题
4、支付服务提供者集群搭建
1、创建cloud-provider-payment8002
集群模块
模块内容参考cloud-provider-payment8001进行编写
1.1、修改8002的pom.xml文件
文件参考8001
1.2、复制修改8001的YAML配置文件
只需要修改端端口为8002即可
1.3、复制包机构内容以及修改8002启动类
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8002 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8002.class, args);
}
}
1.4、修改8001和8002的控制器内容如下
修改内容:
- 添加一个新的属性,通过
@Value("${server.port}")
注解绑定YAML配置文件的服务端口号- 在控制器执行返回的结果语句后添加上当前提供者的端口,方便后面查看负载均衡的效果
package com.zcl.springcloud.controller;
import com.zcl.springcloud.entities.CommonResult;
import com.zcl.springcloud.entities.Payment;
import com.zcl.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* 描述:支付控制器
*
* @author zhong
* @date 2022-09-14 11:56
*/
@Slf4j
@RestController
@RequestMapping("/payment")
public class PaymentController {
/**
* 注入业务接口
*/
@Resource
private PaymentService paymentService;
/**
* 注入YAML配置文件的端口号
*/
@Value("${server.port}")
private String serverPort;
/**
* 根据id查询支付信息
* @param id
* @return
*/
@GetMapping("/get/{id}")
public CommonResult getPaymentById(@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,"查询失败,当前提供者端口号为:"+serverPort,null);
}
}
/**
* 插入支付信息
* @param payment
* @return
*/
@PostMapping("/create")
public CommonResult 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,"插入数据库失败,当前提供者端口号为:"+serverPort,null);
}
}
}
1.5、启动项目测试
启动7001、7002
启动8001、8001
启动80
访问Eureka服务中心
通过postman测试
先通过原来的端口测试
http://localhost:8081/payment/get/2 http://localhost:8082/payment/get/2
通过80端口测试
http://localhost/consumer/payment/get/11
不管怎么刷新都是访问的8001端口的服务,并不会负载均衡访问到8002,因为后端的代码写死访问的是8001。
public static final String PaymentSrv_URL = "http://localhost:8081/payment";
下面步骤就是要将写死访问8001的代码改为动态的Eureka服务中心的
Application
微服务地址
5、消费者实现负载均衡调用支付微服务
1、修改80控制器的代码
public static final String PaymentSrv_URL = "http://localhost:8081/payment";
// ↓ 将访问的端口改为【微服务名称】
public static final String PaymentSrv_URL = "http://CLOUD-PAYMENT-SERVICE/payment";
如果单是修改上面的内容再次访问80测试功能是会直接报错如下
2、开启负载均衡
在实现集群部署的时候,一个微服务实例下可能有多个不同的接口地址,通过注解可以规定一个默认的负载均衡规则
package com.zcl.springcloud.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* 描述:RestTemplate配置类
*
* @author zhong
* @date 2022-09-14 13:29
*/
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate()
{
return new RestTemplate();
}
}
3、重启80项目测试
访问刷新查看每次输出的被调用的访问端口号内容,默认的负载均衡是【轮询】,各自一次