微服务环境搭建SpringCloud入门

news2025/7/23 8:13:26

目录

案例准备

技术选型

模块设计

微服务调用

创建父工程

创建基础模块

创建用户微服务

创建商品微服务

创建订单微服务


我们本次是使用的电商项目中的商品、订单、用户为案例进行讲解。

案例准备

技术选型

maven:3.5.4 数据库:MySQL 5.7 持久层: SpingData Jpa/Mybatis-plus 其他: SpringCloud Alibaba 技术栈

模块设计

springcloud-shop父工程 shop-common 公共模块【实体类】 shop-user 用户微服务 【端口: 807x】 shop-product 商品微服务 【端口: 808x】 shop-order 订单微服务 【端口: 809x】

 

微服务调用

在微服务架构中,最常见的场景就是微服务之间的相互调用。我们以电商系统中常见的用户下单为 例来演示微服务的调用:客户向订单微服务发起一个下单的请求,在进行保存订单之前需要调用商品微 服务查询商品的信息。 我们一般把服务的主动调用方称为服务消费者,把服务的被调用方称为服务提供者。 在这种场景下,订单微服务就是一个服务消费者, 商品微服务就是一个服务提供者。

创建父工程

创建一个maven工程,然后在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>org.example</groupId>
    <artifactId>springcloud-shops</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>shop-common</module>
    </modules>
    <packaging>pom</packaging>
    <!--依赖版本的锁定-->
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.2.RELEASE</spring-boot.version>
        <spring-cloud.version>Hoxton.SR9</spring-cloud.version>
        <spring-cloud-alibaba.version>2.2.6.RELEASE</spring-cloud-alibaba.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <!-- SpringBoot 依赖配置 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

 

版本对应:

版本说明 · alibaba/spring-cloud-alibaba Wiki · GitHub

 

创建基础模块

1 创建shop-common 模块,在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">
    <parent>
        <artifactId>springcloud-shops</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>shop-common</artifactId>

    <!--依赖-->
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.56</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
        </dependency>
    </dependencies>

</project>

2 创建实体类

//用户
@Entity(name = "shop_user")//实体类跟数据表的对应
@Data//不再去写set和get方法
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)//数据库自增
    private Integer uid;//主键
    private String username;//用户名
    private String password;//密码
    private String telephone;//手机号
}
//商品
@Entity(name = "shop_product")
@Data
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer pid;//主键

    private String pname;//商品名称
    private Double pprice;//商品价格
    private Integer stock;//库存
}
//订单@Entity(name = "shop_order")@Datapublic class Order {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    private Long oid;//订单id​    //用户    private Integer uid;//用户id    private String username;//用户名​    //商品    private Integer pid;//商品id    private String pname;//商品名称    private Double pprice;//商品单价​    //数量    private Integer number;//购买数量}

创建用户微服务

步骤: 1 创建模块 导入依赖 2 创建SpringBoot主类 3 加入配置文件 4 创建必要的接口和实现类(controller service dao) 新建一个shop-user 模块,然后进行下面操作 1 创建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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <artifactId>springcloud-shops</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>shop-user</artifactId>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <!--springboot-web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--shop-common-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>shop-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

2 编写主类

package com.zjy.shopuser;

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

@SpringBootApplication
public class ShopUserApplication {

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

}

3 创建配置文件(没有连接数据库的话server下面的删掉)

spring:
    application:
        name: shop-user
server:
    port: 8071

datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql:///shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
    username: root
    password: sasa
  jpa:
    properties:
      hibernate:
        hbm2ddl:
          auto: update
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect

4 编写控制层

package com.zjy.shopuser.controller;

import com.zjy.model.User;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zjy
 * @site Bi8boYin
 * @company xxx公司
 * @create  2022-11-25 15:27
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/get/{id}")
    public User get(@PathVariable("id") Integer id){
        return new User(id,"小猪","xiaozhu111","17346958024");
    }
}

 

创建商品微服务

1 创建一个名为shop-product 的模块,并添加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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <artifactId>springcloud-shops</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>shop-product</artifactId>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <!--springboot-web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--shop-common-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>shop-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

2 创建工程的主类

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

3 创建配置文件application.yml

spring:
    application:
        name: shop-product
server:
    port: 8081
datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql:///shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
    username: root
    password: sasa
  jpa:
    properties:
      hibernate:
        hbm2ddl:
          auto: update
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect

4、创建controller

package com.zjy.shopproduct.controller;

import com.zjy.model.Product;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zjy
 * @site Bi8boYin
 * @company xxx公司
 * @create  2022-11-25 16:28
 */

@RestController
@RequestMapping("/product")
public class ProductController {

    @RequestMapping("/get/{pid}")
    public Product get(@PathVariable("pid") Integer pid){
        return  new Product(pid,"小猪历险记",100d,23);
    }
}

 

创建订单微服务

1 创建一个名为shop-order 的模块,并添加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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <artifactId>springcloud-shops</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>shop-order</artifactId>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <!--springboot-web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--shop-common-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>shop-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>


