首页 > 编程语言 >【java深入学习第4章】精通 Java 微服务:Spring Boot 与 Spring Cloud 的核心技术与设计准则

【java深入学习第4章】精通 Java 微服务:Spring Boot 与 Spring Cloud 的核心技术与设计准则

时间:2024-07-14 10:52:36浏览次数:14  
标签:Java Spring Boot eureka 服务 Server public cloud

在现代软件开发中,微服务架构因其灵活性和可扩展性而备受青睐。本文将探讨Java微服务架构中的关键技术和设计原则,并通过Spring Boot和Spring Cloud提供代码示例,展示如何构建一个简单的微服务应用。

关键技术和设计原则

  1. 服务拆分:将单体应用拆分为多个独立的微服务,每个服务负责特定的业务功能。
  2. 独立部署:每个微服务可以独立部署和扩展,减少了服务之间的耦合。
  3. API网关:提供统一的入口,管理和路由请求到相应的微服务。
  4. 服务发现:动态发现微服务实例,支持负载均衡和故障转移。
  5. 配置管理:集中管理微服务的配置,支持配置的动态更新。
  6. 容错和监控:实现服务的高可用性和可观测性,确保系统的稳定运行。

使用Spring Boot和Spring Cloud构建微服务

我们将构建一个简单的微服务应用,包括以下组件:

  1. 服务注册中心(Eureka Server)
  2. 配置中心(Spring Cloud Config Server)
  3. API网关(Spring Cloud Gateway)
  4. 两个业务微服务
1. 服务注册中心(Eureka Server)

创建一个Spring Boot项目,添加以下依赖:

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

在主类中启用Eureka Server:

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

application.yml中配置Eureka Server:

server:
  port: 8761

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
2. 配置中心(Spring Cloud Config Server)

创建一个Spring Boot项目,添加以下依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

在主类中启用Config Server:

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

application.yml中配置Config Server:

server:
  port: 8888

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/your-repo/config-repo
3. API网关(Spring Cloud Gateway)

创建一个Spring Boot项目,添加以下依赖:

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

application.yml中配置API网关:

server:
  port: 8080

