[阶段4 企业开发进阶] 7. 微服务

news2024/9/23 23:35:22

文章目录

  • 1 微服务
    • 1.1 微服务概念
    • 1.2 SpringCloud
    • 1.3 工程搭建
    • 1.4 支付模块构建
      • cloud-provider-payment-8001

1 微服务

1.1 微服务概念

概念

Microservice architectures are the ‘new normal’. Building small, self-contained, ready to run applications can bring great flexibility and added resilience to your code. Spring Boot’s many purpose-built features make it easy to build and run your microservices in production at scale. And don’t forget, no microservice architecture is complete without Spring Cloud ‒ easing administration and boosting your fault-tolerance.

微服务是一种架构模式,提倡将单一应用程序划分成一组小的服务,服务之间互相协调、互相配合,为用户提供最终价值。每个服务运行在其独立的进程中,服务之间采用轻量级的通信机制互相协作。每个服务都围绕具体业务构建,并且能够独立的部署到生产环境、类生产环境等。

在这里插入图片描述

在这里插入图片描述

1.2 SpringCloud

SpringCloud:

Spring Cloud can help with service discovery, load-balancing, circuit-breaking, distributed tracing, and monitoring. It can even act as an API gateway.

分布式微服务架构的一站式解决方案,是多种微服务架构落地技术的集合体

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

官方文档

(截至2020年份) Cloud升级

服务名使用状况
服务注册中心Eureka(×)、Zookeeper、Consul、Nacos
服务调用Ribbon、LoadBalancer | Feign(×)、OpenFeign
服务降级Hystrix(×)、resilience4j、sentinel
服务网关Zuul(×)、gateway
服务配置Config(×)、Nacos
服务总线Bus(×)、Nacos

1.3 工程搭建

约定 > 配置 > 编码

1.新建maven工程

2.检查字符编码是否正确
在这里插入图片描述

3.注解生效激活
在这里插入图片描述

4.检查jdk配置

5.配置依赖

<?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>com.cyan</groupId>
    <artifactId>cloud</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <!-- 统一管理jar包版本 -->
    <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>
        <junit.version>4.12</junit.version>
        <log4j.version>1.2.17</log4j.version>
        <lombok.version>1.16.18</lombok.version>
        <mysql.version>5.1.47</mysql.version>
        <druid.version>1.1.16</druid.version>
        <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
    </properties>

    <!-- 子模块继承后:锁定版本+子模块无需写组名和版本号 -->
    <dependencyManagement>
        <dependencies>
            <!-- springboot 2.2.2 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- SpringCloud Hoxton.SR1-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- SpringCloud Alibaba 2.1.0 RELEASE -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- mysql -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
                <scope>runtime</scope>
            </dependency>
            <!-- druid-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>
            <!-- mybatis -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis.spring.boot.version}</version>
            </dependency>
            <!-- junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
            </dependency>
            <!-- log4j -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <!-- lombok -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
                <optional>true</optional>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

知识补充

dependencyManagement

maven使用dependencyManagement来提供一种管理依赖版本号的方式

通常在一个组织或项目的最顶层的父POM看到dependencyManagement元素

使用pom.xml中的dependencyManagement元素能让所有在子项目中引用一个依赖而不用显示列出版本号,maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用dependencyManagement元素指定的版本号

优势: 多个子项目引用同样依赖,可以避免在每个子项目中声明一个版本号,若升级或切换其他版本号,仅需要在顶层父容器更新,不需要一个一个子项目的修改;另外若某子项目需要另外版本,仅需要声明version

  • dependencyManagement只是声明依赖,并不实现引入,因此项目需要显示声明需要的依赖
  • 若不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取父pom
  • 如果子项目中指定了版本号,那么会使用子项目中指定的jar版本

父工程创建完成执行mvn:install将父工程发布到仓库方便子工程继承

1.4 支付模块构建

在这里插入图片描述

微服务模块构建

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

cloud-provider-payment-8001

1.建cloud-provider-payment-8001

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud</artifactId>
        <groupId>com.cyan</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-provider-payment-8001</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

3.配置YML

server:
  port: 8001

spring:
  application:
    name: cloud-payment-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root


mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.cyan.springcloud.entities

4.主启动搭建

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

5.数据库搭建

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=utf8

6.业务逻辑

