当前位置: 首页 > news >正文

微服务架构——笔记(3)Eureka

微服务架构——笔记(3)

基于分布式的微服务架构
本次笔记为 此次项目的记录,便于整理思路,仅供参考,笔者也将会让程序更加完善
内容包括:1.支付模块、2.消费者订单模块、支付微服务入驻Eureka、Eureka集群…
在这里插入图片描述
文章来源B站视频
尚硅谷SpringCloud框架开发教程(SpringCloudAlibaba微服务分布式架构丨Spring Cloud)教程
本次笔记内容为消费者订单Module模块

一、Eureka服务注册与发现

在这里插入图片描述
在这里插入图片描述

1.1 是服务治理

SpringCloud封装了Netflix公司开发的Eureka模块来实现服务治理
在传统的rpc远程调用框架中,管理每个服务于服务之间依赖关系比较复杂,需要服务治理,管理服务与服务间的依赖关系,可以实现服务调用、负载均衡、容错 等,实现服务发现与注册。

1.2 服务注册

Eureka系统架构
在这里插入图片描述
Dubbo 系统架构在这里插入图片描述

1.3 Eureka两个组件

Eureka包含两个组件: Eureka Server和Eureka Client

Eureka Server提供服务注册服务
各个微服务节点通过配置启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观看到。

EurekaClient通过注册中心进行访问
是一个Java客户端,用于简化Eureka Server的交与,客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器在应用启动后,将会向Eureka Server发送心跳(默认周期为30秒)。如果Eureka Server在多个心跳周期内没有接收到某人节点的心跳,EurekaServer将会从服务注册表中把这个服务节点移除(默认90秒)

二、 单机Eureka构建步骤

在这里插入图片描述

2.1 建moudle

在这里插入图片描述

2.2 改pom

<?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>cloud2023</artifactId><groupId>com.atliangstar.springcloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-eureka-server7001</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><dependency><groupId>com.atliangstar.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><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></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><!--<dependency>--><!--<groupId>com.h2database</groupId>--><!--<artifactId>h2</artifactId>--><!--<scope>runtime</scope>--><!--</dependency>--></dependencies>
</project>

2.3 写yml

server:port: 7001eureka:instance:hostname: localhost #eureka服务端的实例名称client:#false表示不向注册中心注册自己register-with-eureka: false#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检查服务fetch-registry: falseservice-url:#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

注出现:

Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
解决方案一:

如果你使用嵌入式数据库(如 H2、HSQL 或 Derby),请确保将其放置在类路径,可以在 Maven 依赖中添加相应的数据库驱动依赖项。
例如,如果你要使用 H2 数据库,可以添加以下依赖项到你的 pom.xml 文件中:
xml

<dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope>
</dependency>
解决方案二:
在SpringBoot启动类注解@SpringBootApplication后
加上exclude = DataSourceAutoConfiguration.class,
表示启动时不启用 DataSource的自动配置检查
// exclude :启动时不启用 DataSource的自动配置检查
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableEurekaServer   // 表示它是服务注册中心
public class EurekaServerMain7001 {public static void main(String[] args){SpringApplication.run(EurekaServerMain7001.class, args);}
}

2.4 写启动

package com.liangstar.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;// exclude :启动时不启用 DataSource的自动配置检查
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableEurekaServer
public class EurekaMain7001 {public static void main(String[] args) {SpringApplication.run(EurekaMain7001.class, args);}
}

2.5 测试

在这里插入图片描述

三、支付微服务8001入驻eureka

在这里插入图片描述

3.1 改pom

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>

3.2 写yml

eureka:client:# 表示是否将自己注册进eurekaserver默认为trueregister-with-eureka: true# 是否从eurekaserver抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合fetchRegistry: trueservice-url:defaultZone: http://localhost:7001/eureka

注:如何未注册成功,则配置有问题

3.3 主启动

package com.liangstar.springcloud;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {public static void main(String[] args) {SpringApplication.run(PaymentMain8001.class,args);}
}

