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

举报
三分恶 发表于 2021/04/23 01:54:22 2021/04/23
【摘要】 文章目录 单体架构实例分析与比较单体架构优点单体架构缺点 改进微服务服务注册服务访问分布式集群 单体架构实例 在Idea里新建一个SpringBoot项目, 这里选择SpringBoot 的版本依赖是 2.0.3.RELEASE。 依赖 pom.xml如下: <?xml version="1.0" encoding="UTF-8"?>...


单体架构实例

在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>


  
 
  • 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
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

  • 启动类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); }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

  • 实体类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 + '}'; }
}


  
 
  • 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
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 服务层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; }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

  • 控制层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"; }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

  • 视图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>

  
 
  • 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
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 配置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=/

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

至此一个简单的单体式的MVC分层应用就创建成功了,运行,访问。
在这里插入图片描述

分析与比较

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

单体架构优点

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

单体架构缺点

这个项目很简单,就做两件事: 1. 提供数据 2. 展示数据。
它把两个服务,提供数据和展示数据 放在了一起,这就会出现单体架构固有的缺点。

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

改进

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


微服务

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

服务注册

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

服务访问

在业务逻辑上, 视图微服务 需要 数据微服务 的数据,所以就存在一个微服务访问另一个微服务的需要。
而这俩微服务已经被注册中心管理起来了,所以 视图微服务 就可以通过 注册中心定位并访问 数据微服务了。
在后续教程里,会演示微服务的相互调用。
在这里插入图片描述

分布式

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


集群

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

那么集群有什么好处呢?

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

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



参考:

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

文章来源: blog.csdn.net,作者:三分恶,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/sinat_40770656/article/details/95964331

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。