SpringCloud学习笔记(二、单体架构实例)

简介: SpringCloud学习笔记(二、单体架构实例)

文章目录

单体架构实例

在Idea里新建一个SpringBoot项目, 这里选择SpringBoot 的版本依赖是 2.0.3.RELEASE。

  • 依赖
    pom.xml如下:
<?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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>edu.hpu</groupId>
  <artifactId>product-service</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>product-service</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  <dependencies>
    <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.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <optional>true</optional> <!-- 配置为true 热部署才有效 -->
    </dependency>
    <dependency>         <!--配置hutool工具包-->
      <groupId>cn.hutool</groupId>
      <artifactId>hutool-all</artifactId>
      <version>4.3.1</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <configuration>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

  • 启动类ProducServicetApplication:
package edu.hpu;
import cn.hutool.core.util.NetUtil;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class ProductServiceApplication {
    public static void main(String[] args){
        int port=8080;         //端口号
        if(!NetUtil.isUsableLocalPort(port)){
            System.err.printf("端口%d被占用了,无法启动%n", port );
            System.exit(1);
        }
        new SpringApplicationBuilder(ProductServiceApplication.class).properties("server.port=" + port).run(args);
    }
}

  • 实体类Product:
package edu.hpu.pojo;
public class Product {
    private int id;
    private String name;
    private int  price;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public Product(int id, String name, int price) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
    }
    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}
  • 服务层ProcuctService:
    为了降低代码量,这里数据直接放在内存里,不再引入数据库。
package edu.hpu.service;
import edu.hpu.pojo.Product;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class ProductService {
    public List<Product> listProducts(){
      List<Product> products=new ArrayList<>();
      products.add(new Product(1,"锤子",11));
      products.add(new Product(2,"斧头",23));
      products.add(new Product(3,"棒子",33));
      return products;
    }
}

  • 控制层ProductController:
package edu.hpu.controller;
import edu.hpu.pojo.Product;
import edu.hpu.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class ProductController {
    @Autowired
    private ProductService productService;
    @RequestMapping("/procucts")
    public Object products(Model m){
        List<Product> ps = productService.listProducts();
        m.addAttribute("ps", ps);
        return "products";
    }
}

  • 视图products.html:
    这里用的是thymeleaf模板来编写我们的前端
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        table {
            border-collapse:collapse;
            width:400px;
            margin:20px auto;
        }
        td,th{
            border:1px solid gray;
        }
    </style>
</head>
<body>
   <div id="container">
       <table>
           <thead>
           <th>编号</th>
           <th>名称</th>
           <th>价格</th>
           </thead>
           <tbody>
           <tr th:each="p: ${ps}">
               <td th:text="${p.id}"></td>
               <td th:text="${p.name}"></td>
               <td th:text="${p.price}"></td>
           </tr>
           </tbody>
       </table>
   </div>
</body>
</html>
  • 配置application.properties
#thymeleaf 配置
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
#缓存设置为false, 这样修改之后马上生效,便于调试
spring.thymeleaf.cache=false
#上下文
server.context-path=/

至此一个简单的单体式的MVC分层应用就创建成功了,运行,访问。

image.png

分析与比较

在引入微服务的架构之前,我们先对当前的单体应用做一个分析。

单体架构优点

便于开发,测试,部署也很方便,直接打成一个 jar 或者 war, 就全部搞定了

单体架构缺点

这个项目很简单,就做两件事: 1. 提供数据 2. 展示数据。

它把两个服务,提供数据和展示数据 放在了一起,这就会出现单体架构固有的缺点。

  1. 如果要修改数据部分的代码, 那么必须把整个项目重新编译打包部署。 虽然展示部分,什么都没变但是也会因为重新部署而暂时不能使用,要部署完了,才能使用。
  2. 如果提供数据部分出现了问题,比如有的开发人员改错了,抛出了异常,会导致整个项目不能使用,展示数据部分也因此受到影响。
  3. 性能瓶颈难以突破
    image.png

改进

上面是一个单体式的架构,现在用分布式和集群的思路,对其进行改造:

微服务

微服务简单说,一个 springboot 就是一个 微服务,并且这个 springboot 做的事情很单纯。 比如  product-service 这个项目,就可以拆成两个微服务,分别是 数据微服务,和视图微服务,其实就是俩 springboot,  只是各自做的事情都更单纯。

image.png

服务注册

有了微服务,就存在如何管理这个微服务,以及这两个微服务之间如何通信的问题,所以就要引入一个 微服务注册中心概念,这个微服务注册中心在 springcloud 里就叫做 eureka server, 通过它把就可以把微服务注册起来,以供将来调用。

image.png

服务访问

