【SpringCloud学习笔记】实战练习

/ 微服务 / 没有评论 / 1390浏览

项目搭建实战

创建父项目

  1. 创建一个maven quickstart项目;
  2. 删除src目录并修改pom.xml文件 pom
  3. 配置pom中统一版本及dependencyManagement等信息。
<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>info.huzd.springcloud</groupId>
  <artifactId>springcloud</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>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Hoxton.SR10</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.2.2.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <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>
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
      </dependency>
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>${druid.version}</version>
      </dependency>
      <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>${mybatis.spring.boot.version}</version>
      </dependency>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
      </dependency>
      <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
      </dependency>
      <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${lombok.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <fork>true</fork>
          <addResources>true</addResources>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

创建支付模块

  1. 创建cloud-provider-playment-8001 支付模块

  2. 修改pom文件

        <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.22</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>
                <scope>runtime</scope>
                <optional>true</optional>
            </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>
            </dependency>
        </dependencies>
    
  3. 编写yml文件

    server:
      port: 8001
    
    spring:
      application:
        name: cloud-provider-payment-8001
      datasource:
        url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
        type: com.alibaba.druid.pool.DruidDataSource
        username: root
        password: 123456
    mybatis:
      mapper-locations: classpath:mapper/*.xml
      type-aliases-package: info.huzd.cloud.bean
    
  4. 编写dao、service、mapper文件

    @Mapper
    public interface PaymentDao {
        public int create(Payment payment);
        public Payment getById(@Param("id") Integer id);
    }
    
    
    @Service
    public class PayServiceImpl implements PaymentService {
    
        @Resource
        PaymentDao paymentDao;
    
        @Override
        public int create(Payment payment) {
            return paymentDao.create(payment);
        }
    
        @Override
        public Payment getById(Integer id) {
            return paymentDao.getById(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="info.huzd.cloud.dao.PaymentDao">
    
        <resultMap id="BaseResultMap" type="info.huzd.cloud.bean.Payment">
            <id column="id" property="id" jdbcType="BIGINT"/>
            <result column="serial" property="serial" jdbcType="VARCHAR"/>
        </resultMap>
    
        <insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
            insert into payment(serial) values (#{serial});
        </insert>
    
        <select id="getById" parameterType="Long" resultMap="BaseResultMap">
            select * from payment where id = #{id};
        </select>
    </mapper>
    
  5. 启动程序使用postman测试

创建消费模块

  1. 创建一个maven项目

    创建支付调用模块,cloud-consumer-order-80

  2. 修改项目pom文件

    添加maven依赖

        <dependencies>
            <dependency>
                <groupId>info.huzd.springcloud</groupId>
                <artifactId>cloud-api-common</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
            <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.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </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>
            </dependency>
        </dependencies>
    
    
  3. 创建application.yml文件

    server:
      port: 80
    
  4. 编写业务代码

    • bean

      @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 object;
      
          public CommonResult(Integer code, String message) {
              this.code = code;
              this.message = message;
          }
      }
      
      
    • config

      @Configuration
      public class ApplicationConfig {
      
          @Bean
          public RestTemplate restTemplate(){
              return new RestTemplate();
          }
      }
      
    • controller

      @Controller
      @RequestMapping("/consumer")
      @Slf4j
      public class OrderController {
      
          public static final String PAYMENT_URL = "http://localhost:8001/payment/";
      
          @Resource
          RestTemplate restTemplate;
      
          @GetMapping("/payment/save")
          @ResponseBody
          public CommonResult<Payment> createPayment(Payment payment){
              return restTemplate.postForObject(PAYMENT_URL+"save",payment,CommonResult.class);
          }
      
          @GetMapping("/payment/get/{id}")
          @ResponseBody
          public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
              return restTemplate.getForObject(PAYMENT_URL+"get/"+id,CommonResult.class);
          }
      
      }
      
  5. 启动postman测试

代码重构

将两个项目中的bean抽离出来放到一个新的maven项目(cloud-api-common)中;具体步骤简单所以省略。