Springcloud_微服务架构理论入门

1.微服务架构理论入门

微服务概念

  • 微服务是一种架构风格
  • 一个应用拆分为一组小型服务
  • 每个服务运行在自己的进程内,也就是可独立部署和升级
  • 服务之间使用轻量级HTTP交互
  • 服务围绕业务功能拆分
  • 可以由全自动部署机制独立部署
  • 去中心化,服务自治。服务可以使用不同的语言、不同的存储技术

分布式微服务架构-落地维度

img

  • 服务调用
  • 服务降级
  • 服务注册与发先
  • 服务熔断
  • 负载均衡
  • 服务消息队列
  • 服务网关
  • 配置中心管理
  • 自动化构建部署
  • 服务监控
  • 全链路追踪
  • 服务定时任务
  • 调度操作

Spring Cloud简介

SpringCloud分布式微服务架构的站式解决方案,是多种微服务架构落地技术的集合体,俗称微服务全家桶

img

Spring Cloud技术栈

netflix

img

img

2.Boot和Cloud版本选型

Spring Boot 2.X 版

Spring Cloud 2021.0.x版

Spring Boot 与 Spring Cloud 兼容性查看

  • 文档

  • JSON接口

  • "spring-cloud": {
        "Hoxton.SR12": "Spring Boot >=2.2.0.RELEASE and <2.4.0.M1",
        "2020.0.6": "Spring Boot >=2.4.0.M1 and <2.6.0-M1",
        "2021.0.0-M1": "Spring Boot >=2.6.0-M1 and <2.6.0-M3",
        "2021.0.0-M3": "Spring Boot >=2.6.0-M3 and <2.6.0-RC1",
        "2021.0.0-RC1": "Spring Boot >=2.6.0-RC1 and <2.6.1",
        "2021.0.3": "Spring Boot >=2.6.1 and <3.0.0-M1",
        "2022.0.0-M1": "Spring Boot >=3.0.0-M1 and <3.0.0-M2",
        "2022.0.0-M2": "Spring Boot >=3.0.0-M2 and <3.0.0-M3",
        "2022.0.0-M3": "Spring Boot >=3.0.0-M3 and <3.1.0-M1"
    }
    

接下来开发用到的组件版本

  • Cloud - 2021.0.3
  • Boot - 2.7.1
  • Cloud Alibaba - 2021.0.1.2
  • Java - Java 8
  • Maven - 3.8.4
  • MySQL - 5.7.37

3.Cloud组件停更说明

image-20220716154803617

Spring Cloud官方文档

Spring Cloud中文文档

Spring Boot官方文档

Spring Boot 中文文档

4.父工程Project空间新建

约定 > 配置 > 编码

创建微服务cloud整体聚合父工程Project,有8个关键步骤:

  1. New Project - maven工程 - create from archetype: maven-archetype-site
  2. 聚合总父工程名字
  3. Maven选版本
  4. 工程名字
  5. 字符编码 - Settings - File encoding
  6. 注解生效激活 - Settings - Annotation Processors
  7. Java编译版本选8
  8. File Type过滤 - Settings - File Type

