SpringCloud -1- 项目搭建和技术选型


title: SpringCloud 项目构建 and 技术选型
author: mykk
top: false
cover: false
toc: true
mathjax: false
summary: SpringCloud项目搭建
tags:

  • springcloud
  • 搭建
    categories:
  • 分布式技术栈
    abbrlink: 9df20094
    reprintPolicy: cc_by
    date: 2022-01-12 23:42:15
    coverImg:
    img:
    password:

1、版本

[图片上传失败...(image-3362aa-1648466768288)]

2、官网地址【Spring Cloud】

1、英文

https://cloud.spring.io/spring-cloud-static/Hoxton.SR1/reference/htmlsingle/

2、中文

https://www.bookstack.cn/read/spring-cloud-docs/docs-index.md

3、springboot

https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/reference/htmlsingle/

3、项目搭建-父工程构建

  • 父工程坐标



    4.0.0
    com.kk
    springcloud2021to2022
    1.0-SNAPSHOT
    pom


    
    
        UTF-8
        1.8
        1.8
        4.12
        1.2.17
        1.16.18
        5.1.47
        1.1.16
        1.3.0
    

    
    
        
            
            
                org.springframework.boot
                spring-boot-dependencies
                2.2.2.RELEASE
                pom
                import
            
            
            
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.SR1
                pom
                import
            
            
            
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                2.1.0.RELEASE
                pom
                import
            

            
                mysql
                mysql-connector-java
                ${mysql.version}
            
            
                com.alibaba
                druid
                ${druid.version}
            
            
                org.mybatis.spring.boot
                mybatis-spring-boot-starter
                ${mybatis.spring.boot.version}
            
            
                junit
                junit
                ${junit.version}
            
            
                log4j
                log4j
                ${log4j.version}
            
            
                org.projectlombok
                lombok
                ${lombok.version}
                true
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    true
                    true
                
            
        
    


4、项目搭建-Rest微服务工程构建

1、坐标



    
        springcloud2021to2022
        com.kk
        1.0-SNAPSHOT
    
    4.0.0
    cloud-provider-payment8001
    
    
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
        

        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.10
        
        
        
            mysql
            mysql-connector-java
        

        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        

        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        

        
        
            org.projectlombok
            lombok
            true
        

        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    


2、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/spring_cloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: a1b2c3

mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: com.kk.springclond.entities



3、主启动

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

4、业务类

1、建表

CREATE TABLE `payment` (
`id`  bigint NOT NULL AUTO_INCREMENT ,
`serial`  varchar(255) NULL ,
PRIMARY KEY (`id`)
)

2、实体(entity)

(1)通用返回结果实体
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult {
    private Integer code;
    private String message;
    private T data;

    public CommonResult(Integer code, String message) {
        this(code,message,null);
    }
}
(2)Payment
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment {
    private Long id;
    private String serial;
}

3、dao层

1、接口PaymentDao编写
package com.kk.springcloud.dao;
import com.kk.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface PaymentDao {
    public int create(Payment payment); //写

    public Payment getPaymentById(@Param("id") Long id);  //读取
}

2、mybatis的映射文件PaymentMapper.xml
(1)路径

src\main\resources\mapper\PaymentMapper.xml

(2)头文件




    
            insert into payment(serial) values(${serial});

    

    
        
        
    
    



4、service

1、接口
package com.kk.springcloud.service;
import com.kk.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Param;

public interface PaymentService {
    public int create(Payment payment); //写

    public Payment getPaymentById(@Param("id") Long id);  //读取
}

2、实现类
package com.kk.springcloud.service.impl;
import com.kk.springcloud.dao.PaymentDao;
import com.kk.springcloud.entities.Payment;
import com.kk.springcloud.service.PaymentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

@Service
public class PaymentServiceImpl implements PaymentService {
    @Resource
    private PaymentDao paymentDao;

    public int create(Payment payment) {
        return paymentDao.create (payment);
    }

    public Payment getPaymentById(Long id) {
        return paymentDao.getPaymentById (id);
    }
}


5、controller

