[toc]

作者:雨中散步撒哈拉
来源:https://liudongdong.top
公众号:雨中散步撒哈拉
备注:欢迎关注公众号,学习技术,一起成长!
吃水不忘挖井人:https://www.bilibili.com/video/BV18E411x7eT

项目结构如下
image.png

一、创建cloud_learn_users_8001模块

image.png

image.png

二、修改pom文件

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>cloud_learn</artifactId>
        <groupId>org.cloud.learn</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud_learn_users_8001</artifactId>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

三、创建application配置文件

server:
  port: 8001

四、创建主启动类

package org.cloud.learn;

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


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

五、编写业务类

1. entities创建实体类

常见Payment文件

package org.learn.cloud.entities;

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

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {

    private Long id;

    private String serial;
}

创建CommonResult实体

package org.learn.cloud.entities;

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

@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);
    }
}

2. 创建config文件

package org.cloud.learn.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();
  }

}

3. 创建controller层

package org.cloud.learn.controller;

import lombok.extern.slf4j.Slf4j;
import org.learn.cloud.entities.CommonResult;
import org.learn.cloud.entities.Payment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@Slf4j
public class UsersController {

  public static final String  HTTP_URL = "http://localhost:7001";
  @Resource
  private RestTemplate restTemplate;


  /**
   * 消费者调用第三方创建.
   * @param payment
   * @return
   */
  @GetMapping("/consume/payment/create")
  public CommonResult<Payment> create (@RequestBody Payment payment) {
    return restTemplate.postForObject(HTTP_URL + "/payment/create", payment, CommonResult.class);
  }

  /**
   * 消费者调用第三方查询.
   * @param id
   * @return
   */
  @GetMapping("/consume/payment/get/{id}")
  public CommonResult<Payment> getPayment (@PathVariable("id") String id) {
    return restTemplate.getForObject(HTTP_URL + "/payment/get/" + id, CommonResult.class);
  }

}

六、测试

保存 请求接口:
localhost:8001/consume/payment/create
image.png

查询 请求接口
localhost:8001/consume/payment/get/1
image.png

获取源码

搜索并关注公众号:雨中散步撒哈拉
回复关键词:006

Q.E.D.


只有创造,才是真正的享受,只有拚搏,才是充实的生活。