SpringCloud01_微服务架构入门(概述、技术栈、版本选型、环境搭建)

1、微服务架构概述

  • 将单元应用划分为一组小的服务,各服务独立部署,独立运行,服务之间相互调用,互相配合完成。
  • SpringCloud = 分布式微服务架构的一站式解决方案,多种微服务架构落地技术的几何体。

2、技术栈更新

SpringCloud01_微服务架构入门(概述、技术栈、版本选型、环境搭建)_第1张图片

3、版本选型

每个版本都有对应的SpringBoot版本,版本选型官网链接:版本选型官网链接

4、环境搭建

  • 父工程构建

第一步:新建工程
SpringCloud01_微服务架构入门(概述、技术栈、版本选型、环境搭建)_第2张图片SpringCloud01_微服务架构入门(概述、技术栈、版本选型、环境搭建)_第3张图片
SpringCloud01_微服务架构入门(概述、技术栈、版本选型、环境搭建)_第4张图片

第二步:字符编码
SpringCloud01_微服务架构入门(概述、技术栈、版本选型、环境搭建)_第5张图片
第三步:注解激活
SpringCloud01_微服务架构入门(概述、技术栈、版本选型、环境搭建)_第6张图片
第四步:编译版本
SpringCloud01_微服务架构入门(概述、技术栈、版本选型、环境搭建)_第7张图片
第五步:文件过滤
SpringCloud01_微服务架构入门(概述、技术栈、版本选型、环境搭建)_第8张图片

  • 父工程pom.xml文件整理

第一步:文件管理
SpringCloud01_微服务架构入门(概述、技术栈、版本选型、环境搭建)_第9张图片
第二步:内容修改



