SpringCloud(1)--入门、版本、环境搭建
MOKE 2020-07-09 AM loading 0条

SpringCloud 入门

版本的选择

查看SpringCloud和SpringBoot之间版本的依赖关系:

  1. https://spring.io/projects/spring-cloud#overview

    image-20200705140141655

  2. https://start.spring.io/actuator/info,返回详细json信息

    image-20200705140430017

部分组件停用

首先连接下各个组件:

  • 服务注册中心:EUREKA

  • 服务负载均衡与调用:NETFLIX OSS RIBBON

  • 服务负载与调用:NETTFLIX

  • 服务熔断降级:HYSTRIX

  • 服务网关:Zuul

  • 服务分布式配置:SpringCloud Config

  • 服务开发:SpingBoot

SpringCloud升级后,部分组件停用:

image-20200704144836363

  1. Eureka 停用,可以使用 Zookeeper 作为服务注册中心
  2. 服务调用,Ribbon 准备停更,代替为 LoadBalance
  3. Feign 改为 OpenFeign
  4. Hystrix 停更,改为 resilence4j 或者阿里巴巴的 sentienl
  5. Zuul 改为 gateway
  6. 服务配置 Config 改为 Nacos
  7. 服务总线 Bus 改为 Nacos

环境搭建

接下来我们会搭建一个订单-支付模块微服务。

父工程创建

image-20200704151034161

父工程依赖

  <groupId>com.moke.springcloud</groupId>
  <artifactId>cloud1</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>8.0.18</mysql.version>
    <druid.verison>1.1.16</druid.verison>
    <mybatis.spring.boot.verison>1.3.0</mybatis.spring.boot.verison>
  </properties>

  <!--子模块继承之后,提供作用:锁定版本+子module不用谢groupId和version-->
  <dependencyManagement>
    <dependencies>
      <!--spring boot 2.2.2-->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.2.2.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!--spring cloud Hoxton.SR1-->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Hoxton.SR1</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!--spring cloud alibaba 2.1.0.RELEASE-->
      <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>2.2.0.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!-- MySql -->
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
      </dependency>
      <!-- Druid -->
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>${druid.verison}</version>
      </dependency>
      <!-- mybatis-springboot整合 -->
      <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>${mybatis.spring.boot.verison}</version>
      </dependency>
      <!--lombok-->
      <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${lombok.version}</version>
      </dependency>
      <!--junit-->
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
      </dependency>
      <!-- log4j -->
      <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

补充:

  1. dependencyManagement

​ 在pom.xml文件中使用了之前没有用过的 dependencyManagement,其通常用在项目中对顶层的父POM中,它能让其所有子项目引用一个依赖而不用显式的列出版本号。此外,dependencyManagement 只是声明依赖,并不实现引入,子项目如果不声明依赖,则不会从父项目中继承下来。

  1. maven跳过单元测试

image-20200704153726352

  1. 父工程创建后,可以使用 maven install 发布到仓库方便子工程继承。

image-20200704154012235

  1. idea忽略显示 .idea 文件夹与 .iml 文件

    image-20200704155844996


支付模块

创建Module

父工程右键 New->Module,创建之后父POM会出现:

image-20200704160146214

同时由于使用了dependencyManagement,子工程的POM如下:

image-20200704160258165

修改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>
                <!--如果没写版本,从父层面找,找到了就直接用,全局统一-->
            </dependency>
            <!--mysql-connector-java-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <!--jdbc-->
            <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>
                <scope>test</scope>
            </dependency>
        </dependencies>

修改application.yml

resources下,如果没有则新建

server:
    port: 8001   
spring:
    application:
        name: cloud-payment-service
    datasource:
    # 当前数据源操作类型
    type: com.alibaba.druid.pool.DruidDataSource
    # mysql驱动类
    driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: 123456
mybatis:            
    mapper-locations: classpath*:mapper/*.xml
    type-aliases-package: com.moke.springcloud.entities

主启动类

com.moke.springcloud包下新建类PaymentMain8001:

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

业务类

sql

实体类

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

entity类

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

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

        }

dao层:

        @Mapper
        public interface PaymentDao {

            public int create(Payment payment);

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

        }

mapper配置文件类

<?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="com.moke.springcloud.dao.PaymentDao">

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

    <resultMap id="BaseResultMap" type="com.moke.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>

写service和serviceImpl

service:

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

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

serviceImpl:

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

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

controller

        @RestController
        @Slf4j
        @RequestMapping("/payment")
        public class PaymentController {
            @Resource
            private PaymentService paymentService;

            @PostMapping("/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,"插入失败");
                }
            }

            @GetMapping("/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,"无记录");
                }
            }
        }
标签: SpringCloud

非特殊说明,本博所有文章均为博主原创。

评论啦~


召唤看板娘