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

Spring Cloud学习笔记:基础知识

这是本人学习的总结,主要学习资料如下

  • 马士兵教育

目录

  • 1、Spring Cloud 简介
  • 2、Eureka
  • 3、建立Spring Cloud项目
    • 3.1、启动Server
      • 3.1.1、dependency
      • 3.1.2、配置文件
      • 3.1.3、Server端启动代码
    • 3.2、启动Client
      • 3.2.1、dependency
      • 3.2.2、配置文件
      • 3.3.3、Client端启动代码
    • 3.3、服务之间获取信息

1、Spring Cloud 简介

spring cloud是为了解决微服务架构的难题而诞生的微服务全家桶框架。

确定spring cloud的版本,要根据spring boot来确定,官网上有对应的表格。

注意下面的提示,Dalston, Edgware, Finchley, Greenwich已经不再维护,所以实际开发中不要再使用这四个spring cloud的版本。

在这里插入图片描述

https://spring.io/projects/spring-cloud

2、Eureka

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

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

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

下面是Eureka的简单架构图

在这里插入图片描述

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

3、建立Spring Cloud项目

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

3.1、启动Server

3.1.1、dependency

<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</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.3.7.RELEASE</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR12</version><type>pom</type><scope>import</scope></dependency>
</dependencies>

3.1.2、配置文件

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

3.1.3、Server端启动代码

@EnableEurekaServer
@SpringBootApplication
public class EureakServerApplication {public static void main(String[] args) {SpringApplication.run(EureakServerApplication.class);}
}

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


3.2、启动Client

3.2.1、dependency

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.3.7.RELEASE</version><type>pom</type><scope>import</scope>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR12</version><type>pom</type><scope>import</scope>
</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>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

3.2.2、配置文件

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

3.3.3、Client端启动代码

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

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

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

同样的配置和代码,再启动一个叫msb-stock的服务

到Server的页面查看,两个服务都注册成功。

请添加图片描述

3.3、服务之间获取信息

引入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/34699.html

相关文章:

  • 农产品销售系统/商城,可运行
  • 【Java开发】JUC进阶 05:函数式接口、ForkJoin
  • Nginx支持quic协议
  • 笔记 - Java 内存结构与模型
  • C#基础教程12 数组
  • Android中级——屏幕和绘图
  • Linux - 第6节 - 动态库和静态库
  • 【Java学习笔记】12.Character 类及String 类
  • 【C++修炼之路】26.C++11(语法糖)
  • KD610精密油介损体积电阻率测试仪
  • 快速了解原码、反码、补码和位运算
  • 算法的复杂度介绍
  • 教你如何搭建店铺—收支管理系统,demo可分享
  • java性能分析-堆内存最佳实践-堆分析
  • 3月8号作业
  • Flink相关介绍
  • Java 8 排序
  • Blazor_WASM之4:路由
  • 对Vue响应式的理解
  • 磁盘阵列Raid探讨
  • 基于MyBatis依次、批量、分页增删改查
  • Tomcat源码分析-Session源码解析
  • 常见数据模型
  • Lesson 8.3 ID3、C4.5 决策树的建模流程 Lesson 8.4 CART 回归树的建模流程与 sklearn 参数详解
  • 阿里云手机短信登录
  • Android Camera SDK NDK NDK_vendor介绍
  • SQL基础语句小结
  • 管理类书籍推荐
  • win10 mingw 调用python
  • 教你使用三种方式写一个最基本的spark程序