spring:
  cloud:
    gateway:
      routes:
        - id: service1
          uri: lb://SERVICE1
          predicates:
            - Path=/service1/**
        - id: service2
          uri: lb://SERVICE2
          predicates:
            - Path=/service2/**

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
4. 业务微服务

创建两个Spring Boot项目,分别命名为service1service2,并添加以下依赖:

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

service1的主类中:

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

service2的主类中:

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

service1service2application.yml中分别配置:

spring:
  application:
    name: service1
  cloud:
    config:
      uri: http://localhost:8888

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: service2
  cloud:
    config:
      uri: http://localhost:8888

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

service1service2中分别创建一个简单的REST控制器:

@RestController
@RequestMapping("/service1")
public class Service1Controller {
    @GetMapping("/hello")
    public String hello() {
        return "Hello from Service 1";
    }
}
@RestController
@RequestMapping("/service2")
public class Service2Controller {
    @GetMapping("/hello")
    public String hello() {
        return "Hello from Service 2";
    }
}

运行和测试

  1. 启动Eureka Server。
  2. 启动Config Server。
  3. 启动API网关。
  4. 启动Service1和Service2。

访问以下URL进行测试:

  • http://localhost:8080/service1/hello 应返回 "Hello from Service 1"
  • http://localhost:8080/service2/hello 应返回 "Hello from Service 2"

通过以上步骤,我们成功构建了一个简单的微服务架构应用,展示了如何使用Spring Boot和Spring Cloud实现服务注册、配置管理和API网关等关键功能。希望这篇文章对你理解和实践Java微服务架构有所帮助。

AI写论文,AI4.0技术加持,有需速入

标签:Java,Spring,Boot,eureka,服务,Server,public,cloud
From: https://www.cnblogs.com/zhizu/p/18301169

相关文章

  • 【java深入学习第2章】Spring Boot 结合 Screw:高效生成数据库设计文档之道
    在开发过程中,数据库设计文档是非常重要的,它可以帮助开发者理解数据库结构,方便后续的维护和扩展。手动编写数据库设计文档不仅耗时,而且容易出错。幸运的是,可以使用SpringBoot和Screw来自动生成数据库设计文档。什么是Screw?Screw是一个开源的数据库文档生成工具,它可以根据数据库......
  • 【java深入学习第3章】通过 Spring AOP 完成参数的加解密
    在现代应用中,数据的安全性越来越受到重视。为了保护敏感数据,我们常常需要对数据进行加密和解密。在这篇博客中,我将展示如何使用SpringAOP(面向切面编程)来实现对方法参数的加解密。什么是SpringAOP?SpringAOP是Spring框架中的一个模块,它提供了面向切面编程的功能。AOP允许我们将......
  • Spring的相关内容介绍
    Spring学习的核心内容IOC,AOP,jdbcTemplate,声明式事务IOC控制反转:可以管理相关的Java对象AOP:切面编程jdbctemplate是spring提供的一套访问数据库的相关技术,相对来说是要简单一点声明式事务:是基于ioc/aop实现的事务管理,应用性是比较强的Spring框架是管理其他框架的框架......
  • Java进阶之路66问 | 对接口签名是怎么理解的?如何防止接口重放攻击?
    接口签名为什么需要接口签名?现在越来越多的公司以API的形式对外提供服务,这些API接口大多暴露在公网上,所以安全性就变的很重要了。最直接的风险如下:非法使用API服务。(收费接口非法调用)恶意攻击和破坏。(数据篡改、DOS)因此需要设计一些接口安全保护的方式来增强接口......
  • Java进阶之路66问 | 什么是幂等性?如何保证接口的冥等性?
    API设计中的幂等性什么是幂等性幂等性是指无论一个操作执行多少次,最终的结果都是一样的。也就是说,重复执行同一个操作不会改变系统的状态或产生不同的结果。想象你在一栋大楼里等电梯。你按下电梯按钮的5楼按钮键,电梯开始向5楼的位置移动。后面即使你再按几次5楼按钮键,......
  • Java优雅使用线程池连接SFTP进行文件上传下载 解决请求量大问题
    Java优雅使用线程池连接SFTP进行文件上传下载解决请求量大问题使用FTP连接池降低资源消耗,提高响应速率为什么要使用线程池连接SFTP呢?在Java中使用线程池来连接SFTP(SecureFileTransferProtocol)工具的原因主要与性能、资源管理和效率有关。以下是一些关键原因:资源管......
  • 花几千上万学习Java,真没必要!(四)
    1、关系运算符:packagetest.com;publicclassRelationalArithmetic{ /*关系运算符用于比较两个值之间的关系,关系运算符的结果是一个布尔值,即true或false。 Java提供了6种关系运算符: >:大于 <:小于 >=:大于等于 <=:小于等于 ==:等于 !=:不等于*/publicstaticvoi......
  • Java计算机毕业设计个人健康管理系统的设计与实现(开题报告+源码+论文)
    本系统(程序+源码)带文档lw万字以上 文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容研究背景随着生活节奏的加快和健康意识的增强,个人健康管理成为了现代社会的重要议题。传统医疗模式下,人们往往只在出现症状时才寻求医生的帮助,这种“被动医疗......
  • Java计算机毕业设计的高校疫情防控系统(开题报告+源码+论文)
    本系统(程序+源码)带文档lw万字以上 文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容研究背景在全球新冠疫情持续蔓延的背景下,高校作为人群密集、流动性大的场所,其疫情防控工作面临着前所未有的挑战。传统的疫情防控手段难以有效应对疫情传播的......
  • Java计算机毕业设计校园二手物品交易平台(开题+源码+论文)
    本系统(程序+源码)带文档lw万字以上 文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容研究背景:随着高等教育的普及与校园生活的日益丰富,学生群体对各类学习资料、生活用品及电子产品的需求日益增长。同时,由于更新换代迅速及经济因素的考量,大量二......