08、SpringCloud实战:新建子module消费者订单模块(八)

SpringCloud无介绍快使用,新建子module消费者订单模块(八)

  • 问题背景
  • 项目搭建
  • Lyric: 还我骷髅头 这故事

问题背景

从零开始学springcloud微服务项目
注意事项:

  • 约定 > 配置 > 编码
  • IDEA版本2021.1
  • 这个项目,我分了很多篇章,每篇文章一个操作步骤,目的是显得更简单明了
  • controller调service,service调dao
  • 项目源码以及sentinel安装包

项目搭建

1右键新增module

*

2点击next

*

3输入项目名,cloud-consumer-order80,点击finish

*

4替换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>springcloud2022</artifactId>
        <groupId>com.yg</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

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

    <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.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>

5在resources下新增application.yml文件

server:
  port: 80

6新增启动类

package com.yg.springcloud;

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

/**
 * @Author suolong
 * @Date 2022/6/15 15:10
 * @Version 2.0
 */

@SpringBootApplication
public class MainApp80 {
   
     

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

7新增RestTemplate配置类,使用@Bean注入到spring容器,才能使用自动注入的方式

package com.yg.springcloud.config;

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

/**
 * @Author suolong
 * @Date 2022/6/15 20:53
 * @Version 2.0
 */
@Configuration
public class ApplicationContextConfig {
   
     

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

}

8添加entities

package com.yg.springcloud.entities;

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

import java.io.Serializable;

/**
 * @Author suolong
 * @Date 2022/6/14 21:13
 * @Version 2.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Payment implements Serializable {
   
     

    private Long id;
    private String serial;

}
package com.yg.springcloud.entities;

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

/**
 * @Author suolong
 * @Date 2022/6/14 21:15
 * @Version 2.0
 */
@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);
    }
}

9添加controller

package com.yg.springcloud.controller;

import com.yg.springcloud.entities.CommonResult;
import com.yg.springcloud.entities.Payment;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * @Author suolong
 * @Date 2022/6/15 20:54
 * @Version 2.0
 */

@RestController
public class OrderController {
   
     
    public static final String PaymentSrv_URL = "http://localhost:8001";

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/create") //客户端用浏览器是get请求,但是底层实质发送post调用服务端8001
    public CommonResult create(Payment payment) {
   
     
        return restTemplate.postForObject(PaymentSrv_URL + "/payment/create", payment, CommonResult.class);
    }
    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult getPayment(@PathVariable Long id) {
   
     
        return restTemplate.getForObject(PaymentSrv_URL + "/payment/get/" + id, CommonResult.class, id);
    }

}

10整体目录结构
*

11启动MainApp80和PaymentMain8001,选择服务名可以切换日志打印
*

12打开postman调用:http://localhost/consumer/payment/get/1
*

13postman调用:http://localhost/consumer/payment/create?serial=aa

*

SpringCloud无介绍快使用,Seata处理分布式事务(二十五)
SpringCloud无介绍快使用,sentinel服务熔断功能(二十四)
SpringCloud无介绍快使用,sentinel注解@SentinelResource的基本使用(二十三)
SpringCloud无介绍快使用,sentinel热点key限流与系统规则的基本使用(二十二)
SpringCloud无介绍快使用,sentinel熔断降级和限流的基本使用(二十一)
SpringCloud无介绍快使用,Nacos集群和Nginx代理(二十)
SpringCloud无介绍快使用,nacos配置中心的基本使用(十九)
SpringCloud无介绍快使用,nacos注册中心的基本使用(十八)
SpringCloud无介绍快使用,gateway通过微服务名实现动态路由(十七)
SpringCloud无介绍快使用,gateway的基本使用(十六)
SpringCloud无介绍快使用,Ribbon负载均衡工具与OpenFeign的使用(十五)
SpringCloud无介绍快使用,使用Zookeeper替换Eureka服务注册与发现(十四)
SpringCloud无介绍快使用,服务发现Discovery和Eureka自我保护(十三)
SpringCloud无介绍快使用,集群cloud-provider-payment8002搭建(十二)
SpringCloud无介绍快使用,集群Eureka服务注册中心cloud-eureka-server7002搭建(十一)
SpringCloud无介绍快使用,单机Eureka服务注册中心cloud-eureka-server7001搭建(十)
SpringCloud无介绍快使用,新建cloud-api-commons公共模块module(九)
SpringCloud无介绍快使用,新建子module消费者订单模块(八)
SpringCloud无介绍快使用,热部署devtools配置(七)
SpringCloud无介绍快使用,子module提供者支付微服务业务开发(六)
SpringCloud无介绍快使用,新建子module提供者支付微服务yml整合和新建启动类(五)
SpringCloud无介绍快使用,新建子module提供者支付微服务pom整合(四)
SpringCloud无介绍快使用,springcloud父工程pom文件整理(三)
SpringCloud无介绍快使用,IDEA新建springcloud父工程(二)
SpringCloud无介绍快使用,与Spingboot之间的兼容版本选择(一)

作为程序员第 175 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …

***

Lyric: 还我骷髅头 这故事