随笔 - 367  文章 - 0  评论 - 20  阅读 - 63万 

初学SpringCloud

前言

在SpringBoot的坑还没填完的情况下,我又迫不及待地开新坑了。主要是寒假即将结束了,到时又得忙于各种各样的事情……留个坑给自己应该就会惦记着它,再慢慢地补上…………………………

附录

个人博客地址 : https://zggdczfr.cn
个人参考项目 : https://github.com/FunriLy/springcloud-study/tree/master/%E6%A1%88%E4%BE%8B1

SpringCloud 介绍

Spring Cloud是一个基于Spring Boot实现的云应用开发工具(就是说,接触Spring Cloud之前需要了解一下Spring Boot)。归结起来,Spring Cloud 是一个微服务工具包,为开发者提供了在分布式系统的配置管理、服务发现、断路器、智能路由、微代理、控制总线等许多开发工具包。
按照官方的说法就是,Spring Cloud 为开发者提供了在分布式系统操作的工具。而且容易上手,可以进行快速开发。

微服务架构

微服务架构概念

“微服务架构”,就是将一个完整的应用从数据存储开始拆分成多个不同的服务,每个服务都能独立部署、独立维护、独立扩展。系统中的各个微服务可被独立部署,各个微服务之间是松耦合的。每个微服务仅关注于完成一件任务并很好地完成该任务。服务与服务间通过诸如RESTful API的方式互相调用。

关于微服务架构的扯淡

“微服务架构”,其实这个概念好几年前就已经出现了,而且国外也出现了较为成熟的产品:netflix、dubbo等。听说目前 Spring 开发团队的精力主要集中于 spring boot 和 spring cloud 相关框架的开发。一般情况下,作为一个学习 java 的屌丝,基本上跟上 spring 屌丝的步伐,也就跟上了主流技术。

微服务架构的选择

附上大神 程序猿DD(翟永超) 的一篇博客,可以参考一下:微服务架构的基础框架选择:Spring Cloud还是Dubbo?

服务注册与发现(Eureka)

Eureka Server

    • 创建一个 Spring Boot 项目。只要引入了Log4j2,便于日志记录。pom.xml依赖文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!-- SpringBoot 框架 -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter</artifactId>
          <exclusions>
              <exclusion>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-logging</artifactId>
              </exclusion>
          </exclusions>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
      </dependency>
      <!--log4j2-->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-log4j2</artifactId>
      </dependency>
      <!-- eureka-服务 -->
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-eureka-server</artifactId>
      </dependency>

 版本参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!-- 还是比较喜欢稳定的 Brixton 版本 -->
  <dependencyManagement>
      <dependencies>
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-dependencies</artifactId>
              <version>Brixton.RELEASE</version>
              <type>pom</type>
              <scope>import</scope>
          </dependency>
      </dependencies>
  </dependencyManagement>
  <!--spring boot 的 maven 插件-->
  <build>
      <plugins>
          <plugin>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
      </plugins>
  </build>

 

  • 通过@EnableEurekaServer注解启动一个服务注册中心。直接在SpringBoot启动类加上注解@EnableEurekaServer即可。

  • application.properties配置:

1
2
3
4
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

 

同时,简要说明一下这些配置项:
eureka.client.registerWithEureka :表示是否将自己注册到Eureka Server,默认为true。由于当前这个应用就是Eureka Server,故而设为false。
eureka.client.fetchRegistry :表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设为false。
eureka.client.serviceUrl.defaultZone :设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。

如图所示:

这里写图片描述

Eureka Client

  • 创建一个 Spring Boot 项目。基本上与上一个工程无异,pom.xml需要修改了一个依赖。
1
2
3
4
5
<!-- eureka-服务 -->
   <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-eureka</artifactId>
   </dependency>

 

  • 通过@EnableDiscoveryClient注解启动一个服务注册中心。直接在SpringBoot启动类加上注解@EnableDiscoveryClient即可。同时,Eureka Client 提供的服务与一般 Spring Boot 项目是一样的。

  • application.properties配置:

1
2
3
4
# eureka client 配置
spring.application.name=service0
server.port=1111
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

 

如图所示:
这里写图片描述

 

posted on   巨象  阅读(2103)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示