</project>

2 创建工程的主类

package com.zjy.shoporder;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class ShopOrderApplication {

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

    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

3、yml文件

server:
  port: 8091
spring:
  application:
    name: shop-order
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql:///shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
    username: root
    password: sasa
  jpa:
    properties:
      hibernate:
        hbm2ddl:
          auto: update
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect

4编写controller层

package com.zjy.shoporder.controller;

import com.zjy.model.Order;
import com.zjy.model.Product;
import com.zjy.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author zjy
 * @site Bi8boYin
 * @company xxx公司
 * @create  2022-11-25 16:54
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;
    @RequestMapping("/get/{uid}/{pid}")
    public Order get(@PathVariable("uid") Integer uid,
                     @PathVariable("pid") Integer pid){
        /**
         * 要在订单微服务中去调用 用户微服务 以及我们的 商品微服务 ,也就以为这跨项目调用
         httpClients
         */
        User user = restTemplate.getForObject("http://localhost:8071/user/get/" + uid, User.class);
        Product product = restTemplate.getForObject("http://localhost:8081/product/get/" + pid, Product.class);
        Order order=new Order();
        order.setUsername(user.getUsername());
        order.setUid(user.getUid());
        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setOid(System.currentTimeMillis());
        order.setNumber(product.getStock());
        return order;
    }

}

 

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

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

相关文章

【矩阵论】4. 矩阵运算——广义逆——广义逆的计算

4.3.2 AA^A 计算 a. 秩1公式 若A(aij)mn,r(A)1,则A1∑∣aij∣2AH1tr(AHA)AH\begin{matrix} 若A(a_{ij})_{m\times n},r(A)1,则A^\frac{1}{\sum \vert a_{ij}\vert^2}A^H\frac{1}{tr(A^HA)}A^H \end{matrix} 若A(aij​)mn​,r(A)1,则A∑∣aij​∣21​AHtr(AHA)1​AH​ AHAA^HAA…

Qt Quick 用cmake怎么玩子项目

以往在公司开发众多的项目中&#xff0c;都会出现要求本项目里部分功能模块代码需要具备保密性。如果需要对外输出demo工程&#xff0c;那么需要做到不会泄密。 举一下爪子&#xff0c;以前做雷达开发的时候&#xff0c;客户从公司那儿买了这些雷达模块&#xff0c;也会需要从…

信号量的使用

信号量 英文名字&#xff1a;semaphore 这里的进程信号量会为下列的信号量打基础 Ucos系统的信号量c线程的信号量java进程和线程的信号量 信号量作用 当多个进程/线程进行共享操作时&#xff0c;用于资源保护&#xff0c;以防止出现相互干扰的情况 信号量用于“资源的保护“ …

图论算法大合集【包括图的dfs和bfs遍历】【欧拉回路】【判断连通图】【Dijkstra算法】【floyd算法】【最小生成树prim算法】【拓扑排序】

图论算法大合集一. dfs和bfs 过程中要有visited数组标记已遍历过的节点6-1.1 邻接矩阵存储图的深度优先遍历6-1.2 邻接表存储图的广度优先遍历二、欧拉回路&#xff08;度为偶数&#xff0c;且为连通图&#xff09;6-1.3 哥尼斯堡的“七桥问题”三、判断连通图6-1.4 地下迷宫探…

进程调度的基本关系

文章目录1.什么是进程(process)2.进程的特性1.进程是非常重要的"软件资源"2.PCB(进程控制块)描述了哪些进程特征3.并行和并发4.进程的虚拟地址空间和进程间通信1.什么是进程(process) 简单来说就是:一个程序跑起来就是一个进程 一个应用没跑起来叫做程序,跑起来了就…

堆排序算法

一、大顶堆和小顶堆概念 堆排序是利用堆数据结构而设计的一种排序算法&#xff0c;堆排序是一种选择排序&#xff0c;其最坏&#xff0c;最好&#xff0c;平均时间复杂度均为O(nlogn)&#xff0c;同时也是不稳定排序。 堆是具有以下性质的完全二叉树&#xff1a;每个结点的值都…

Hive数据定义语言-DDL-建表高阶语法(内外部、分区、分桶、事务、视图、物化视图)

文章目录1. 内部表、外部表1.1 内部表1.2 外部表1.3 内、外部表差异1.4 Location关键字的作用2. 分区表-Partitioned Tables2.1 概念2.2 创建2.3 分区表数据加载2.3.1 静态分区2.3.2 动态分区2.4 注意事项3. 分桶表-Bucketed Tables3.1 概念3.2 规则3.3 语法3. 事务表-Transact…

编译openMVG出现的错误的解决

作者&#xff1a;朱金灿 来源&#xff1a;clever101的专栏 为什么大多数人学不会人工智能编程&#xff1f;>>> submodule(s) are missing, please update your repository 在使用CMake生成openMVG解决方案时&#xff0c;出现错误&#xff1a; CMake Error at CMakeL…