实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {

    private Long id;
    private String serial;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {

    private Integer code;
    private String message;
    private T data;

    public CommonResult(Integer code, String message) {
        this(code, message, null);
    }
}

数据访问层

@Mapper
public interface PaymentDao {

    int create(Payment payment);

    Payment getPaymentById(@Param("id") Long id);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cyan.springcloud.dao.PaymentDao">
    <insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO payment(serial) VALUES(#{serial});
    </insert>

    <resultMap id="BaseResultMap" type="com.cyan.springcloud.entities.Payment">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <id column="serial" property="serial" jdbcType="VARCHAR"/>
    </resultMap>
    <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
        SELECT * FROM payment WHERE id = #{id};
    </select>
</mapper>

业务逻辑层

public interface PaymentService {

    int create(Payment payment);

    Payment getPaymentById(Long id);
}

@Service
public class PaymentServiceImpl implements PaymentService {

    @Resource
    private PaymentDao paymentDao;

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

    @Override
    public Payment getPaymentById(Long id) {
        return paymentDao.getPaymentById(id);
    }
}

控制器层

@Slf4j
@RestController
@RequestMapping(value = "/payment")
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @PostMapping(value = "/create")
    public CommonResult create(Payment payment) {
        int result = paymentService.create(payment);
        log.info("插入结果:{}", result);
        if (result > 0) {
            return new CommonResult(200, "Insert Success", result);
        } else {
            return new CommonResult(500, "Insert Failed", null);
        }
    }

    @GetMapping(value = "/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id) {
        Payment payment = paymentService.getPaymentById(id);
        log.info("查询结果:{}", payment);

        if (payment != null) {
            return new CommonResult(200, "Query Success", payment);
        } else {
            return new CommonResult(404, "Query Failed" + id, null);
        }
    }
}

结论 测试成功

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/98413.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

毕业设计 Stm32家庭气象仪 天气监控系统 - 物联网 单片机 嵌入式

文章目录0 前言1 简介2 主要器件3 实现效果4 设计原理4.1 DHT11温湿度传感器4.2 MQ135空气质量传感器4.35 部分核心代码6 最后0 前言 &#x1f525; 这两年开始毕业设计和毕业答辩的要求和难度不断提升&#xff0c;传统的毕设题目缺少创新和亮点&#xff0c;往往达不到毕业答辩…

安全分析模型自动化调优

安全分析模型自动化调优 MLOps&#xff08;Machine Learning Operations&#xff09;是一种人工智能 的工程实践&#xff0c;是面向机器学习项目的研发运营管理体系 。旨在实现 ML 管道的操作、ML 模型的部署和管理标准化&#xff0c;支持ML 模型的发布、激活、监控、性能跟踪…

Stm32旧版库函数18——读传感器

/******************************************************************************* // // 使用单片机STM32F100C8T6 // 晶振&#xff1a;8.00M // 编译环境 Keil uVision4 // 在3.3V的供电环境下&#xff0c;就能运行 // 波特率 19200 ************************************…

SpringBoot请求参数传递与接收

一、GET请求和POST请求的区别是什么 GET和POST请求的区别主要有下面几点 GET没有请求体&#xff0c;POST有请求体GET传输比POST快GET只能携带少量数据&#xff08;因为其请求url有长度限制&#xff09;&#xff0c;POST可以携带的数据量较大POST因为将数据放在请求体中&#…

使用IDEA工具通过Java API 访问HDFS

文章目录一&#xff0c;了解 HDFS Java API&#xff08;一&#xff09;HDFS常见类与接口&#xff08;二&#xff09;FileSystem的常用方法二&#xff0c;编写Java程序访问HDFS01 创建Maven项目02 添加相关依赖03 创建日志属性文件&#xff08;1&#xff09;在resources目录里创…

变电站火灾检测项目(tf2)

目录 1. 项目背景 2. 项目研究数据集介绍&#xff08;变电站火灾检测图像数据集&#xff09; 3. 目标检测模型介绍&#xff08;SE改进的YOLOv4-tiny模型&#xff09; 4. 模型训练及测试 1. 项目背景 我们的日常生活与电力息息相关&#xff0c;变电站作为输配电系统的关键环…

【设计模式】责任链模式

【设计模式】责任链模式 文章目录【设计模式】责任链模式一&#xff1a;责任链模式概述二&#xff1a;责任链模式结构三&#xff1a;责任链模式案例实现四&#xff1a;优缺点五&#xff1a;责任链模式实战一&#xff1a;责任链模式概述 在现实生活中&#xff0c;常常会出现这样…

Jmeter(二十一):jmeter导入和导出接口的处理

JMeter测试导入接口 利用Jmeter测试上传文件&#xff0c;首先可根据接口文档或者fiddler抓包分析文件上传的接口&#xff1b;如下图&#xff1a; 以下是我通过fiddler所截取的文件上传的接口 1、填写导入接口的信息 查看文件上传栏下的填写信息&#xff1a; 文件名称&#xf…

【图像分割】多种阈值图像分割(带面板)【含GUI Matlab源码 733期】

⛄一、图像分割简介 理论知识参考&#xff1a;【基础教程】基于matlab图像处理图像分割【含Matlab源码 191期】 ⛄二、部分源代码 function varargout yuzhifenge(varargin) % YUZHIFENGE MATLAB code for yuzhifenge.fig % YUZHIFENGE, by itself, creates a new YUZHIFEN…

【Redis】缓存问题

用户数据一般都是存储在数据库中&#xff0c;数据库则落在磁盘上。而磁盘的I/O速度是计算机中最慢的硬件。 当用户的访问量在某一个时间段突然上升&#xff0c;数据库就很容易崩溃。为了避免用户直接访问数据库&#xff0c;所以会使用缓存数据库(Redis)作为缓冲层。 Redis 是内…

DIY NAS服务器之OMV 5.6入坑指南(二)- 安装omv-extras插件

DIY NAS服务器之OMV 5.6入坑指南&#xff08;一&#xff09;-openmediavalut 5.6安装 前面我们已经安装好了OMV5.6了。 接下来就是建设我们的OMV系统了。 首先第一步&#xff0c;我们开启社区插件omv-extras&#xff1a; 通过如下url把插件安装文件 下载到电脑本地&#xff0c…

Mysql分布式锁(二)直接用一条sql语句来实现原子性

文章目录一、直接用更新的sql实现1. 场景描述2. 修改sqlStockMapperStockService3. 重新测试二、问题1. 锁范围问题&#xff1a;行级锁 表级锁2. 若逻辑太复杂&#xff0c;一个sql无法实现4. 无法监控库存变化前后的状态一、直接用更新的sql实现 1. 场景描述 之前的deduct()方…

DevOps实战系列【第十三章】:流水线应用工具Blue Ocean使用

个人亲自录制全套DevOps系列实战教程 &#xff1a;手把手教你玩转DevOps全栈技术 BlueOcean图形化工具 可以通过插件的方式安装到jenkins&#xff0c;搜索“Blue Ocean”&#xff0c;安装后重启即可。 由于兼容问题&#xff0c;BlueOcean依赖的插件有些是失败的&#xff0c;我…

实际生产中使用Oracle的小问题及解决方法记录:ORA-00911,ORA-12514,ORA-28547

背景 在上次安装并初步测试 Oracle 后Oracle 11g安装使用、备份恢复并与SpringBoot集成&#xff0c;在实际生产中使用 Oracle 时又遇到几个小问题&#xff1a; ORA-00911 &#xff0c; ORA-12514 &#xff0c; ORA-28547 。下面分别列出这几个问题的解决方法。 Note&#xff…

DCBC路由模式配置端口映射

DCBC路由模式配置端口映射 拓扑搭建 前提&#xff1a;使用DCBC、CS6200、两台PC机&#xff1a;一台用户配置IP另一台用于测试端口映射&#xff0c;外网环境使用192.168.19.0/24网段 网段划分 全网互通 1.DCBC对应工作模式配置 对应接口配置 配置静态路由两条&#xff0c;一条…

ArcGIS And ENVI:如何进行植被指数的提取并制作成专题地图?

目录 01 目的 02 操作步骤 2.1 在ENVI加载tm_860516.img文件 2.2 进行NDVI指数的计算 2.3 使用ArcGIS对NDVI植被指数提取图进行专题制作 2.3.1 加载NDVI植被指数提取图 2.3.2 对NDVI植被指数提取图进行重分类 2.3.3 布局视图下的NDVI植被指数的相关编辑 03 实验结果 01 目的 对…

Iterated function

In mathematics, an iterated function is a function X → X (that is, a function from some set X to itself) which is obtained by composing another function f : X → X with itself a certain number of times. The process of repeatedly applying the same function…

【数据结构与算法 - 数据结构基础】什么是数据结构?

【数据结构与算法 - 数据结构基础】什么是数据结构&#xff1f; 文章目录【数据结构与算法 - 数据结构基础】什么是数据结构&#xff1f;1 数据结构包含的三个方面1.1 数据的逻辑结构1.1.1 线性结构数组【Array】链表【LinkedList】栈【Stack】队列【Queue】1.1.2 树结构【Tree…

【生信】初探基因定位和全基因组关联分析

初探QTL和GWAS 文章目录初探QTL和GWAS实验目的实验内容实验题目第一题&#xff1a;玉米MAGIC群体的QTL分析第二题&#xff1a;TASSEL自带数据集的关联分析实验过程玉米MAGIC群体的QTL分析① 包含的数据② 绘制LOD曲线株高对应的QTLTASSEL自带数据集的关联分析TASSEL简介实际操作…

Docker ( 一 ) 基本概念及安装

1.Docker是什么&#xff1f; Docker 是一个开源的应用容器引擎, 可以简化理解实现应用与运行环境分离. Docker 其中包括&#xff0c;镜像、容器、仓库等概念&#xff0c;目的就是通过对应用组件的封装、分发、部署、运行等生命周期的管理&#xff0c;使用户的产品&#xff08…