SpringCloud无介绍快使用,新建子module消费者订单模块(八)
问题背景
从零开始学微服务项目
注意事项:
项目搭建
1 右键新增
2 点击next
3 输入项目名,cloud--,点击
4 替换pom文件
<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>springcloud2022artifactId><groupId>com.yggroupId><version>1.0-SNAPSHOTversion>parent><modelVersion>4.0.0modelVersion><artifactId>cloud-consumer-order80artifactId><dependencies><dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-webartifactId>dependency><dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-actuatorartifactId>dependency><dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-devtoolsartifactId><scope>runtimescope><optional>trueoptional>dependency><dependency><groupId>org.projectlombokgroupId><artifactId>lombokartifactId><optional>trueoptional>dependency><dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-testartifactId><scope>testscope>dependency>dependencies>project>
5 在下新增.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 新增配置类,使用@Bean注入到容器,才能使用自动注入的方式
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 {@Beanpublic RestTemplate restTemplate(){return new RestTemplate();}}
8 添加
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 添加
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";@Autowiredprivate RestTemplate restTemplate;@GetMapping("/consumer/payment/create") //客户端用浏览器是get请求,但是底层实质发送post调用服务端8001public 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 启动和,选择服务名可以切换日志打印
12 打开调用:
13 调用:
无介绍快使用,Seata处理分布式事务(二十五)
无介绍快使用,服务熔断功能(二十四)
无介绍快使用,注解@的基本使用(二十三)
无介绍快使用,热点key限流与系统规则的基本使用(二十二)
无介绍快使用,熔断降级和限流的基本使用(二十一)
无介绍快使用,Nacos集群和Nginx代理(二十)
无介绍快使用,nacos配置中心的基本使用(十九)
无介绍快使用,nacos注册中心的基本使用(十八)
无介绍快使用,通过微服务名实现动态路由(十七)
无介绍快使用,的基本使用(十六)
无介绍快使用,负载均衡工具与的使用(十五)
无介绍快使用,使用替换服务注册与发现(十四)
无介绍快使用,服务发现和自我保护(十三)
无介绍快使用,集群cloud--搭建(十二)
无介绍快使用,集群服务注册中心cloud--搭建(十一)
无介绍快使用,单机服务注册中心cloud--搭建(十)
无介绍快使用,新建cloud-api-公共模块(九)
无介绍快使用,新建子消费者订单模块(八)
无介绍快使用,热部署配置(七)
无介绍快使用,子提供者支付微服务业务开发(六)
无介绍快使用,新建子提供者支付微服务yml整合和新建启动类(五)
无介绍快使用,新建子提供者支付微服务pom整合(四)
无介绍快使用,父工程pom文件整理(三)
无介绍快使用,IDEA新建父工程(二)
无介绍快使用,与之间的兼容版本选择(一)
作为程序员第 175 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间, …
Lyric: 还我骷髅头 这故事