• SpringCloud-父工程及子模块搭建(未集成微服务组件)


    一、父工程搭建

    (1)一路next在这里插入图片描述
    (2)一路next在这里插入图片描述
    (3)一路next在这里插入图片描述
    (4)字符编码集配置
    在这里插入图片描述
    (5)注解生效激活配置
    在这里插入图片描述
    (6)java编译版本选择8
    在这里插入图片描述
    (7)删除父工程的src文件夹
    在这里插入图片描述
    (8)编写pom文件

    
    
    <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.0modelVersion>
    
      <groupId>com.wsh.springcloudgroupId>
      <artifactId>demo20220821artifactId>
      <version>1.0-SNAPSHOTversion>
      <packaging>pompackaging>
    
      
      <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
        <junit.version>4.12junit.version>
        <log4j.version>1.2.17log4j.version>
        <lombok.version>1.16.18lombok.version>
        <mysql.version>5.1.47mysql.version>
        <druid.version>1.1.16druid.version>
        <mybatis.spring.boot.version>1.3.0mybatis.spring.boot.version>
      properties>
    
      
      <dependencyManagement>
        <dependencies>
          
          <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-dependenciesartifactId>
            <version>2.2.2.RELEASEversion>
            <type>pomtype>
            <scope>importscope>
          dependency>
          
          <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-dependenciesartifactId>
            <version>Hoxton.SR1version>
            <type>pomtype>
            <scope>importscope>
          dependency>
          
          <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-alibaba-dependenciesartifactId>
            <version>2.1.0.RELEASEversion>
            <type>pomtype>
            <scope>importscope>
          dependency>
          <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>${mysql.version}version>
          dependency>
          <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>${druid.version}version>
          dependency>
          <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>${mybatis.spring.boot.version}version>
          dependency>
          <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>${junit.version}version>
          dependency>
          <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>${log4j.version}version>
          dependency>
          <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>${lombok.version}version>
            <optional>trueoptional>
          dependency>
        dependencies>
      dependencyManagement>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-maven-pluginartifactId>
            <configuration>
              <fork>truefork>
              <addResources>trueaddResources>
            configuration>
          plugin>
        plugins>
      build>
    
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100

    二、子模块搭建(支付模块微服务,作为微服务提供者)

    微服务模块搭建步骤
    (1)创建模块
    (2)编写pom文件
    (3)编写yml文件
    (4)编写启动类
    (5)依次编写dao、service、controller

    (1)在父工程新建子模块
    在这里插入图片描述
    (2)在这里插入图片描述
    (3)创建完子模块后,查看父工程的pom文件,发现多出子模块信息
    在这里插入图片描述
    (4)编写pom文件

    
    <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>demo20220821artifactId>
            <groupId>com.wsh.springcloudgroupId>
            <version>1.0-SNAPSHOTversion>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>cloud-provider-payment8001artifactId>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-actuatorartifactId>
            dependency>
            <dependency>
                <groupId>org.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
            dependency>
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druid-spring-boot-starterartifactId>
            dependency>
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-jdbcartifactId>
            dependency>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
        dependencies>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    (5)编写application.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: wsh666
    
    mybatis:
      mapperLocations: classpath:mapper/*.xml
      type-aliases-package: com.wsh.springcloud.entity
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    (6)创建启动类
    在这里插入图片描述

    @SpringBootApplication
    public class PaymentMain8001 {
        public static void main(String[] args) {
            SpringApplication.run(PaymentMain8001.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    (7)创建数据库
    在这里插入图片描述
    (8)创建实体类Payment
    在这里插入图片描述

    @Data
    @EqualsAndHashCode
    public class Payment {
        private Long id;
        private String serial;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    (9)创建消息响应体CommonResult
    在这里插入图片描述

    @Data
    @EqualsAndHashCode
    @AllArgsConstructor
    public class CommonResult<T> {
        private Integer code;
        private String msg;
        private T data;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    (10)创建dao接口及对应的mapper文件

    @Mapper
    public interface PaymentDao {
        
        public int insert(Payment payment);
    
        public Payment selectById(@Param("id") Long id);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    
    <mapper namespace="com.wsh.springcloud.dao.PaymentDao">
    
        <insert id="insert" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
            insert into payment(serial) values (#{serial});
        insert>
    
        <resultMap id="PaymentResultMap" type="Payment">
            <id column="id" property="id"/>
            <id column="serial" property="serial"/>
        resultMap>
        <select id="selectById" parameterType="Long" resultMap="PaymentResultMap">
            select * from payment where id=#{id};
        select>
    
    mapper>
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述
    (11)编写service接口及其实现类

    public interface PaymentService {
    
        public int insert(Payment payment);
    
        public Payment selectById(Long id);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    @Service
    public class PaymentServiceImpl implements PaymentService {
    
        @Autowired
        private PaymentDao paymentDao;
    
        @Override
        public int insert(Payment payment){
            return this.paymentDao.insert(payment);
        }
    
        @Override
        public Payment selectById(Long id){
            return this.paymentDao.selectById(id);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述
    (12)编写controller

    @Slf4j
    @RequestMapping("/payment")
    @RestController
    public class PaymentController {
    
        @Autowired
        private PaymentService paymentService;
    
        @RequestMapping(value = "", method = RequestMethod.POST)
        public CommonResult<Integer> insert(Payment payment){
            int i = paymentService.insert(payment);
            log.info(String.valueOf(i));
            return new CommonResult<>(200, "插入完毕", i);
        }
    
        @RequestMapping(value = "/{id}", method = RequestMethod.GET)
        public CommonResult<Payment> selectById(@PathVariable("id") Long id){
            Payment payment = paymentService.selectById(id);
            if (payment != null){
                log.info(payment.toString());
            }
            return new CommonResult<>(200, "查询完毕", payment);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在这里插入图片描述
    (13)测试
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    三、子模块搭建(订单模块微服务,作为微服务消费者)

    (1)在这里插入图片描述
    (2)在这里插入图片描述
    (3)编写pom文件

    
    <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>demo20220821artifactId>
            <groupId>com.wsh.springcloudgroupId>
            <version>1.0-SNAPSHOTversion>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>cloud-consumer-order80artifactId>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-actuatorartifactId>
            dependency>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
        dependencies>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    (4)编写application.yml文件

    server:
      port: 80
    
    • 1
    • 2

    (5)创建启动类
    在这里插入图片描述

    @SpringBootApplication
    public class OrderMain80 {
        public static void main(String[] args) {
            SpringApplication.run(OrderMain80.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    (6)编写配置类,创建RestTemplate实例并将其加入IOC容器中
    在这里插入图片描述

    @Configuration
    public class ApplicationContextConfig {
        
        @Bean
        public RestTemplate restTemplate(){
            return new RestTemplate();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    (7)创建实体类Payment

    @Data
    @EqualsAndHashCode
    public class Payment {
        private Long id;
        private String serial;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    (8)创建消息响应体CommonResult

    @Data
    @EqualsAndHashCode
    @AllArgsConstructor
    public class CommonResult<T> {
        private Integer code;
        private String msg;
        private T data;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    (9)编写controller

    @Slf4j
    @RequestMapping("/order")
    @RestController
    public class OrderController {
    
        @Autowired
        private RestTemplate restTemplate;
    
        private static final String URL_PREFIX = "http://localhost:8001";
    
        @RequestMapping(value = "", method = RequestMethod.POST)
        public CommonResult<Integer> insert(Payment payment){
            return restTemplate.postForObject(URL_PREFIX + "/payment", payment, CommonResult.class);
        }
    
        @RequestMapping(value = "/{id}", method = RequestMethod.GET)
        public CommonResult<Payment> selectById(@PathVariable("id") Long id){
            return restTemplate.getForObject(URL_PREFIX + "/payment/" + id, CommonResult.class);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    (10)测试
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    四、代码重构(抽取相同的代码到一个通用模块中)

    上述两个子模块均使用到实体类Payment及消息响应体CommonResult,故我们可以新建一个子模块并将这两个类转移到该通用模块中。

    在这里插入图片描述

    (1)在这里插入图片描述
    (2)编写pom文件

    
    <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>demo20220821artifactId>
            <groupId>com.wsh.springcloudgroupId>
            <version>1.0-SNAPSHOTversion>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>cloud-api-commonartifactId>
    
        <dependencies>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
            dependency>
            <dependency>
                <groupId>cn.hutoolgroupId>
                <artifactId>hutool-allartifactId>
                <version>5.8.1version>
            dependency>
        dependencies>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    (3)删除两个子模块的Payment及CommonResult类,将其创建在通用模块中
    在这里插入图片描述
    (4)另外两个子模块在pom添加该通用模块依赖
    在这里插入图片描述

  • 相关阅读:
    【Nginx】Linux keepalived+nginx实现主从模式
    【MyBatis】使用手册
    c++:数据结构链表list的模拟实现
    xml总结
    java多线程为什么每个线程都有一个单独的栈 ?
    【ChatGPT散文篇】ChatGPT-清华大学的讲座要点
    docker--redis容器部署及地理空间API的使用示例-II
    如何设计一个C语言面向结构体的内存数据库
    windows机器配置自签名ssl证书,部署文件服务器
    前端小技巧: 拍平数组的6种常见方法
  • 原文地址:https://blog.csdn.net/qq_25243147/article/details/126456984