在业务逻辑上, 视图微服务 需要 数据微服务 的数据,所以就存在一个微服务访问另一个微服务的需要。

而这俩微服务已经被注册中心管理起来了,所以 视图微服务 就可以通过 注册中心定位并访问 数据微服务了。

在后续教程里,会演示微服务的相互调用。

image.png

分布式

当前这个架构可以说是分布式,原来是在一个 springboot里就完成的事情,现在分布在多个 springboot里做,这就是初步具备 分布式雏形了。

image.png

集群

原来数据微服务只有这一个springboot, 现在做同样数据微服务的,有两个 springboot, 他们提供的功能一模一样,只是端口不一样,这样就形成了集群。

那么集群有什么好处呢?

  1. 比起一个 springboot, 两个springboot 可以分别部署在两个不同的机器上,那么理论上来说,能够承受的负载就是 x 2. 这样系统就具备通过横向扩展而提高性能的机制。
  2. 如果 8001 挂了,还有 8002 继续提供微服务,这就叫做高可用 。
    image.png

这样一个简单的微服务的架构就搭建出来了,接下来的学习就围绕这个架构来实现和完善。

参考:

【1】、分布式和集群 / SpringCloud / SpringCloud系列教材 (一)- 介绍

【2】、分布式和集群 / SpringCloud / SpringCloud系列教材 (二)- 单体架构例子


目录
相关文章
|
28天前
|
负载均衡 Java API
Spring Cloud 面试题及答案整理,最新面试题
Spring Cloud 面试题及答案整理,最新面试题
130 1
|
28天前
|
Java Nacos Sentinel
Spring Cloud Alibaba 面试题及答案整理,最新面试题
Spring Cloud Alibaba 面试题及答案整理,最新面试题
134 0
|
29天前
|
SpringCloudAlibaba Java 持续交付
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
126 0
|
1天前
|
负载均衡 Java 开发者
细解微服务架构实践:如何使用Spring Cloud进行Java微服务治理
【4月更文挑战第17天】Spring Cloud是Java微服务治理的首选框架,整合了Eureka(服务发现)、Ribbon(客户端负载均衡)、Hystrix(熔断器)、Zuul(API网关)和Config Server(配置中心)。通过Eureka实现服务注册与发现,Ribbon提供负载均衡,Hystrix实现熔断保护,Zuul作为API网关,Config Server集中管理配置。理解并运用Spring Cloud进行微服务治理是现代Java开发者的关键技能。
|
1天前
|
Java API 对象存储
对象存储OSS产品常见问题之使用Spring Cloud Alibaba情况下文档添加水印如何解决
对象存储OSS是基于互联网的数据存储服务模式,让用户可以安全、可靠地存储大量非结构化数据,如图片、音频、视频、文档等任意类型文件,并通过简单的基于HTTP/HTTPS协议的RESTful API接口进行访问和管理。本帖梳理了用户在实际使用中可能遇到的各种常见问题,涵盖了基础操作、性能优化、安全设置、费用管理、数据备份与恢复、跨区域同步、API接口调用等多个方面。
16 2
|
16天前
|
负载均衡 网络协议 Java
构建高效可扩展的微服务架构:利用Spring Cloud实现服务发现与负载均衡
本文将探讨如何利用Spring Cloud技术实现微服务架构中的服务发现与负载均衡,通过注册中心来管理服务的注册与发现,并通过负载均衡策略实现请求的分发,从而构建高效可扩展的微服务系统。
|
29天前
|
SpringCloudAlibaba 负载均衡 Java
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(目录大纲)
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(目录大纲)
61 1
|
29天前
|
Java Nacos Sentinel
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(九)Nacos+Sentinel+Seata
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(九)Nacos+Sentinel+Seata
183 0
|
10天前
|
API 数据库 开发者
构建高效可靠的微服务架构:后端开发的新范式
【4月更文挑战第8天】 随着现代软件开发的复杂性日益增加,传统的单体应用架构面临着可扩展性、维护性和敏捷性的挑战。为了解决这些问题,微服务架构应运而生,并迅速成为后端开发领域的一股清流。本文将深入探讨微服务架构的设计原则、实施策略及其带来的优势与挑战,为后端开发者提供一种全新视角,以实现更加灵活、高效和稳定的系统构建。
18 0
|
24天前
|
负载均衡 测试技术 持续交付
高效后端开发实践:构建可扩展的微服务架构
在当今快速发展的互联网时代,后端开发扮演着至关重要的角色。本文将重点探讨如何构建可扩展的微服务架构,以及在后端开发中提高效率的一些实践方法。通过合理的架构设计和技术选型,我们可以更好地应对日益复杂的业务需求,实现高效可靠的后端系统。