3.4 测试

在这里插入图片描述

四、消费者服务80入驻eureka

4.1 改pom

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

4.2 写yml

eureka:client:# 表示是否将自己注册进eurekaserver默认为trueregister-with-eureka: true# 是否从eurekaserver抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合fetchRegistry: trueservice-url:defaultZone: http://localhost:7001/eureka

4.3 主启动

@SpringBootApplication
@EnableEurekaClient
public class OrderMain80 {public static void main(String[] args) {SpringApplication.run(OrderMain80.class,args);}
}

4.4 测试

在这里插入图片描述

五、Eureka集群

在这里插入图片描述
集群:互相注册,相互守望
在这里插入图片描述

5.1 构建

在这里插入图片描述
在这里插入图片描述
构建一个与7001一样的moudle

5.2 修改映射配置

打开C:WindowsSystem32driversetc
在这里插入图片描述
修改映射配置添加进hosts文件
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
在这里插入图片描述

5.2 修改映射配置

moudle7001:

server:port: 7001eureka:instance:hostname: eureka7001.com #eureka服务端的实例名称client:#false表示不向注册中心注册自己register-with-eureka: false#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检查服务fetch-registry: falseservice-url:#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址defaultZone: http://eureka7002.com:7002/eureka/

moudle7002:

server:port: 7002eureka:instance:hostname: eureka7002.com #eureka服务端的实例名称client:#false表示不向注册中心注册自己register-with-eureka: false#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检查服务fetch-registry: falseservice-url:#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址defaultZone: http://eureka7001.com:7001/eureka/

5.3 服务添加至7001/7002

8001yml文件

eureka:client:# 表示是否将自己注册进eurekaserver默认为trueregister-with-eureka: true# 是否从eurekaserver抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合fetchRegistry: trueservice-url:defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

80yml文件


eureka:client:# 表示是否将自己注册进eurekaserver默认为trueregister-with-eureka: true# 是否从eurekaserver抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合fetchRegistry: trueservice-url:defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

5.3 测试

http://eureka7001.com:7001/

在这里插入图片描述

http://eureka7002.com:7002/

在这里插入图片描述

http://www.lryc.cn/news/223228.html

相关文章:

  • 网络编程套接字(2)——简单的TCP网络程序
  • MySQL数据库的简单的面试题
  • hbuilderx打包应用上传到app store构建版本的教程
  • 第五届泰迪杯数据分析技能赛B题源码图片分享
  • 【小白专用】VSCode下载和安装与配置PHP开发环境(详细版) 23.11.08
  • Qlik Sense : Fetching data with Qlik Web Connectors
  • 聊一聊 tcp/ip 在.NET故障分析的重要性
  • 利用梯度上升可视化卷积核:基于torch实现
  • python+playwright 学习-85 启动参数 proxy 设置代理几种方式
  • Clion 搭建Qt projects
  • 合肥工业大学数据库实验报告
  • 设计模式-装饰器模式(Decorator)
  • Java 数据结构篇-实现双链表的核心API
  • 电脑如何截屏?一起来揭晓答案!
  • 【实战-08】flink 消费kafka自定义序列化
  • 深入浅出 Django 异步编程
  • 力扣 138. 随机链表的复制
  • STM32外部中断大问题
  • FPGA配置采集AR0135工业相机,提供2套工程源码和技术支持
  • KubeSphere v3.4.0 部署K8S Docker + Prometheus + grafana
  • Codeforces Round 908 (Div. 2)题解
  • Redis笔记 Redis主从同步
  • 数据结构-Prim算法构造无向图的最小生成树
  • MFC串口通信(SerialPort)
  • Vim基本使用操作
  • 【深蓝学院】手写VIO第8章--相机与IMU时间戳同步--作业
  • Naocs配置中心配置映射List、Map、Map嵌套List等方式
  • 如何通过CRM系统进行销售机会管理?
  • 解决idea启动tomcat控制台中文乱码
  • vscode + cmake + opencv example