SpringCloud&alibaba学习笔记系列1

SpringCloud&alibaba学习笔记系列
该笔记仅作为个人学习使用。
github:https://github.com/sj110110/springcloud2020.git

第一章微服务架构概述及入门

1.1微服务架构概述

​ 微服务架构是一种架构模式,它提倡将单一应用程序划分为一组小的服务,服务之间互相协调、互相配合,为用户提供最终价值。每个服务运行在其独立的进程中,服务与服务之间采用轻量级的通信机制互相协作(通常是基于HTTP协议的RESTful API)。每个服务都围绕着具体业务进行构建,并且能够被独立的部署到生产环境、类生产环境等。另外,应当尽量避免统一的、集中式的服务管理机制,对具体的一个服务而言,应根据业务上下文,选择合适的语音、工具对其进行构建。

1.1.1SpringCloud技术栈

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9wPKc1rJ-1605787919031)(C:\Users\sj\AppData\Roaming\Typora\typora-user-images\image-20201009064453913.png)]

1.1.2SpringBoot和SpringCloud选型

SpringBoot 2.x版和springcloud H版

springboot选型

​ https://github.com/spring-projects/spring-boot/releases

springcloud选型

​ https://github.com/spring-projects/spring-cloud/wiki

官网:https://spring.io/projects/spring-cloud

springcloud对应springcloud版本:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wZCCENQ9-1605787919033)(C:\Users\sj\AppData\Roaming\Typora\typora-user-images\image-20201009070624265.png)]

更加详细的版本对应关系:

https://start.spring.io/actuator/info

