微服务架构——笔记(1)

微服务架构——笔记(1)

在这里插入图片描述
文章来源B站视频
尚硅谷SpringCloud框架开发教程(SpringCloudAlibaba微服务分布式架构丨Spring Cloud)教程

own process 独立部署
(1.微服务架构零基础理论)

叙述

马丁福勒 架构模式,倡导将单一应用程序划分为一组小的服务,服务之间相互协调,互相配合。每个服务在其独立的进程中,服务间采用轻量级的通信机制相相互协作(HTTPrestfulapi),应用根据业务上下层。

背景

生活箱包、平板电脑、智能家电、笔记本、手机

基于分布式的微服务架构

一、SpringCloud

NetFlix 分布式微服务架构的一站式架构,

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

上:SpringBoot2.X 版本 和SpringCloud H版本 19 年后必须2.0

下:SpringCould Alibaba

(2.从2.2.x和H版本开始)

二、环境

熟读官方文档,推荐

cloud hoxton.sr1 mysql 5.7

boot 2.2.2 release cloud alibaba 2.1.0 release

java java8 mavan 3.5

(3.关于Cloud各组件的停更)

2.1项目创建

工程名字设置,英文微服务架构——笔记(1)_第1张图片

2.2 环境更改

字符编码更改
微服务架构——笔记(1)_第2张图片
注解激活
微服务架构——笔记(1)_第3张图片
java8设置
微服务架构——笔记(1)_第4张图片
文件过滤微服务架构——笔记(1)_第5张图片

2.3 父pom.xml

Maven使用dependencyManagement元素提供管理依赖版本号的方式,通常在组织或项目的最顶层父pom中看到此元素。
能让所有子项目引用一个依赖而不用列出版本号,Maven会沿着父子层向上找到有dependencyManagement元素项目,使用这个版本号,先用父类再用子类。
聚合统一更改,一处更改不需要处处声明
version、scope都取自父类

2.4 application.yml

server:
  port: 8081

spring:
  application:
    name: cloud-payment-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource  #当前数据源操作类型
    driver-class-name: org.gjt.mm.mysql.Driver                 #mysql驱动包
    url: jdbc:mysql://localhost:3306/db2023?useUnicode=true&&characterEncoding=utf-8&useSSL=false
    username: root
    password: tiger

mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: com.liangstar.springcloud.entities   #所有Entity别名类所在包

2.5 spring启动类

package com.liangstar.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

三、微服务架构编码构建

(4.微服务架构编码构建) 建module、改pom、写yml、主启动、业务类
微服务架构——笔记(1)_第6张图片

以上在二、环境模块进行了:
1、项目的配置遵循(配置>环境>编码)、 版本、工程名字、utf-8、注解代替html文件、java8、环境
2、Rest微服务工程构建
3、技术架构:vue – | json | – controller - service - dao - mysql
CommonResult{200,success}

以下对程序业务类进行说明:
1.建表SQL、2.entities、3.dao、4.service、5.controller

3.1 数据库构建

create table 'payment'(
`id`bigint(20) not null auto_increment comment `id`,
`serial` varchar(200) default ``,
primary key(`id`)
)engint=innoDb auto_increment=1 default charset=utf8

3.2 创建entity

@Data   :注解在类上;提供类所有属性的 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法
@Setter:注解在属性上;为属性提供 setting 方法
@Getter:注解在属性上;为属性提供 getting 方法
@Log4j :注解在类上;为类提供一个 属性名为log 的 log4j 日志对象
@NoArgsConstructor:注解在类上;为类提供一个无参的构造方法
@AllArgsConstructor:注解在类上;为类提供一个全参的构造方法

序列化分布式部署用的到

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
    private Long id;
    private String serial;
}

封装、泛型

public class CommonResult<T>{
    private Integer code;
    private String message;
    private T 	    data;
    public CommonResult(Integer code,String message){
        this.(code,message,null);
    }
}

3.3 创建dao

读操作写操作 接口

@Mapper
public interface PaymentDao {

    public int create(Payment payment);

    public Payment getPaymentId(@Param("id") Long id);

}

微服务架构——笔记(1)_第7张图片微服务架构——笔记(1)_第8张图片

3.4 创建mapper

resource 下装配置文件,写映射mapper.xml
写落地实现类
微服务架构——笔记(1)_第9张图片
数据库插入成功返回1,大于0则成功,所以create类型为int,
命名不规范,做一个字段映射baseresultMap
PaymentMapper.xml





    
        insert into payment(serial) value (#{serial});
    
    
        
        
    
    


3.5 创建service

微服务架构——笔记(1)_第10张图片

3.5.1 实现接口PaymentService
public interface PaymentService {
    public int create(Payment payment);
    public Payment getPaymentId(@Param("id") Long id);
}
3.5.2 实现类PaymentServiceImpl

在这里插入图片描述
@AutoWirte是spring的
@Resource是java的也可以实现依赖注入

@Service
public class PaymentServiceImpl implements PaymentService {
    @Resource
    private PaymentDao paymentDao;

    public int create(Payment payment){
        return paymentDao.create(payment);
    }

    public Payment getPaymentId(Long id)1{
        return paymentDao.getPaymentId(id);
    }
}

3.5 创建controller

调用类、转发类
注解重写

@RestController
@Slf4j
public class PaymentController {
    @Resource
    private PaymentService paymentService;

    @PostMapping(value = "/payment/create")
    public CommonResult create(Payment payment){
        int result = paymentService.create(payment);
        log.info("********插入结果"+result);
        if(result > 0){
            return new CommonResult(200,"插入数据库成功",result);
        }else{
            return new CommonResult(444,"插入数据库失败",null);
        }
    }

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id){
       Payment payment = paymentService.getPaymentId(id);
        log.info("********查询结果"+payment);
        if(payment != null){
            return new CommonResult(200,"查询成功",payment);
        }else{
            return new CommonResult(444,"没有对应记录,查询Id"+id,null);
        }
    }
}

四、运行测试

微服务架构——笔记(1)_第11张图片
微服务架构——笔记(1)_第12张图片

五、热部署devtools

代码更改自动重启
微服务架构——笔记(1)_第13张图片

4.1 add devtools 添加工具

粘贴至子pom.xml中


      org.springframework.boot
      spring-boot-devtools
      runtime
      true
 

4.2 add plugin to pom.xml 添加插件

粘贴至父pom.xml中


    
      
        org.springframework.boot
        spring-boot-maven-plugin
        
          true
          true
        
      
    
 

4.3 Enabling automatic build 自动构建

开启自动编译权限
微服务架构——笔记(1)_第14张图片

4.4 update the value of 开启热注册

热键 :ctrl + shift + alt + /

微服务架构——笔记(1)_第15张图片
微服务架构——笔记(1)_第16张图片

4.5 重启

随着代码的更改,开发者可以不重启项目就可更改部署代码。
注:开发阶段需要热部署,生产阶段则不需要!

你可能感兴趣的:(JavaSpringCloud,架构,微服务,云原生)