• 微服务环境搭建


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

    1 案例准备

    1.1 技术选型

    maven3.5.0+

    数据库:MySQL 5.7 以上

    持久层: Mybatis-plus 《Mybatis  Mapper Mybatis-plus》

    其他: SpringCloud Alibaba 技术栈  druid

    1.2 模块设计

    day0818-springcloud 父工程 ----jar的版本管理  公共jar的引入

    springcloud-common 公共模块【实体类】 《实体类,公共依赖,工具类。》

    springcloud-product 商品微服务 【端口: 8080~8089  搭建集群

    springcloud-order 订单微服务 【端口: 8090~8099  搭建集群

     其中pom.xml(day0818-springcloud)文件中

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <modules>
    6. <module>springcloud-commonmodule>
    7. <module>springcloud-ordermodule>
    8. <module>springcloud-productmodule>
    9. modules>
    10. <parent>
    11. <groupId>org.springframework.bootgroupId>
    12. <artifactId>spring-boot-starter-parentartifactId>
    13. <version>2.3.12.RELEASEversion>
    14. <relativePath/>
    15. parent>
    16. <groupId>com.lqhgroupId>
    17. <artifactId>day0818-springcloudartifactId>
    18. <version>0.0.1-SNAPSHOTversion>
    19. <name>day0818-springcloudname>
    20. <description>day0818-springclouddescription>
    21. <packaging>pompackaging>
    22. <properties>
    23. <java.version>1.8java.version>
    24. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    25. <project.reporting.outputEncoding>UTF- 8project.reporting.outputEncoding>
    26. <spring-cloud.version>Hoxton.SR8spring-cloud.version>
    27. <spring-cloud-alibaba.version>2.2.3.RELEASEspring-cloud-alibaba.version>
    28. properties>
    29. <dependencyManagement>
    30. <dependencies>
    31. <dependency>
    32. <groupId>org.springframework.cloudgroupId>
    33. <artifactId>spring-cloud-dependenciesartifactId>
    34. <version>${spring-cloud.version}version>
    35. <type>pomtype>
    36. <scope>importscope>
    37. dependency>
    38. <dependency>
    39. <groupId>com.alibaba.cloudgroupId>
    40. <artifactId>spring-cloud-alibaba-dependenciesartifactId>
    41. <version>${spring-cloud-alibaba.version}version>
    42. <type>pomtype>
    43. <scope>importscope>
    44. dependency>
    45. dependencies>
    46. dependencyManagement>
    47. <dependencies>
    48. <dependency>
    49. <groupId>mysqlgroupId>
    50. <artifactId>mysql-connector-javaartifactId>
    51. dependency>
    52. <dependency>
    53. <groupId>com.baomidougroupId>
    54. <artifactId>mybatis-plus-boot-starterartifactId>
    55. <version>3.5.2version>
    56. dependency>
    57. dependencies>
    58. <build>
    59. <plugins>
    60. <plugin>
    61. <groupId>org.springframework.bootgroupId>
    62. <artifactId>spring-boot-maven-pluginartifactId>
    63. plugin>
    64. plugins>
    65. build>
    66. project>

    版本对应:

    1 创建基础模块(Maven工程)  

    1.1springcloud-common

    1 创建 springcloud-common 模块,在pom.xml中添加依赖

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <parent>
    6. <artifactId>day0818-springcloudartifactId>
    7. <groupId>com.lqhgroupId>
    8. <version>0.0.1-SNAPSHOTversion>
    9. parent>
    10. <modelVersion>4.0.0modelVersion>
    11. <artifactId>springcloud-commonartifactId>
    12. <properties>
    13. <maven.compiler.source>8maven.compiler.source>
    14. <maven.compiler.target>8maven.compiler.target>
    15. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    16. properties>
    17. <dependencies>
    18. <dependency>
    19. <groupId>org.projectlombokgroupId>
    20. <artifactId>lombokartifactId>
    21. dependency>
    22. <dependency>
    23. <groupId>com.baomidougroupId>
    24. <artifactId>mybatis-plus-boot-starterartifactId>
    25. <version>3.5.2version>
    26. dependency>
    27. dependencies>
    28. project>

    2 创建实体类

    Order

    1. package com.lqh.entity;
    2. import com.baomidou.mybatisplus.annotation.IdType;
    3. import com.baomidou.mybatisplus.annotation.TableId;
    4. import com.baomidou.mybatisplus.annotation.TableName;
    5. import lombok.Data;
    6. @Data
    7. @TableName("shop_order")
    8. public class Order {
    9. @TableId(type = IdType.AUTO)
    10. private Long oid; //订单id
    11. private Integer uid;//用户id
    12. private String username;//用户名
    13. private Integer pid;//商品id
    14. private String pname;//商品名称
    15. private Double pprice;//商品价格
    16. private Integer number;//购买数量
    17. }

    Product

    1. package com.lqh.entity;
    2. import com.baomidou.mybatisplus.annotation.IdType;
    3. import com.baomidou.mybatisplus.annotation.TableId;
    4. import com.baomidou.mybatisplus.annotation.TableName;
    5. import lombok.Data;
    6. @Data
    7. @TableName(value="shop_product")
    8. public class Product {
    9. @TableId(type= IdType.AUTO)
    10. private Integer pid;
    11. private String pname;//商品名称
    12. private Double pprice;//商品价格
    13. private Integer stock;//库存
    14. }

     1.2 创建商品微服务springcloud-product

    1 创建一个名为springcloud-product 的模块,并添加springboot依赖 pom.xml

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <parent>
    6. <artifactId>day0818-springcloudartifactId>
    7. <groupId>com.lqhgroupId>
    8. <version>0.0.1-SNAPSHOTversion>
    9. parent>
    10. <modelVersion>4.0.0modelVersion>
    11. <artifactId>springcloud-productartifactId>
    12. <properties>
    13. <maven.compiler.source>8maven.compiler.source>
    14. <maven.compiler.target>8maven.compiler.target>
    15. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    16. properties>
    17. <dependencies>
    18. <dependency>
    19. <groupId>org.springframework.bootgroupId>
    20. <artifactId>spring-boot-starter-webartifactId>
    21. dependency>
    22. <dependency>
    23. <groupId>com.lqhgroupId>
    24. <artifactId>springcloud-commonartifactId>
    25. <version>0.0.1-SNAPSHOTversion>
    26. <scope>compilescope>
    27. dependency>
    28. <dependency>
    29. <groupId>com.alibaba.cloudgroupId>
    30. <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    31. dependency>
    32. dependencies>
    33. project>

    2 创建工程的主类 ProductApp

    1. package com.lqh;
    2. import org.mybatis.spring.annotation.MapperScan;
    3. import org.springframework.boot.SpringApplication;
    4. import org.springframework.boot.autoconfigure.SpringBootApplication;
    5. /**
    6. * @author: Li Qinghua
    7. * @create: 2022-08-18 13:50
    8. **/
    9. @SpringBootApplication
    10. public class ProductApp {
    11. public static void main(String[] args) {
    12. SpringApplication.run(ProductApp.class,args);
    13. }
    14. }

    3 创建配置文件application.yml

    1. server:
    2. port: 8081
    3. spring:
    4. datasource:
    5. url: jdbc:mysql://localhost:3306/springcloud?serverTimezone=Asia/Shanghai
    6. driver-class-name: com.mysql.cj.jdbc.Driver
    7. username: root
    8. password: 123456
    9. cloud:
    10. nacos:
    11. discovery:
    12. server-addr: localhost:8848
    13. enabled: true
    14. application:
    15. name: product
    16. mybatis-plus:
    17. configuration:
    18. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

    4 创建ProductDao接口

    1. package com.lqh.dao;
    2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    3. import com.lqh.entity.Product;
    4. import org.apache.ibatis.annotations.Mapper;
    5. @Mapper
    6. public interface ProductDao extends BaseMapper {
    7. }

    5 创建ProductService接口

    1. package com.lqh.server;
    2. import com.lqh.entity.Product;
    3. public interface ProductServer {
    4. Product getProduct(Integer pid);
    5. }

    6 创建ProductServiceImpl实现类

    1. package com.lqh.server.impl;
    2. import com.lqh.dao.ProductDao;
    3. import com.lqh.entity.Product;
    4. import com.lqh.server.ProductServer;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.stereotype.Service;
    7. /**
    8. * @author: Li Qinghua
    9. * @create: 2022-08-18 13:30
    10. **/
    11. @Service
    12. public class ProductServerImpl implements ProductServer {
    13. @Autowired
    14. private ProductDao productDao;
    15. @Override
    16. public Product getProduct(Integer pid) {
    17. Product product = productDao.selectById(pid);
    18. return product;
    19. }
    20. }

    7 创建ProductController类

    1. package com.lqh.controller;
    2. import com.lqh.entity.Product;
    3. import com.lqh.server.ProductServer;
    4. import com.lqh.utils.CommonResult;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.web.bind.annotation.GetMapping;
    7. import org.springframework.web.bind.annotation.PathVariable;
    8. import org.springframework.web.bind.annotation.RequestMapping;
    9. import org.springframework.web.bind.annotation.RestController;
    10. /**
    11. * @author: Li Qinghua
    12. * @create: 2022-08-18 13:29
    13. **/
    14. @RestController
    15. @RequestMapping("/product")
    16. public class ProductController {
    17. @Autowired
    18. private ProductServer productServer;
    19. @GetMapping("/search/{pid}")
    20. public Product searchProduct(@PathVariable Integer pid){
    21. Product product = productServer.getProduct(pid);
    22. if(product!=null){
    23. return product;
    24. }else return null;
    25. }
    26. }

    8 启动工程,等到数据库表创建完毕之后,加入测试数据

     9.通过浏览器访问服务

    1.3创建订单微服务 springcloud-order

    1 创建一个名为 springcloud-order 的模块,并添加springboot依赖 pom.xml

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <parent>
    6. <artifactId>day0818-springcloudartifactId>
    7. <groupId>com.lqhgroupId>
    8. <version>0.0.1-SNAPSHOTversion>
    9. parent>
    10. <modelVersion>4.0.0modelVersion>
    11. <artifactId>springcloud-orderartifactId>
    12. <properties>
    13. <maven.compiler.source>8maven.compiler.source>
    14. <maven.compiler.target>8maven.compiler.target>
    15. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    16. properties>
    17. <dependencies>
    18. <dependency>
    19. <groupId>org.springframework.bootgroupId>
    20. <artifactId>spring-boot-starter-webartifactId>
    21. dependency>
    22. <dependency>
    23. <groupId>com.alibabagroupId>
    24. <artifactId>fastjsonartifactId>
    25. <version>2.0.5version>
    26. dependency>
    27. <dependency>
    28. <groupId>com.lqhgroupId>
    29. <artifactId>springcloud-commonartifactId>
    30. <version>0.0.1-SNAPSHOTversion>
    31. <scope>compilescope>
    32. dependency>
    33. <dependency>
    34. <groupId>com.alibaba.cloudgroupId>
    35. <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    36. dependency>
    37. <dependency>
    38. <groupId>org.springframework.cloudgroupId>
    39. <artifactId>spring-cloud-starter-openfeignartifactId>
    40. dependency>
    41. dependencies>
    42. project>

    2 创建启动类

    1. package com.lqh;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    5. import org.springframework.cloud.openfeign.EnableFeignClients;
    6. import org.springframework.context.annotation.Bean;
    7. import org.springframework.web.client.RestTemplate;
    8. /**
    9. * @author: Li Qinghua
    10. * @create: 2022-08-18 14:02
    11. **/
    12. @SpringBootApplication
    13. @EnableFeignClients
    14. public class OrderApp {
    15. public static void main(String[] args) {
    16. SpringApplication.run(OrderApp.class,args);
    17. }
    18. }

    3 创建配置文件application.yml

    1. server:
    2. port: 8092
    3. spring:
    4. datasource:
    5. url: jdbc:mysql://localhost:3306/springcloud?serverTimezone=Asia/Shanghai
    6. driver-class-name: com.mysql.cj.jdbc.Driver
    7. username: root
    8. password: 123456
    9. cloud:
    10. nacos:
    11. discovery:
    12. server-addr: localhost:8848
    13. enabled: true
    14. application:
    15. name: order
    16. mybatis-plus:
    17. configuration:
    18. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

    4 创建OrderDao接口

    1. package com.lqh.dao;
    2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    3. import com.lqh.entity.Order;
    4. import org.apache.ibatis.annotations.Mapper;
    5. @Mapper
    6. public interface OrderDao extends BaseMapper {
    7. }

    5 创建OrderService接口

    1. package com.lqh.server;
    2. import com.lqh.entity.Order;
    3. /**
    4. * @author: Li Qinghua
    5. * @create: 2022-08-18 14:11
    6. **/
    7. public interface OrderServer {
    8. Order saveProductOrder(Integer pid, Integer num);
    9. }

    6 创建OrderServiceImpl实现类

    1. package com.lqh.server.impl;
    2. import com.lqh.dao.OrderDao;
    3. import com.lqh.entity.Order;
    4. import com.lqh.entity.Product;
    5. import com.lqh.server.OrderServer;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.stereotype.Service;
    8. import org.springframework.web.client.RestTemplate;
    9. /**
    10. * @author: Li Qinghua
    11. * @create: 2022-08-18 14:11
    12. **/
    13. @Service
    14. public class OrderServerImpl implements OrderServer {
    15. @Autowired
    16. private OrderDao orderDao;
    17. @Autowired
    18. private RestTemplate restTemplate;
    19. @Override
    20. public Order saveProductOrder(Integer pid, Integer num) {
    21. Order order=new Order();
    22. order.setUid(1);
    23. order.setUsername("zhangsan");
    24. Product product = restTemplate.getForObject("http://localhost:8081/product/search/" + pid, Product.class);
    25. order.setPid(product.getPid());
    26. System.out.println(product);
    27. order.setPname(product.getPname());
    28. order.setPprice(product.getPprice());
    29. order.setNumber(num);
    30. orderDao.insert(order);
    31. return order;
    32. }
    33. }

    7 创建OrderController类

    1. package com.lqh.controller;
    2. import com.lqh.entity.Order;
    3. import com.lqh.server.OrderServer;
    4. import com.lqh.utils.CommonResult;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.beans.factory.annotation.Value;
    7. import org.springframework.cloud.context.config.annotation.RefreshScope;
    8. import org.springframework.web.bind.annotation.GetMapping;
    9. import org.springframework.web.bind.annotation.PathVariable;
    10. import org.springframework.web.bind.annotation.RequestMapping;
    11. import org.springframework.web.bind.annotation.RestController;
    12. /**
    13. * @author: Li Qinghua
    14. * @create: 2022-08-18 14:10
    15. **/
    16. @RestController
    17. @RequestMapping("/order")
    18. @RefreshScope
    19. public class OrderController {
    20. @Autowired
    21. private OrderServer orderServer;
    22. @GetMapping("/save/{pid}/{num}")
    23. public CommonResult saveOrder(@PathVariable Integer pid,@PathVariable Integer num){
    24. Order order = orderServer.saveProductOrder(pid, num);
    25. if(order!=null){
    26. return new CommonResult(200,"订单保存成功",order);
    27. }
    28. return new CommonResult(500,"订单保存失败",null);
    29. }
    30. }

    8 启动工程并在浏览器上测试

  • 相关阅读:
    Java中三种I/O模型 BIO,NIO,AIO
    第十节:继承【java】
    硬件工程师必备的35个资料网站
    飞书API 2-2:如何使用 API 建多维表
    Redis高可用之战:主从架构
    夏季预防声带息肉的方法有哪些?
    自定义windows右键菜单,软件卸载后 右键菜单残留 打开方式残留 解决方法
    技能学习链接
    [AndroidStudio]_[初级]_[修改虚拟设备镜像文件的存放位置]
    分布式医疗大数据存储方案研究综述
  • 原文地址:https://blog.csdn.net/m0_58212960/article/details/126450573