{
  "git": {
    "branch": "ba82e022a38b596b5a6c0173a4652d91a8e8ea34",
    "commit": {
      "id": "ba82e02",
      "time": "2020-10-08T14:28:31Z"
    }
  },
  "build": {
    "version": "0.0.1-SNAPSHOT",
    "artifact": "start-site",
    "versions": {
      "spring-boot": "2.3.4.RELEASE",
      "initializr": "0.10.0-SNAPSHOT"
    },
    "name": "start.spring.io website",
    "time": "2020-10-08T14:42:26.791Z",
    "group": "io.spring.start"
  },
  "bom-ranges": {
    "azure": {
      "2.0.10": "Spring Boot >=2.0.0.RELEASE and <2.1.0.RELEASE",
      "2.1.10": "Spring Boot >=2.1.0.RELEASE and <2.2.0.M1",
      "2.2.4": "Spring Boot >=2.2.0.M1 and <2.3.0.M1",
      "2.3.5": "Spring Boot >=2.3.0.M1"
    },
    "codecentric-spring-boot-admin": {
      "2.0.6": "Spring Boot >=2.0.0.M1 and <2.1.0.M1",
      "2.1.6": "Spring Boot >=2.1.0.M1 and <2.2.0.M1",
      "2.2.4": "Spring Boot >=2.2.0.M1 and <2.3.0.M1",
      "2.3.0": "Spring Boot >=2.3.0.M1 and <2.4.0-M1"
    },
    "solace-spring-boot": {
      "1.0.0": "Spring Boot >=2.2.0.RELEASE and <2.3.0.M1",
      "1.1.0": "Spring Boot >=2.3.0.M1"
    },
    "solace-spring-cloud": {
      "1.0.0": "Spring Boot >=2.2.0.RELEASE and <2.3.0.M1",
      "1.1.1": "Spring Boot >=2.3.0.M1"
    },
    "spring-cloud": {
      "Finchley.M2": "Spring Boot >=2.0.0.M3 and <2.0.0.M5",
      "Finchley.M3": "Spring Boot >=2.0.0.M5 and <=2.0.0.M5",
      "Finchley.M4": "Spring Boot >=2.0.0.M6 and <=2.0.0.M6",
      "Finchley.M5": "Spring Boot >=2.0.0.M7 and <=2.0.0.M7",
      "Finchley.M6": "Spring Boot >=2.0.0.RC1 and <=2.0.0.RC1",
      "Finchley.M7": "Spring Boot >=2.0.0.RC2 and <=2.0.0.RC2",
      "Finchley.M9": "Spring Boot >=2.0.0.RELEASE and <=2.0.0.RELEASE",
      "Finchley.RC1": "Spring Boot >=2.0.1.RELEASE and <2.0.2.RELEASE",
      "Finchley.RC2": "Spring Boot >=2.0.2.RELEASE and <2.0.3.RELEASE",
      "Finchley.SR4": "Spring Boot >=2.0.3.RELEASE and <2.0.999.BUILD-SNAPSHOT",
      "Finchley.BUILD-SNAPSHOT": "Spring Boot >=2.0.999.BUILD-SNAPSHOT and <2.1.0.M3",
      "Greenwich.M1": "Spring Boot >=2.1.0.M3 and <2.1.0.RELEASE",
      "Greenwich.SR6": "Spring Boot >=2.1.0.RELEASE and <2.1.18.BUILD-SNAPSHOT",
      "Greenwich.BUILD-SNAPSHOT": "Spring Boot >=2.1.18.BUILD-SNAPSHOT and <2.2.0.M4",
      "Hoxton.SR8": "Spring Boot >=2.2.0.M4 and <2.3.5.BUILD-SNAPSHOT",
      "Hoxton.BUILD-SNAPSHOT": "Spring Boot >=2.3.5.BUILD-SNAPSHOT and <2.4.0.M1",
      "2020.0.0-M3": "Spring Boot >=2.4.0.M1 and <=2.4.0.M1",
      "2020.0.0-M4": "Spring Boot >=2.4.0.M2 and <2.4.0-SNAPSHOT",
      "2020.0.0-SNAPSHOT": "Spring Boot >=2.4.0-SNAPSHOT"
    },
    "spring-cloud-alibaba": {
      "2.2.1.RELEASE": "Spring Boot >=2.2.0.RELEASE and <2.3.0.M1"
    },
    "spring-cloud-services": {
      "2.0.3.RELEASE": "Spring Boot >=2.0.0.RELEASE and <2.1.0.RELEASE",
      "2.1.7.RELEASE": "Spring Boot >=2.1.0.RELEASE and <2.2.0.RELEASE",
      "2.2.3.RELEASE": "Spring Boot >=2.2.0.RELEASE and <2.3.0.M1"
    },
    "spring-statemachine": {
      "2.0.0.M4": "Spring Boot >=2.0.0.RC1 and <=2.0.0.RC1",
      "2.0.0.M5": "Spring Boot >=2.0.0.RC2 and <=2.0.0.RC2",
      "2.0.1.RELEASE": "Spring Boot >=2.0.0.RELEASE"
    },
    "vaadin": {
      "10.0.17": "Spring Boot >=2.0.0.M1 and <2.1.0.M1",
      "14.3.7": "Spring Boot >=2.1.0.M1 and <2.4.0-M1"
    },
    "wavefront": {
      "2.0.1": "Spring Boot >=2.1.0.RELEASE"
    }
  },
  "dependency-ranges": {
    "okta": {
      "1.2.1": "Spring Boot >=2.1.2.RELEASE and <2.2.0.M1",
      "1.4.0": "Spring Boot >=2.2.0.M1 and <2.4.0-M1"
    },
    "mybatis": {
      "2.0.1": "Spring Boot >=2.0.0.RELEASE and <2.1.0.RELEASE",
      "2.1.3": "Spring Boot >=2.1.0.RELEASE and <2.4.0-M1"
    },
    "geode": {
      "1.2.10.RELEASE": "Spring Boot >=2.2.0.M5 and <2.3.0.M1",
      "1.3.4.RELEASE": "Spring Boot >=2.3.0.M1 and <2.4.0-M1",
      "1.4.0-M3": "Spring Boot >=2.4.0-M1"
    },
    "camel": {
      "2.22.4": "Spring Boot >=2.0.0.M1 and <2.1.0.M1",
      "2.25.2": "Spring Boot >=2.1.0.M1 and <2.2.0.M1",
      "3.3.0": "Spring Boot >=2.2.0.M1 and <2.3.0.M1",
      "3.5.0": "Spring Boot >=2.3.0.M1 and <2.4.0-M1"
    },
    "open-service-broker": {
      "2.1.3.RELEASE": "Spring Boot >=2.0.0.RELEASE and <2.1.0.M1",
      "3.0.4.RELEASE": "Spring Boot >=2.1.0.M1 and <2.2.0.M1",
      "3.1.1.RELEASE": "Spring Boot >=2.2.0.M1 and <2.4.0-M1"
    }
  }
}

(尚硅谷阳哥)最终选型:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1aTavmtN-1605787919034)(C:\Users\sj\AppData\Roaming\Typora\typora-user-images\image-20201009071218229.png)]

1.1.3cloud组件停更说明

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nZ5TsA4c-1605787919036)(C:\Users\sj\AppData\Roaming\Typora\typora-user-images\image-20201009072500367.png)]

1.2微服务父工程搭建

1.2.1新建父工程project

项目名:springcloud2020

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iwJ2r5N6-1605787919037)(C:\Users\sj\AppData\Roaming\Typora\typora-user-images\image-20201117210920684.png)]

