Springcloud_H版-alibaba学习笔记(四) 订单模块

本文详细介绍了如何构建一个SpringCloud的订单模块,包括创建模块、编写POM文件、设置application.yml、编写主启动类、实现业务类以及使用RestTemplate进行HTTP请求。此外,还提到了在IntelliJIDEA中管理多个应用的RunDashboard工具的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

四 、订单模块

顺序:
1.建module
2.改pom
3.写yml
4.编写主启动类
5.业务类

1. 工程下创建订单模块

在这里插入图片描述

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>dt-SpringCloud-2020</artifactId>  
        <groupId>com.dt.springcloud</groupId>  
        <version>1.0.0-SNAPSHOT</version>  
    </parent>  
    <modelVersion>4.0.0</modelVersion>  
  
    <artifactId>cloud-consumer-order80</artifactId>  
  
    <properties>  
        <maven.compiler.source>8</maven.compiler.source>  
        <maven.compiler.target>8</maven.compiler.target>  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
    </properties>  
    <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>  
        <!-- 省去Get/Set方法 -->  
        <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>

3.resource下新建application.yml

server:  
  port: 80

4.编写主启动类

package com.dt.springcloud;  
  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;  
  
/**  
 * 主启动类  
 * 若项目不需要连接数据库,需要增加:@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})  
 */@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})  
public class OrderMain80 {  
    public static void main(String[] args) {  
        SpringApplication.run(OrderMain80.class, args);  
    }  
}

5.编写Entities

Entities:Payment 和CommonResutl 同上。

6.RestTemplate

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

配置类

package com.dt.springcloud.config;  
  
import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.Configuration;  
import org.springframework.web.client.RestTemplate;  
  
/**  
 * 配置类  
 */  
@Configuration  
public class ApplicationContextConfig {  
  
    /**  
     * 用注解的方式注入RestTemplate对象,  @Bean相当于在applicationContext.xml中<bean id = "" class="">  
     * @return RestTemplate     */    @Bean  
    public RestTemplate getRestTemplate()  
    {  
        return new RestTemplate();  
    }  
}

7.Controller 层

package com.dt.springcloud.controller;  
  
import com.dt.springcloud.entities.CommonResult;  
import com.dt.springcloud.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;  
  
@RestController  
@Slf4j  
public class OrderController {  
    public static final String PAYMENT_URL = "http://localhost:8001";  
    /**  
     * 注入并实例化  
     */  
    @Resource  
    private RestTemplate restTemplate;  
  
    @GetMapping("/consumer/payment/create")  
    public CommonResult<Payment> create(Payment payment) {  
        return this.restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);  
    }  
  
    @GetMapping("/consumer/payment/get/{id}")  
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id) {  
        return this.restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);  
    }  
}

8.rundashbroad

运用spring cloud框架基于spring boot构建微服务,一般需要启动多个应用程序,在idea开发工具中,多个同时启动的应用
需要在RunDashboard运行仪表盘中可以更好的管理,但有时候idea中的RunDashboard窗口没有显示出来,也找不到直接的开启按钮
idea中打开Run Dashboard的方法如下

view > Tool Windows > Run Dashboard

如果上述列表找不到Run Dashboard,则可以在工程目录下找到.idea文件夹下的workspace.xml,在其中相应位置加入以下代码(替换)即可:

<component name="RunDashboard">
<option name="configurationTypes">
  <set>
    <option value="SpringBootApplicationConfigurationType"/>
  </set>
</option>
<option name="ruleStates">
  <list>
    <RuleState>
      <option name="name" value="ConfigurationTypeDashboardGroupingRule"/>
    </RuleState>
    <RuleState>
      <option name="name" value="StatusDashboardGroupingRule"/>
    </RuleState>
  </list>
</option>
</component>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值