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

Spring Cloud学习笔记:Eureka简介,Eureka简单样例

这是本人学习的总结,主要学习资料如下
- 马士兵教育

@[TOC](目录)

1、Eureka

1.1、架构

EurekaSpringCloud Nexflix的核心子模块,其中包含ServerClient

Server提供服务注册,存储所有可用服务节点。

Client用于简化和Server的通讯复杂度。

下面是Eureka的简单架构图

在这里插入图片描述

每一个服务节点需要在Eureka Server中注册,如果需要其他节点的服务,则需要远程调用Service ProviderProvider会访问Server,由Server找到一个合适的节点提供服务给cumsumer



1.2、核心特性

  1. 服务注册:这是最核心的功能,其余的特性都是对这个功能的加强。
  2. 服务续约:Client每隔30s就会向Server发送一次心跳来续约,超过90s没有续约就会被Server删除这个服务节点。
  3. 服务下线:Client可以主动向Server发送cancel命令优雅下线。
  4. 缓存注册列表:Client会缓存从Server获取的注册列表,并且每30s更新一次。

2、建立Spring Cloud项目

2.1、项目结构和父项目依赖

接下来就是代码展示如何配置启动serverclient,以及client之间获取信息。

这是项目结构,两个子module,分别是serverorder-clientuser-clientserver提供注册服务,另外两个作为client则是到server注册然后互相调用对方的服务。

在这里插入图片描述

这是根目录的dependency

<properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><java.version>1.8</java.version><spring.cloud.version>Hoxton.SR12</spring.cloud.version><spring.boot.version>2.3.12.RELEASE</spring.boot.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><version>${spring.boot.version}</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>${spring.boot.version}</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.30</version></dependency><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-actuator</artifactId><version>${spring.boot.version}</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-netflix-eureka-server</artifactId></dependency></dependencies>

2.2、启动Server

2.2.1、dependency

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

2.2.2、配置文件

resources/application.yml

spring:application:name: msb-eureka-server
server:port: 8761eureka:instance:#注册实例名称hostname: localhost#是否将自己的ip注册到eureka中,默认false 注册 主机名prefer-ip-address: true# Eureka客户端需要多长时间发送心跳给Eureka,表明他仍然或者,默认是30# 通过下面方式我们可以设置,默认单位是秒lease-renewal-interval-in-seconds: 10# Eurkea服务器在接受到实例最后一次发送的心跳后,需要等待多久可以将次实例删除# 默认值是90# 通过下面方式我们可以设置,默认单位是秒lease-expiration-duration-in-seconds: 30client:#是否注册到eureka服务中register-with-eureka: false#是否拉取其他服务fetch-registry: false

2.2.3、Server端启动代码

@EnableEurekaServer
// 因为一直报Gson包冲突所以加上了exclude
@SpringBootApplication(exclude = {GsonAutoConfiguration.class})
public class EureakServerApplication {public static void main(String[] args) {SpringApplication.run(EureakServerApplication.class);}
}

启动以后打开网页检查。localhost:8761
请添加图片描述


2.3、启动Client

2.3.1、dependency

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

2.3.2、配置文件