1.2.2父工程POM文件
<?xml version="1.0" encoding="UTF-8"?>

<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>com.jzt</groupId>
  <artifactId>springcloud2020</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>

  <!--子模块继承之后,提供作用:锁定版本+子模块不用写groupid和version-->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Hoxton.SR1</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.projectlombok</groupId>
        <artifactId>lombok</artifactId>
      </dependency>
      <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>${mybatis.spring.boot.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.2.3maven工程陆地细节

复习dependenceManagement

Maven使用dependencyManagement元素来提供了一种管理依赖版本号的方式。

通常会在一个组织或者项目的最顶层的服务POM中看到dependencyManagement元素。

使用pom.xml中的dependencyManagement元素能让所有在子项目中引用一个依赖而不用显示的列出版本号。

Maven会沿着父子层次向上走,知道找到一个拥有dependencyManagement元素的项目,然后他就会使用这个dependencyManagement元素中指定的版本号:

例如在父项目中:

<dependencyManagement>
	<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.2</version>
      </dependency>
  </dependencyManagement>

然后在自项目中就可以添加mysql-connector时可以不用再指定版本号,例如:

<dependencies>
		<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
 </dependencies>

这样做的好处就是:如果多个子项目都引用同样的依赖,则可以避免在每个使用的子项目中都声明一个版本号,这样当想升级或者切换到另一个版本时,只需要在顶层父容器中更新,而不需要一个一个在子项目中修改;另外如果某个子项目需要另外的一个版本,只需要生命version即可。

注意:

  • dependencyManagement里只声明依赖,并不实现引入,因此子项目需要显示声明需要用的依赖;

  • 如果不在子项目中声明依赖,是不会在复兴门中继承下来的;只有在自项目中写了该依赖项,并没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父POM;

  • 如果子项目中指定了版本号,那么会使用自项目中指定的jar版本。

1.2.4maven跳过单元测试:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0HZ42IQo-1605787919038)(C:\Users\sj\AppData\Roaming\Typora\typora-user-images\image-20201010071102539.png)]

父工程创建完成执行mvn:install将父工程发布到仓库方便子工程继承。

1.3微服务工程构建

搭建两个工程:order订单模块微服务(服务调用者)和payment支付模块微服务(服务提供者)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ozrn065X-1605787919039)(C:\Users\sj\AppData\Roaming\Typora\typora-user-images\image-20201010071853717.png)]

1.3.1支付模块构建

1、建module

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-R3Bishcx-1605787919040)(C:\Users\sj\AppData\Roaming\Typora\typora-user-images\image-20201117212957382.png)]

2、该pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>springcloud2020</artifactId>
        <groupId>com.jzt</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-provider-payment8001</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <!-- 添加这个依赖之后就可以创建一个web应用程序。starter poms部分可以引入所有需要在实际项目中使用的依赖。
            spring-boot-starter-web依赖包含所有的spring-core, spring-web, spring-webmvc,嵌入的Tomcat server和其他web应用相关的库。 -->
            <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.10</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>
            <scope>test</scope>
        </dependency>

    </dependencies>

</project>

3、写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.jzt.springcloud.entities #所有entity别名类所在包

4、主启动类

package com.jzt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 主启动类
 */
@SpringBootApplication
public class SpringCloudApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudApplication.class, args);
    }
}

5、业务类

1)建表SQL

Create database db2020;
CREATE TABLE payment
(
	id 数据类型,
列名称2 数据类型,
.......
)

2)Entities

Payment

package com.jzt.entities;

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

import java.io.Serializable;

/**
 * @author sj
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {

    private long id;

    private String serial;
}

3)CommonResult

用于封装返回给前端的实体

package com.jzt.entities;

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

/**
 * @author sj
 */
@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);
    }
}

4)dao

IPaymentDao

package com.jzt.dao;

import com.jzt.entities.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

/**
 * @author sj
 */
@Mapper
public interface IPaymentDao {

    public int addPayment(Payment payment);

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

5)service

IPaymentService

package com.jzt.service;

import com.jzt.entities.Payment;
import org.apache.ibatis.annotations.Param;


/**
 * @author sj
 */
public interface IPaymentService {
    public int addPayment(Payment payment);

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

6)PaymentServiceImpl

package com.jzt.service.impl;

import com.jzt.dao.IPaymentDao;
import com.jzt.entities.Payment;
import com.jzt.service.IPaymentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author sj
 */
@Service
public class PaymentServiceImpl implements IPaymentService {

    @Resource
    private IPaymentDao iPaymentDao;

    @Override
    public int addPayment(Payment payment) {
        return iPaymentDao.addPayment(payment);
    }