5.父工程pom文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>pers.itqiqi</groupId>
    <artifactId>springcloud-study</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springcloud-study</name>
    <description>springcloud-study</description>
    <packaging>pom</packaging>

    <modules>
        <module>cloud-provider-payment8001</module>
        <module>cloud-admin12000</module>
        <module>cloud-consumer-order80</module>
        <module>cloud-eureka-server7001</module>
        <module>cloud-eureka-server7002</module>
        <module>cloud-eureka-server7003</module>
    </modules>

    <properties>
        <java.version>1.8</java.version>
        <spring.boot.version>2.7.1</spring.boot.version>
        <spring-cloud.version>2021.0.3</spring-cloud.version>
        <spring-boot-admin.version>2.7.1</spring-boot-admin.version>
        <mybatis-plus.version>3.5.2</mybatis-plus.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>pers.itqiqi.springcloud-study</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-dependencies</artifactId>
                <version>${spring-boot-admin.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>

    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                        <exclude>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-devtools</artifactId>
                        </exclude>
                        <exclude>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-configuration-processor</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

6.创建spring-boot-admin服务监控

  1. 建名为cloud-admin12000的Maven工程

  2. 改POM

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <artifactId>springcloud-study</artifactId>
            <groupId>pers.itqiqi</groupId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
    
        <artifactId>cloud-admin12000</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-starter-server</artifactId>
            </dependency>
        </dependencies>
    </project>
    
  3. 改application.yml

    server:
      port: 12000
    spring:
      application:
        name: cloud-admin12000
    
  4. 在主启动类添加注解

    @EnableAdminServer
    

7.支付模块构建

创建微服务模块套路:

  1. 建Module
  2. 改POM
  3. 写YML
  4. 主启动
  5. 业务类

image-20220716192356968

  1. sql

    CREATE TABLE `payment`(
    	`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
        `serial` varchar(200) DEFAULT '',
    	PRIMARY KEY (id)
    )ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4
    
    
  2. 建名为cloud-provider-payment8001的Maven工程

  3. 改POM

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <artifactId>springcloud-study</artifactId>
            <groupId>pers.itqiqi</groupId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
    
        <artifactId>cloud-provider-payment8001</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-starter-client</artifactId>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>
        </dependencies>
    
    </project>
    
  4. 创建Payment实体类

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @TableName("payment")
    public class Payment {
    
        @TableId
        private Long id;
        @TableField("serial")
        private String serial;
    
    }
    
  5. 创建CommonResult实体类

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class CommonResult {
    
        private Integer code;
        private String msg;
        private Object data;
    
        public CommonResult(Integer code, String msg) {
            this(code, msg, null);
        }
    }
    
  6. 创建dao层

    @Mapper
    public interface PaymentDao extends BaseMapper<Payment> {}
    
  7. 创建service层

    public interface PaymentService extends IService<Payment> {}
    
    @Service
    public class PaymentServiceImpl extends ServiceImpl<PaymentDao, Payment> implements PaymentService {}
    
  8. 编写controller层

    @RestController
    @Slf4j
    public class PaymentController {
    
        @Autowired
        private PaymentService paymentService;
    
        /**
         * 添加payment记录
         * @param payment payment的json
         * @return 返回公共返回json
         */
        @PostMapping("/payment/add")
        public CommonResult addPayment(@RequestBody Payment payment) {
            boolean save = paymentService.save(payment);
            log.info("payment 是否添加成功:{}",save);
            if (save) {
                return new CommonResult(200, "添加成功!", payment);
            } else {
                return new CommonResult(401, "添加失败!", payment);
            }
        }
    
        /**
         * 根据id查询单条payment
         * @param id payment的id值
         * @return 返回公共返回json
         */
        @GetMapping("/payment/get/{id}")
        public CommonResult getPayment(@PathVariable Long id) {
            Payment payment = paymentService.getById(id);
            log.info("payment 是否查询成功:{}", payment);
            if (payment != null) {
                return new CommonResult(200, "查询成功!", payment);
            } else {
                return new CommonResult(401, "查询失败!");
            }
        }
    
    }
    

8.消费者订单模块

  1. 创建名为cloud-consumer-order80的maven工程

  2. 改POM

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <artifactId>springcloud-study</artifactId>
            <groupId>pers.itqiqi</groupId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
    
        <artifactId>cloud-consumer-order80</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-starter-client</artifactId>
            </dependency>
        </dependencies>
    
    </project>
    
  3. 配置application.yml

    server:
      port: 80
    spring:
      application:
        name: cloud-consumer-order80
      boot:
        admin:
          client:
            url: http://localhost:12000
            instance:
              service-host-type: ip
    management:
      endpoints:
        web:
          exposure:
            include: '*'
    
  4. 创建实体类

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Payment {
    
        private Long id;
        private String serial;
    
    }
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class CommonResult {
    
        private Integer code;
        private String msg;
        private Object data;
    
        public CommonResult(Integer code, String msg) {
            this(code, msg, null);
        }
    }
    
  5. 编写controller层

    @RestController
    @Slf4j
    public class PaymentController {
    
        public static final String PAYMENT_URL = "http://localhost:8001";
    
        @Autowired
        private RestTemplate restTemplate;
    
        @PostMapping("/consumer/payment")
        public CommonResult create(Payment payment) {
            return restTemplate.postForObject(PAYMENT_URL + "/payment/add", payment, CommonResult.class);
        }
    
        @GetMapping("/consumer/payment")
        public CommonResult getPayment(Long id){
            return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id, CommonResult.class);
        }
    
    }
    

9.工程重构

由于实体类有重复冗余代码,所有重构!

  1. 新建 - cloud-api-commons

  2. 改POM

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.7.1</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <groupId>pers.itqiqi.springcloud-study</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>cloud-api-commons</name>
        <description>cloud-api-commons</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>5.8.3</version>
            </dependency>
        </dependencies>
    
    </project>
    
  3. 复制pojo层代码

    image-20220717210811609

  4. maven打包并安装

    image-20220717211021209

  5. 父项目引入依赖

    <dependency>
        <groupId>pers.itqiqi.springcloud-study</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值