• 教你快速搭建微服务环境


    目录

    一、项目搭建

    1.父项目搭建

    2.子项目的搭建

     二、项目展示


    一、项目搭建

    1.父项目搭建

    步骤:

            1 选择maven项目作为父项目,修改为1.8之后直接点击next

            2  定义父项目的名称,修改Artifct Coordinates ---> GroupId 然后点击finsh就可以啦

            3 修改pom.xml

    1 选择maven项目作为父项目,修改为1.8之后直接点击next

     2  定义父项目的名称,修改Artifct Coordinates ---> GroupId 然后点击finsh就可以啦 

    3 修改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. <modelVersion>4.0.0modelVersion>
    6. <groupId>com.zkinggroupId>
    7. <artifactId>spcloud-shopartifactId>
    8. <version>1.0-SNAPSHOTversion>
    9. <modules>
    10. <module>shop-commonmodule>
    11. modules>
    12. <packaging>pompackaging>
    13. <properties>
    14. <java.version>1.8java.version>
    15. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    16. <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
    17. <spring-boot.version>2.3.2.RELEASEspring-boot.version>
    18. <spring-cloud.version>Hoxton.SR9spring-cloud.version>
    19. <spring-cloud-alibaba.version>2.2.6.RELEASEspring-cloud-alibaba.version>
    20. properties>
    21. <dependencyManagement>
    22. <dependencies>
    23. <dependency>
    24. <groupId>org.springframework.bootgroupId>
    25. <artifactId>spring-boot-dependenciesartifactId>
    26. <version>${spring-boot.version}version>
    27. <type>pomtype>
    28. <scope>importscope>
    29. dependency>
    30. dependencies>
    31. dependencyManagement>
    32. project>

    2.子项目的搭建

    搭建一个shop-common的maven子项目

    当建完这个项目时 父项目的pom.xml就会自动多出几行代码 ,这表示

    关联成功 

     

    shop-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>spcloud-shopartifactId>
    7. <groupId>com.zkinggroupId>
    8. <version>1.0-SNAPSHOTversion>
    9. parent>
    10. <modelVersion>4.0.0modelVersion>
    11. <artifactId>shop-commonartifactId>
    12. <dependencies>
    13. <dependency>
    14. <groupId>org.projectlombokgroupId>
    15. <artifactId>lombokartifactId>
    16. dependency>
    17. <dependency>
    18. <groupId>com.alibabagroupId>
    19. <artifactId>fastjsonartifactId>
    20. <version>1.2.56version>
    21. dependency>
    22. <dependency>
    23. <groupId>mysqlgroupId>
    24. <artifactId>mysql-connector-javaartifactId>
    25. <version>5.1.44version>
    26. dependency>
    27. dependencies>
    28. project>

    创建springboot项目: 

     

    直接选择next,然后finsh就可以了 

     以同样的方式创建一下三个

     pom.xml

    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. <parent>
    5. <artifactId>spcloud-shopartifactId>
    6. <groupId>com.zkinggroupId>
    7. <version>1.0-SNAPSHOTversion>
    8. parent>
    9. <modelVersion>4.0.0modelVersion>
    10. <artifactId>shop-orderartifactId>
    11. <dependencies>
    12. <dependency>
    13. <groupId>org.springframework.bootgroupId>
    14. <artifactId>spring-boot-starter-webartifactId>
    15. dependency>
    16. <dependency>
    17. <groupId>com.zkinggroupId>
    18. <artifactId>shop-commonartifactId>
    19. <version>1.0-SNAPSHOTversion>
    20. dependency>
    21. dependencies>
    22. project>

    =====================以上的基本配置完成=========================

     二、项目展示

    写入实体类

     order

    1. package com.zking.model;
    2. import lombok.AllArgsConstructor;
    3. import lombok.Data;
    4. import lombok.NoArgsConstructor;
    5. @Data
    6. @AllArgsConstructor
    7. @NoArgsConstructor
    8. public class Order {
    9. private Long oid;//订单id
    10. //用户
    11. private Integer uid;//用户id
    12. private String username;//用户名
    13. //商品
    14. private Integer pid;//商品id
    15. private String pname;//商品名称
    16. private Double pprice;//商品单价
    17. //数量
    18. private Integer number;//购买数量
    19. }

    Product

    1. package com.zking.model;
    2. import lombok.AllArgsConstructor;
    3. import lombok.Data;
    4. import lombok.NoArgsConstructor;
    5. @Data
    6. @NoArgsConstructor
    7. @AllArgsConstructor
    8. public class Product {
    9. private Integer pid;//主键
    10. private String pname;//商品名称
    11. private Double pprice;//商品价格
    12. private Integer stock;//库存
    13. }

    User

    1. package com.zking.model;
    2. import lombok.AllArgsConstructor;
    3. import lombok.Data;
    4. import lombok.NoArgsConstructor;
    5. //用户
    6. @Data//不再去写set和get方法
    7. @NoArgsConstructor
    8. @AllArgsConstructor
    9. public class User {
    10. private Integer uid;//主键
    11. private String username;//用户名
    12. private String password;//密码
    13. private String telephone;//手机号
    14. }

    编写aplication.yml

     

     

     

     

    编写controller层

    shop-user:

    1. package com.zking.shopuser.controller;
    2. import com.zking.model.User;
    3. import org.springframework.web.bind.annotation.PathVariable;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import org.springframework.web.bind.annotation.RestController;
    6. /**
    7. * @author 小何吖
    8. * @create 2022-11-24 15:47
    9. */
    10. @RestController
    11. @RequestMapping("/user")
    12. public class UserController {
    13. @RequestMapping("/get/{id}")
    14. public User get(@PathVariable("id") Integer id){
    15. return new User(id,"骚强","5438","3838438");
    16. }
    17. }

     运行,选中写了controller的运行

     

     

     shop-product:

    1. package com.zking.shopproduct.controller;
    2. import com.zking.model.Product;
    3. import org.springframework.web.bind.annotation.PathVariable;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import org.springframework.web.bind.annotation.RestController;
    6. /**
    7. * @author 小何吖
    8. * @create 2022-11-24 15:52
    9. */
    10. @RestController
    11. @RequestMapping("/product")
    12. public class ProductContoller {
    13. @RequestMapping("/get/{pid}")
    14. public Product get(@PathVariable("pid") Integer pid){
    15. return new Product(pid,"金瓶梅",66d,20);
    16. }
    17. }

     

     shop-order:

    1. package com.zking.shoporder.controller;
    2. import com.zking.model.Order;
    3. import com.zking.model.Product;
    4. import com.zking.model.User;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.web.bind.annotation.PathVariable;
    7. import org.springframework.web.bind.annotation.RequestMapping;
    8. import org.springframework.web.bind.annotation.RestController;
    9. import org.springframework.web.client.RestTemplate;
    10. /**
    11. * @author 小何吖
    12. * @create 2022-11-24 15:43
    13. */
    14. @RestController
    15. @RequestMapping("/order")
    16. public class OrderController {
    17. @Autowired
    18. private RestTemplate restTemplate;
    19. @RequestMapping("/get/{uid}/{pid}")
    20. public Order get(@PathVariable("uid") Integer uid
    21. ,@PathVariable("pid") Integer pid){
    22. /*
    23. *要在订单微服务调用 用户微服务、商品微服务、也就意味着跨项目调用
    24. *
    25. */
    26. User user = restTemplate.getForObject("http://localhost:8070/user/get/" + uid, User.class);
    27. Product product = restTemplate.getForObject("http://localhost:8080/product/get/" + pid, Product.class);
    28. Order order = new Order();
    29. order.setUsername(user.getUsername());
    30. order.setUid(user.getUid());
    31. order.setPprice(product.getPprice());
    32. order.setPname(product.getPname());
    33. order.setPid(product.getPid());
    34. order.setOid(System.currentTimeMillis());
    35. order.setNumber(product.getStock());
    36. return order;
    37. }
    38. }

     

     

    这就是简单的微服务,没有连接数据库的简单应用

  • 相关阅读:
    猿创征文|〖Python 数据库开发实战 - Python与MySQL交互篇⑰〗- 项目实战 - 实现用户管理 - 修改用户
    快来了解抖音小店的爆款秘籍:选择、营销与服务的关键
    GlobalTransactional
    Linux环境下安装人大金仓数据库
    BLUE引擎变量数据分析
    day19 爬虫和前端
    事 件 流
    【C++】string类的模拟实现
    学习笔记-FRIDA脚本系列(一)
    Java安全-CC1
  • 原文地址:https://blog.csdn.net/hmjcxy/article/details/128049692