    @Override
    public Payment getPaymentById(long id) {
        return iPaymentDao.getPaymentById(id);
    }
}

7)controller

PaymentController

package com.jzt.controller;

import com.jzt.entities.CommonResult;
import com.jzt.entities.Payment;
import com.jzt.service.IPaymentService;
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;

/**
 * @author sj
 */
@Slf4j
@RestController
public class PaymentController {

    @Resource
    private IPaymentService iPaymentService;

    @PostMapping(value = "/payment/add")
    public CommonResult add(Payment payment){
        int result = iPaymentService.addPayment(payment);
        log.info("****插入数据:"+result);
        if(result > 0){
            return new CommonResult(200, "插入数据成功!",result);
        }else{
            return new CommonResult(502, "插入数据失败", null);
        }
    }

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult getById(@PathVariable("id") Long id){
        Payment payment = iPaymentService.getPaymentById(id);
        log.info("****查询数据:"+payment);
        if(payment != null){
            return new CommonResult(200, "查询数据成功!",payment);
        }else{
            return new CommonResult(502, "查询数据失败,查询ID:"+id, null);
        }
    }
}

6、测试:

访问http://localhost:8001/payment/get/31

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UTbpKVGF-1605787919040)(C:\Users\sj\AppData\Roaming\Typora\typora-user-images\image-20201118074917310.png)]

热部署(开发阶段)

1)在pom.xml中添加对应依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

2)在聚合工程的总工程师的pom.xml中添加构建插件

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

3)开启自动编译权限

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-npqp4ua9-1605787919041)(C:\Users\sj\AppData\Roaming\Typora\typora-user-images\image-20201013075333926.png)]

4)开启自动更新值

快捷键:alt+shift+ctrl+/

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4IZBXKog-1605787919041)(C:\Users\sj\AppData\Roaming\Typora\typora-user-images\image-20201013075510159.png)]

5)重启idea

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DMDT59ow-1605787919042)(C:\Users\sj\AppData\Roaming\Typora\typora-user-images\image-20201013075815890.png)]

1.3.2消费者订单模块构建

1、建立module

新建module,cloud-consumer-order80;

2、修改POM

<?xml version="1.0" encoding="UTF-8"?>
<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>springcloud2020</artifactId>
        <groupId>com.jzt</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consumer-order80</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <!-- 添加这个依赖之后就可以创建一个web应用程序。starter poms部分可以引入所有需要在实际项目中使用的依赖。
            spring-boot-starter-web依赖包含所有的spring-core, spring-web, spring-webmvc,嵌入的Tomcat server和其他web应用相关的库。 -->
            <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>
            <version>1.18.14</version>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

3、写yml

server:
  port: 80

4、主启动

package com.jzt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 功能描述:
 *
 * @Author: sj
 * @Date: 2020/10/16 7:27
 */
@SpringBootApplication
public class OrderApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }
}

5、业务类

1)RestTemplete介绍

​ RestTemplete提供了多种便捷访问远程Http服务的方法,是一种简单便捷的范文restful服务模板类,时Spring提供的用于访问Rest服务的客户端模板工具集。

官网:https://docs.spring.io/spring-framework/docs/5.2.11.RELEASE/javadoc-api/org/springframework/web/clientRestTemplete.html

使用:

创建配置类ApplicationContextConfig

package com.jzt.config;

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

/**
 * 功能描述:
 *
 * @Author: sj
 * @Date: 2020/10/16 7:37
 */
@Configuration
public class ApplicationContextConfig {

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

使用@Bean将返回的RestTemplate对象加入到spring容器中。这样就可以使用RestTemplete了。

2)controller

package com.jzt.controller;

import entities.CommonResult;
import 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;

/**
 * 功能描述:
 *
 * @Author: sj
 * @Date: 2020/10/16 7:30
 */
@RestController
@Slf4j
public class OrderController {

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

    @Resource
    private RestTemplate restTemplate;

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

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

3)拷贝entities

​ 将8001服务业务中的entities包拷贝到80服务下;

6、测试

启动后就也可以测试了:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IXkW0fNM-1605787919043)(C:\Users\sj\AppData\Roaming\Typora\typora-user-images\image-20201018074044077.png)]

1.3.3工程重构

将8001和80微服务中的公共代码抽取成一个公共服务中:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BkmxLo5i-1605787919043)(C:\Users\sj\AppData\Roaming\Typora\typora-user-images\image-20201117220053669.png)]

这样在其他服务中就可以同过POM依赖的方式使用公共代码了。

        <dependency>
            <groupId>com.jzt</groupId>
            <artifactId>cloud-api-common</artifactId>
            <version>${project.version}</version>
        </dependency>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值