zoukankan      html  css  js  c++  java
  • SpringCloud系列教材 (二)- 单体架构例子

    SpringCloud系列教材 (二)- 单体架构例子


     
    步骤1:先运行,看到效果,再学习
    步骤2:模仿和排错
    步骤3:Springboot
    步骤4:pom.xml
    步骤5:ProductServiceApplication
    步骤6:Product
    步骤7:ProductService
    步骤8:ProductController
    步骤9:products.html
    步骤10:application.properties
    步骤11:单体结构
    步骤 1 : 先运行,看到效果,再学习
    老规矩,先下载下载区(点击进入)的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。 
    运行 ProductServiceApplication, 然后访问地址:
    http://127.0.0.1:8080/products
    就可以看到如图所示的效果了。
    先运行,看到效果,再学习
    步骤 2 : 模仿和排错
    在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。 
    模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。 
    采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。 

    推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。 
    这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来 
    这里提供了绿色安装和使用教程:diffmerge 下载和使用教程
    步骤 3 : Springboot
    关于 springboot 本身的学习, 如果不会,可以先去站长的 springboot 教程专区学习:Springboot入门
    步骤 4 : pom.xml
    springcloud 我们会使用 Finchley 这个版本,而它对 springboot 的版本依赖是 2.0.3.RELEASE, 所以我们会用 2.0.3.RELEASE 这个版本的 springboot 来撸。
    <?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">
        <groupId>cn.howj</groupId>
        <artifactId>product-service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <modelVersion>4.0.0</modelVersion>
        <packaging>war</packaging>
     
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.3.RELEASE</version>
            <relativePath/>
        </parent>
     
        <properties>
            <java.version>1.8</java.version>
        </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>
                <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>
    步骤 5 : ProductServiceApplication
    ProductServiceApplication 启动类, 这里使用代码指定端口的方式,免得有同学8080端口被占用了,自己都不知道,老是折磨自己~
    package cn.how2j.springboot;
     
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
     
    import cn.hutool.core.util.NetUtil;
     
    @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);
        }
    }
    步骤 6 : Product
    实体类
    package cn.how2j.springboot.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;
        }
         
    }
    步骤 7 : ProductService
    服务类,这里直接在内存里提供数据了,免得大家配数据库配来配去,还配不对,影响学习。。。
    package cn.how2j.springboot.service;
     
    import java.util.ArrayList;
    import java.util.List;
     
    import org.springframework.stereotype.Service;
     
    import cn.how2j.springboot.pojo.Product;
     
    @Service
    public class ProductService {
        public List<Product> listProducts(){
            List<Product> ps = new ArrayList<>();
            ps.add(new Product(1,"product a"50));
            ps.add(new Product(2,"product b"100));
            ps.add(new Product(3,"product c"150));
            return ps;
        }
    }
    步骤 8 : ProductController
    控制器
    package cn.how2j.springboot.web;
    import java.util.List;
     
    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 cn.how2j.springboot.pojo.Product;
    import cn.how2j.springboot.service.ProductService;
      
    @Controller
    public class ProductController {
      
        @Autowired ProductService productService;
         
        @RequestMapping("/products")
        public Object products(Model m) {
            List<Product> ps = productService.listProducts();
            m.addAttribute("ps", ps);
            return "products";
        }
    }
    步骤 9 : products.html
    thymeleaf 文件, 用来遍历数据。 thymeleaf 不熟悉的同学,跳过去学习: thymeleaf 入门 基于 springboot 方式
    <!DOCTYPE HTML>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>products</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <style>
     
            table {
                border-collapse:collapse;
                400px;
                margin:20px auto;
            }
            td,th{
                border:1px solid gray;
            }
             
        </style>       
    </head>
    <body>
     
    <div class="workingArea">
        <table>
            <thead>
                <tr>
                    <th>id</th>
                    <th>产品名称</th>
                    <th>价格</th>
                </tr>
            </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>
    步骤 10 : application.properties
    springboot 配置信息,主要是 thymeleaf 的相关配置
    #thymeleaf 配置
    spring.thymeleaf.mode=HTML5
    spring.thymeleaf.encoding=UTF-8
    spring.thymeleaf.content-type=text/html
    #缓存设置为false, 这样修改之后马上生效,便于调试
    spring.thymeleaf.cache=false
    #上下文
    server.context-path=/
    步骤 11 : 单体结构
    这个项目很简单,就做两件事: 1. 提供数据 2. 展示数据。 这就是一个典型的单体结构。
    它把两个服务 提供数据和展示数据 放在了一起,这就会出现固有的缺点。
    1. 如果要修改数据部分的代码, 那么必须把整个项目重新编译打包部署。 虽然展示部分,什么都没变但是也会因为重新部署而暂时不能使用,要部署完了,才能使用。
    2. 如果提供数据部分出现了问题,比如有的开发人员改错了,抛出了异常,会导致整个项目不能使用,展示数据部分也因此受到影响。
    3. 性能瓶颈难以突破
    4. 等等。。。

    以上就是单体结构的问题,接下来,站长就会带着大家,把这个单体结构的项目,改造成为springcloud 微服务分布式架构,通过观察和参与改造过程,就能够掌握和理解 springcloud啦。
    单体结构
  • 相关阅读:
    面向对象
    反射的基本介绍
    简单的总结
    生成器和迭代器
    shutil
    模块
    利用reguests 请求获取列车时刻表
    初识requests
    hashlib:用于加密相关的操作,代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法
    【网站】一些有用的网站
  • 原文地址:https://www.cnblogs.com/how2j/p/10971616.html
Copyright © 2011-2022 走看看