package com.kk.springcloud.controller;
import com.kk.springcloud.entities.CommonResult;
import com.kk.springcloud.entities.Payment;
import com.kk.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @PostMapping(value = "/payment/create")
    public CommonResult create(Payment payment) {
        int result = paymentService.create (payment);
        log.info ("*****插入结果:" + result);
        if (result > 0) {  //成功
            return new CommonResult (200, "插入数据库成功", result);
        } else {
            return new CommonResult (444, "插入数据库失败", null);
        }
    }

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id) {
        Payment payment = paymentService.getPaymentById (id);
        log.info ("*****查询结果:" + payment);
        if (payment != null) {  //说明有数据,能查询成功
            return new CommonResult (200, "查询成功", payment);
        } else {
            return new CommonResult (444, "没有对应记录,查询ID:" + id, null);
        }
    }
}

5、测试

1、插入:使用post请求才能被插入,所以在url上无效,可以使用postman

http://localhost:8001/payment/create?serial=mykk02

[图片上传失败...(image-d36e2-1648466768288)]

2、查询:get请求,url,postman皆可

http://localhost:8001/payment/get/1

[图片上传失败...(image-bdd95d-1648466768288)]

5、热部署

1、子工程 pom

上面的依赖已经有加了,是这个

[图片上传失败...(image-2154f1-1648466768288)]

2、父工程 pom插件

上面的依赖已经有加了,是这个

[图片上传失败...(image-8cc33a-1648466768288)]

3、设置自动编译

[图片上传失败...(image-b4d78e-1648466768288)]

4、开启自动更新

1、打开设置面板

ctrl + shift + alt + /同时按住,点击第一个Registry...

[图片上传失败...(image-a7ef5f-1648466768288)]

2、两个打勾

[图片上传失败...(image-c930f9-1648466768288)]

[图片上传失败...(image-d1fded-1648466768288)]

5、重启 IDEA

6、项目搭建-Order订单微服务构建

1、坐标



    
        springcloud2021to2022
        com.kk
        1.0-SNAPSHOT
    
    4.0.0

    cloud-consumer-order80
    
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        

        
        
            org.projectlombok
            lombok
            true
        

        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    



2、yml

server:
  port: 80

3、主启动

package com.kk.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

4、业务类

1、创建entities

  • 将cloud-provider-payment8001工程下的entities包下的两个实体类复制过来

2、RestTemplate

1、官网:
  • https://docs.spring.io/spring-framework/docs/5.2.2.RELEASE/javadoc-api/org/springframework/web/client/RestTemplate.html
2、是什么

[图片上传失败...(image-45e0de-1648466768289)]

3、怎么用

[图片上传失败...(image-ac4d2e-1648466768289)]

3、配置类

ApplicationContextConfig

package com.kk.springcloud.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

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

4、创建 controller

package com.kk.springcloud.controller;
import com.kk.springcloud.entities.CommonResult;
import com.kk.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;

@RestController
@Slf4j
public class OrderController {

    public static final String PAYMENT_URL = "http://localhost:8001";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/create")
    public CommonResult create(Payment payment) {
        return restTemplate.postForObject (PAYMENT_URL + "/payment/create", payment, CommonResult.class);  //写操作
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult getPayment(@PathVariable("id") Long id) {
        return restTemplate.getForObject (PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
    }
}

5、测试

1、先启动cloud-provider-payment8001
2、再启动cloud-consumer-order80
3、测试消费者接口

http://localhost/consumer/payment/get/1

注意点:被调用的生产者接口传参记得加注解

[图片上传失败...(image-34bf7d-1648466768289)]

7、项目重构

1、观察问题

1、系统中有重复部分,重构

[图片上传失败...(image-5b9331-1648466768289)]

2、新建公共模块【cloud-api-commons】

1、pom



    
        springcloud2021to2022
        com.kk
        1.0-SNAPSHOT
    
    4.0.0
    cloud-api-commons
    
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        

        
            org.projectlombok
            lombok
            true
        

        
        
            cn.hutool
            hutool-all
            5.1.0
        
    



2、实体

将订单模块和支付模块公共实体放到这里

[图片上传失败...(image-f43ce5-1648466768289)]

3、改造订单和支付

1、删除各自的原先有过的entities文件夹

2、各自分别引入公共模块

        
        
            com.kk
            cloud-api-commons
            ${project.version}
        

[图片上传失败...(image-4d59b9-1648466768289)]

7、项目模块结构图

[图片上传失败...(image-45b887-1648466768289)]

你可能感兴趣的:(SpringCloud -1- 项目搭建和技术选型)