SpringCloud2020学习笔记03——微服务架构编码构建

目录

  • 一、IDEA新建project工作控件
    • 1、微服务cloud整体聚合父工程Project
      • 父工程步骤:
    • 2、父工程POM
    • 3、Maven工程落地细节复习
      • ① Maven中的dependencyManagement和dependencies
      • ② Maven中跳过单元测试
  • 二、Rest微服务工程构建
    • 1、构建步骤
      • ① cloud-provider-payment8001微服务提供者支付Module模块
      • ② 热部署Devtools
      • ③ cloud-consumer-order80微服务消费者订单Module模块
      • ④ 工程重构

一、IDEA新建project工作控件

1、微服务cloud整体聚合父工程Project

父工程步骤:

① New Project
SpringCloud2020学习笔记03——微服务架构编码构建_第1张图片
SpringCloud2020学习笔记03——微服务架构编码构建_第2张图片
② 字符编码
SpringCloud2020学习笔记03——微服务架构编码构建_第3张图片
③ 注解激活生效
SpringCloud2020学习笔记03——微服务架构编码构建_第4张图片
④ java编译版本选8
SpringCloud2020学习笔记03——微服务架构编码构建_第5张图片

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">
  <modelVersion>4.0.0modelVersion>
  <groupId>com.radish.springcloudgroupId>
  <artifactId>springcloudartifactId>
  <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>4.12log4j.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>druidartifactId>
        <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>
    dependencies>
  dependencyManagement>
project>

3、Maven工程落地细节复习

① Maven中的dependencyManagement和dependencies

Maven使用dependencyManagement元素提供了一种管理依赖版本号的方式。
通常会在一个组织或者项目的最顶层的父POM中看到dependencyManagement元素。
在父POM指定了版本号,而在子POM中可以不指定版本号,直接引用父POM的版本号。

父POM:

<dependencyManagement>
	<dependencies>
		<dependency>
	        <groupId>mysqlgroupId>
	        <artifactId>mysql-connector-javaartifactId>
	        <version>${mysql.version}version>
	    dependency>
	dependencies>
dependencyManagement>

子POM:

<dependencies>
	<dependency>
	    <groupId>mysqlgroupId>
	    <artifactId>mysql-connector-javaartifactId>
	dependency>
dependencies>

② Maven中跳过单元测试

SpringCloud2020学习笔记03——微服务架构编码构建_第6张图片

二、Rest微服务工程构建

1、构建步骤

① cloud-provider-payment8001微服务提供者支付Module模块

  1. 建module
    SpringCloud2020学习笔记03——微服务架构编码构建_第7张图片
    SpringCloud2020学习笔记03——微服务架构编码构建_第8张图片
  2. 改POM
    <dependencies>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-zipkinartifactId>
        dependency>
        <dependency>
            <groupId>com.eiletxie.springcloudgroupId>
            <artifactId>cloud-api-commonsartifactId>
            <version>${project.version}version>
        dependency>
        <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>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        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.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>
  1. 写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        #mysql驱动包
    url: jdbc:mysql://localhost:3306/db2020?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456

mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.radish.springcloud.entities     #所有Entity别名类所在包
  1. 主启动
    SpringCloud2020学习笔记03——微服务架构编码构建_第9张图片
  2. 业务类

(1)建表

CREATE TABLE payment(
	id bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
	serial varchar(200) DEFAULT '',
	PRIMARY KEY(id)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8

(2)entities

主实体Payment

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
    private Long id;
    private String serial;
}

Json封装体CommonResult

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {
    //404 not_found
    private Integer code;
    private String message;
    private T data;

    public CommonResult(Integer code, String message) {
        this(code, message, null);
    }
}

注意:还需要安装Lombok插件,lombok才可以生效

(3)dao

PaymentDao.java

@Mapper
public interface PaymentDao {

    public int create(Payment payment);

    public Payment getPaymentById(@Param("id") Long id);

    public int delete(@Param("id") Long id);

    public int update(Payment payment);
}

PaymentMapper.xml




<mapper namespace="com.radish.springcloud.dao.PaymentDao">

    
    <insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
        insert into payment(serial) values(#{serial});
    insert>

    <resultMap id="BaseResultMap" type="com.radish.springcloud.entities.Payment">
        <id column="id" property="id" jdbcType="BIGINT" />
        <id column="serial" property="serial" jdbcType="VARCHAR"/>
    resultMap>
    <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
        select * from payment where id=#{id};
    select>
    
mapper>

(4)service

PaymentService.java

public interface PaymentService {

    public int create(Payment payment);

    public Payment getPaymentById(@Param("id") Long id);

    public int delete(@Param("id") Long id);

    public int update(Payment payment);
}

PaymentServiceImpl.java

@Service
public class PaymentServiceImpl implements PaymentService {

    @Resource
    public PaymentDao paymentDao;

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

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

    @Override
    public int delete(Long id) {
        return paymentDao.delete(id);
    }

    @Override
    public int update(Payment payment) {
        return paymentDao.update(payment);
    }
}

(5)controller

@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @PostMapping(value = "/payment/create")
    public CommonResult create(@RequestBody 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);
        }
    }
}

② 热部署Devtools

1.Adding devtools to your project


<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-devtoolsartifactId>
   <scope>runtimescope>
    <optional>trueoptional>
dependency>

2.Adding plugin to your pom.xml

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-maven-pluginartifactId>
      <configuration>
        <fork>truefork>
        <addResources>trueaddResources>
      configuration>
    plugin>
  plugins>
build>

3.Enabling automatic build 四个打钩
SpringCloud2020学习笔记03——微服务架构编码构建_第10张图片

4.Update the value of
SpringCloud2020学习笔记03——微服务架构编码构建_第11张图片
SpringCloud2020学习笔记03——微服务架构编码构建_第12张图片
5.重启IDEA

③ cloud-consumer-order80微服务消费者订单Module模块

同上创建cloud-consumer-order80模块
改POM

<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.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>

        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

写YML

server:
  port: 80

主启动

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

业务类
1. 创建entities(将cloud-provider-payment8001工程下的entities包下的两个实体类复制过来)
2. RestTemplate

RestTemplate提供了多种便捷访问远程Http服务的方法,是一种简单便捷的访问restful服务模板类,是spring提供的用于访问Rest服务的客户端模板工具集
SpringCloud2020学习笔记03——微服务架构编码构建_第13张图片

3.config配置类

ApplicationContextConfig.java

@Configuration
public class ApplicationContextConfig {

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

4.创建controller

OrderController.java

@RestController
@Slf4j
public class OrderController {

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

    @Resource
    private RestTemplate restTemplate;

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

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

测试
SpringCloud2020学习笔记03——微服务架构编码构建_第14张图片

④ 工程重构

1.新建cloud-api-commons
2.pom

<dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
           <optional>trueoptional>
        dependency>

        
        <dependency>
            <groupId>cn.hutoolgroupId>
            <artifactId>hutool-allartifactId>
            <version>5.1.0version>
        dependency>
    dependencies>

3.entities
复制之前的两个实体类

4.maven命令clean install

5.订单80和支付8001分别改造
(1)删除各自的原先有过的entities文件夹
(2)各自黏贴POM内容


        <dependency>
            <groupId>com.radish.springcloudgroupId>
            <artifactId>cloud-api-commonsartifactId>
            <version>${project.version}version>
        dependency>

SpringCloud2020学习笔记03——微服务架构编码构建_第15张图片

你可能感兴趣的:(SpringCloud,学习,学习笔记)