<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>org.examplegroupId>
  <artifactId>com.cloud2022artifactId>
  <version>1.0-SNAPSHOTversion>
  <packaging>pom packaging>

  
  <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-plus.spring.boot.version>3.4.3mybatis-plus.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>com.baomidougroupId>
          <artifactId>mybatis-plus-boot-starterartifactId>
          <version>${mybatis-plus.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>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-clean-pluginartifactId>
          <version>3.1.0version>
        plugin>
        <plugin>
          <artifactId>maven-site-pluginartifactId>
          <version>3.7.1version>
        plugin>
        <plugin>
          <artifactId>maven-project-info-reports-pluginartifactId>
          <version>3.0.0version>
        plugin>
      plugins>
    pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-site-pluginartifactId>
        <configuration>
          <locales>en,frlocales>
        configuration>
      plugin>
    plugins>
  build>

project>

dependencyManagement与dependencies区别

  • dependencyManagement用作总pom文件中,便于统一管理版本;
  • dependencyManagement只是声明依赖,并不引入,子项目需重新声明依赖;

其他技巧:

  • 跳过测试
    SpringCloud01_微服务架构入门(概述、技术栈、版本选型、环境搭建)_第10张图片

  • install发布
    SpringCloud01_微服务架构入门(概述、技术栈、版本选型、环境搭建)_第11张图片

  • 子模块搭建-支付模块

第一步:建module
SpringCloud01_微服务架构入门(概述、技术栈、版本选型、环境搭建)_第12张图片
SpringCloud01_微服务架构入门(概述、技术栈、版本选型、环境搭建)_第13张图片

第二步:改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>cloud2022artifactId>
        <groupId>org.examplegroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>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>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.1.10version>
        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>

project>

第三步:写yml
application.yml

server:
  port: 8001

spring:
  application:
    name : payment8001

  datasource:
    url: jdbc:mysql://localhost:3306/springboot-mybatisplus?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
    username: root
    password: 123456
    driver-class-name=com: mysql.cj.jdbc.Driver
#    type: com.alibaba.druid.pool.DruidDataSource

#2、mybatis-plus配置
mybatis-plus:
  #2-1 lodging 日志
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  #2-2 deleted 逻辑删除
  global-config :
    db-config:
      logic-delete-value: 1
      logic-not-delete-value: 0
  mapper-locations: classpath:mapper/*.xml

第四步:主启动
PaymentApplication .java

package com.payment;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = "com.payment")
@MapperScan("com.payment.Mapper")
public class PaymentApplication {

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

第五步:业务类
SpringCloud01_微服务架构入门(概述、技术栈、版本选型、环境搭建)_第14张图片执行:http://localhost:8001/payment/1
结果:{“code”:200,“msg”:“成功”,“date”:null}

注意:
——使用result封装数据结果集;

  • 子模块搭建-订单模块
    搭建步骤与支付模块相同

注意:
——调用支付模块,不需要业务处理逻辑;
SpringCloud01_微服务架构入门(概述、技术栈、版本选型、环境搭建)_第15张图片

——需要config配置RestTemplate;

package com.consumer.Config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

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

——controller类
注意:

  • 请求方式为get;
  • post无法使用RequestBody注解;
package com.consumer.Controller;

import com.consumer.Entity.Payment;
import com.consumer.Entity.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/consumer")
@Slf4j
@RequiredArgsConstructor
public class PaymentController {

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

    private final RestTemplate restTemplate  ;

    @GetMapping("/get/save")
    public Result<Payment> saveEntity(Payment payment) {
        return restTemplate.postForObject(payment_URL+"/payment/save",payment,Result.class);
    }

    @GetMapping("/{id}")
    public Result getEntity(@PathVariable("id") Integer id) {
        return restTemplate.getForObject(payment_URL+"/payment/"+id,Result.class);
    }
}

总结

1、子工程-支付模块

  • pom.xml

<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>cloud2022artifactId>
        <groupId>org.examplegroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>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>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.1.10version>
        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>

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

    dependencies>

project>
  • yml
server:
  port: 8001

spring:
  application:
    name : payment8001

  datasource:
    url: jdbc:mysql://localhost:3306/springboot-mybatisplus?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
    username: root
    password: 123456
    driver-class-name=com: mysql.cj.jdbc.Driver
#    type: com.alibaba.druid.pool.DruidDataSource

#2、mybatis-plus配置
mybatis-plus:
  #2-1 lodging 日志
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  #2-2 deleted 逻辑删除
  global-config :
    db-config:
      logic-delete-value: 1
      logic-not-delete-value: 0
  mapper-locations: classpath:mapper/*.xml
  • controller
package com.payment.Controller;

import com.payment.Entity.Payment;
import com.payment.Entity.Result;
import com.payment.Service.PaymentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/payment")
@Slf4j
@RequiredArgsConstructor
public class PaymentController {

    private final PaymentService paymentService  ;

    @PostMapping("/save")
    public Result saveEntity(@RequestBody Payment payment) {
        paymentService.saveEntity(payment);
        return new Result(200,"成功","");
    }

    @GetMapping("/{id}")
    public Result saveEntity(@PathVariable("id") Integer id) {
        System.out.println(id);
        return new Result(200,"成功",paymentService.getPayment(id));
    }

}

2、子工程-订单模块

  • pom.xml

<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>cloud2022artifactId>
        <groupId>org.examplegroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>consumerartifactId>

    <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>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.1.10version>
        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>

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

    dependencies>

project>
  • yml
server:
  port: 8002

spring:
  application:
    name : consumer

  datasource:
    url: jdbc:mysql://localhost:3306/springboot-mybatisplus?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
    username: root
    password: 123456
    driver-class-name=com: mysql.cj.jdbc.Driver
#    type: com.alibaba.druid.pool.DruidDataSource

#2、mybatis-plus配置
mybatis-plus:
  #2-1 lodging 日志
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  #2-2 deleted 逻辑删除
  global-config :
    db-config:
      logic-delete-value: 1
      logic-not-delete-value: 0
#  mapper-locations: classpath:mapper/*.xml
  • controller
package com.consumer.Controller;

import com.consumer.Entity.Payment;
import com.consumer.Entity.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/consumer")
@Slf4j
@RequiredArgsConstructor
public class PaymentController {

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

    private final RestTemplate restTemplate  ;

    @GetMapping("get/save")
    public Result<Payment> saveEntity(Payment payment) {
        return restTemplate.postForObject(payment_URL+"/payment/save",payment,Result.class);
    }

    @GetMapping("/{id}")
    public Result getEntity(@PathVariable("id") Integer id) {
        return restTemplate.getForObject(payment_URL+"/payment/save/"+id,Result.class);
    }
}

  • Result
package com.consumer.Entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<R>{

    private Integer code;

    private String msg;

    private R Date;

}

  • 代码重构
    (1)提出公共部分
    (2)install发布
    SpringCloud01_微服务架构入门(概述、技术栈、版本选型、环境搭建)_第16张图片
    (3)在其他服务中引入
      <dependency>
            <groupId>com.commonsgroupId>
            <artifactId>commonsartifactId>
            <version>1.0-SNAPSHOTversion>
            <scope>compilescope>
        dependency>

采坑1:发布的服务一定要有gruopId字段,与引入的groupId一致

    <parent>
        <artifactId>cloud2022</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.commons</groupId>
    <artifactId>commons</artifactId>

你可能感兴趣的:(5-Java开发框架,#,5-6,SpringCloud,微服务,架构,spring,cloud)