另眼看数据中台

目录 前言&#xff1a; 一、故事的开始 二、成也中台&#xff0c;败也中台 1、中台是什么 2、数据中台与企业数字化转型 3、中台的赋能 4、数据中台、业务中台、技术中台 5、中台不一定适合你 ​编辑​编辑小结&#xff1a; 三、 自检数据应用的成熟度 前言&#xff1…

Python学习基础笔记十二——文件

1、目的&#xff1a;是要将数据永久地保存下来&#xff0c;就需要将数据永久保存在硬盘中。 2、概念&#xff1a;文件就是操作系统提供给应用程序来操作硬盘虚拟接口&#xff0c;用户或应用程序通过操作文件&#xff0c;可以将自己的数据永久地保存下来。 3、步骤&#xff1b; …

MACU-Net-用于精细分辨率遥感图像语义分割网络

摘要&#xff1a; 在本文中我们结合了由不同层次的U-Net生成的多尺度特征&#xff0c;设计了一个多尺度跳跃连接和基于非对称卷积的网络--MACU-Net。 网络具有以下几个优点1&#xff09;多尺度跳跃连接将低层和高层特征图中包含的语义特征结合并重新进行排列2&#xff09;非对…

一文搞懂傅里叶级数与变换

描述 这篇文章的目标&#xff1a;以最简单易懂的方式&#xff0c;让大家学会傅里叶变换&#xff01; 为什么要写一篇关于傅里叶变换相关知识的文章呢。有两个原因&#xff1a; 一、这个知识很有趣&#xff0c;可以理解它是一件炫酷的事情。在工作中&#xff0c;一次分享会同事…

BufferPool缓存机制

BufferPool缓存机制 1、更新数据流程 流程图 sql更新数据刷到磁盘前会经过serve层 连接器-管理连接和权限校验优化器-语法词法分析优化器-执行计划生成索引选择执行器-连接bufferPool 1.1 流程步骤 从磁盘加载数据到buffer pool&#xff0c;会先去判断要更新的数据所在数据…

Hantek6022BE 虚拟示波器

​ 0. Hantek 厂家提供的介绍 安装方法按照说明来。 很多人都说原厂的软件不好用&#xff0c;于是折腾就开始了&#xff1a; 1. VIRTINS Multi-Instrument 这个别人已经写的很详细了 大概需要准备 Multi-Ins 这软件&#xff0c;目前找到 3.7 的合用 菜单里 添加设备到库&a…

HTML+CSS详细知识点复习(上)

文章目录一、初识HTML1、标签概述二、初识CSS1、CSS核心基础2、设置文本样式3、高级特性4、CSS的优先级三、CSS选择器1、关系选择器四、盒子模型1、边框属性2、边距属性3、背景属性4、盒子的宽与高5、CSS3新增盒子模型属性一、初识HTML HTML&#xff08;超文本标记语言&#x…

用Python采集世界杯球队热搜数据 并发送邮箱通知

前言 嗨嗨&#xff0c;最近世界杯的热度可是一直在增长啊 待会就是 卡塔尔和塞内加尔打了 怎么说 还是有点期待结果的 趁现在有点无聊 就想着用Python采集世界杯球队热搜数据 顺便 发送邮箱通知 话不多说&#xff0c;马上开始 知识点 动态数据抓包requests发送请求json数据…

【Proteus仿真】【51单片机】智能雨刷器设计

文章目录一、主要功能二、硬件资源三、软件设计四、实验现象联系作者一、主要功能 本项目使用Proteus8仿真51单片机控制器&#xff0c;使用LCD1602液晶模块、按键模块、雨滴传感器、ADC、LED模块等。 主要功能&#xff1a; 系统运行后&#xff0c;LCD1602显示雨刷器当前模式、…

如何制定测试团队度量体系

1、前言 每当月底或一个季度结束&#xff0c;公司或项目都会进行考核指标的统计&#xff0c;来总结每个组员在这个阶段的工作产出与绩效成绩。 那么制定哪些指标最为标准&#xff0c;最为专业&#xff0c;同时针对整个项目组都是公平的&#xff0c;这个就需要每个公司或项目根…

Python入门

目录 一、Python安装及环境搭建 二、Python运用&#xff08;数据类型&#xff09; 基本数据类型 引用数据类型 一、Python安装及环境搭建 Python环境安装包下载 https://www.python.org/ https://www.python.org/downloads/windows/ Python开发工具PyCharm下载 https://www.…

南卡和FIIL 哪个更好用?南卡和FIIL CC nano蓝牙耳机对比测评

作为一个狂热的蓝牙爱好者&#xff0c;我也用了不少蓝牙耳机了&#xff0c;真的是对蓝牙耳机爱不释手。自从蓝牙耳机出现以来&#xff0c;我们都看到了它的迅速发展&#xff0c;尤其是这两年&#xff0c;蓝牙耳机越来越受欢迎&#xff0c;已经取代了传统的有线耳机&#xff0c;…