order-client和```user-client````都一样,因为是单机模拟所以监听的端口号不同。

# 节点在server中注册的名字
spring:application:# user-client则用user-clientname: order-client
server:
# order-client 监听9002, user-client监听9003port: 9002eureka:client:# 这个一定要配对,server地址后面默认要加一个上下文eurekaservice-url:defaultZone: http://localhost:8761/eurekamanagement:endpoints:web:exposure:include: shutdown #暴露shutdown端点endpoint:shutdown:enabled: true #再次确认暴露shutdown端点feign:tokenId: 11111111111111111111

2.3.3、Client端启动代码

注意有两个注解可以将其标注为Client,分别是@EnableDiscoveryClient@EnableEurekaClient

这里推荐使用@EnableDiscoveryClient,因为后者是netfliex提供的,如果使用后者,后期要更换其它注册中心就需要更换注解,比较麻烦。

这是order-client的代码

@EnableDiscoveryClient // 这是官方提供的  ,我们以后可能切换其他的注册中心比如说nacos,那我们就直接切换就行了
//@EnableEurekaClient  // 是netflix提供的,如果用这个注解就只能服务于eureka
@SpringBootApplication
public class EurekaOrderClientApplication {public static void main(String[] args) {SpringApplication.run(EurekaOrderClientApplication.class);}
}

这时user-client的代码

@EnableDiscoveryClient // 这是官方提供的  ,我们以后可能切换其他的注册中心比如说nacos,那我们就直接切换就行了
//@EnableEurekaClient  // 是netflix提供的,如果用这个注解就只能服务于eureka
@SpringBootApplication(exclude = {GsonAutoConfiguration.class})
public class EurekaUserClientApplication {public static void main(String[] args) {SpringApplication.run(EurekaUserClientApplication.class);}
}

到Server的页面查看,两个Client都注册成功。
在这里插入图片描述

2.3.4、提供RPC服务

因为RPC是基于HTTP实现的协议,所以我们提供RPC服务时就像写一个controller的服务一样。

这里设定order模块会调用user提供的服务。

@Slf4j
@RestController
public class UserController {@RequestMapping("/getUserInfo")public String getUser(String userId) {log.info(userId);	return "userInfo: {userId: "+ userId +"}";;}
}

接下来就看order模块如何通过Eureka调用user提供的服务。

2.4、服务之间获取信息

引入LoadBalancerClient,从这个bean中可以获得其他注册的client元数据,比如地址,端口号等。

获取到这些信息后就可以组成请求地址,然后获取数据。

下面这个例子展示了如何获取其他client的元信息并且调用其它client的服务。

@Service
public class OrderService {@Autowiredprivate LoadBalancerClient eurekaClient;@Autowiredprivate RestTemplate restTemplate;public void getUser() {ServiceInstance instance = eurekaClient.choose("msb-user");String hostname = instance.getHost();int port = instance.getPort();String uri = "/getUserInfo?userId=" + userId;String url = "http://" + hostname + ":" + port + uri;return restTemplate.getForObject(url, String.class);}}
http://www.lryc.cn/news/336462.html

相关文章:

  • 【漏洞复现】WordPress Welcart 任意文件读取漏洞(CVE-2022-4140)
  • 快速排序:深入解析其原理、实现与性能特性
  • 一文看懂Mac地址
  • 2024.4.10作业
  • python - Django创建项目
  • WPF —— 动画缩放变换
  • SQL注入---盲注
  • PlanUML和Mermaid哪个好?
  • leetcode 343. 整数拆分
  • 【MATLAB源码-第180期】基于matlab的PTS,SLM,CPFilter三种降低OFDM系统的PAPR仿真。
  • 学透Spring Boot — 004. Spring Boot Starter机制和自动配置机制
  • 面试算法-170-二叉树的最大深度
  • 【数据结构】哈希
  • Kubernetes(k8s)监控与报警(qq邮箱+钉钉):Prometheus + Grafana + Alertmanager(超详细)
  • STM32-04基于HAL库(CubeMX+MDK+Proteus)中断案例(按键中断扫描)
  • 第十五篇:Mybatis
  • 【MacBook系统homebrew镜像记录】
  • 深拷贝总结
  • RabbitMQ在云原生环境中部署和应用实践
  • flask 后端 + 微信小程序和网页两种前端:调用硬件(相机和录音)和上传至服务器
  • 蓝桥杯嵌入式(G431)备赛笔记——ADC+LCD
  • 最近公共祖先(LCA)
  • ABBYY FineReader15免费电脑OCR图片文字识别软件
  • 2024年第十七届 认证杯 网络挑战赛 (A题)| 保暖纤维的保暖能力 |数学建模完整代码+建模过程全解全析
  • 算法训练营第37天|LeetCode 738.单调递增的数字 968.监控二叉树
  • Vue+el-table 修改表格 单元格横线边框颜色及表格空数据时边框颜色
  • 大恒相机-程序异常退出后显示被占用
  • 头歌-机器学习 第16次实验 EM算法
  • 电脑启动引导的两种方式
  • 用php编写网站源码的一些经验