文章来源B站视频
尚硅谷SpringCloud框架开发教程(SpringCloudAlibaba微服务分布式架构丨Spring Cloud)教程
本次笔记内容为消费者订单Module模块
com.atliangstar.springcloud
cloud2023
1.0-SNAPSHOT
4.0.0
could_coustmer_order80
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
mysql
mysql-connector-java
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-devtools
runtime
true
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.0
com.alibaba
druid-spring-boot-starter
1.1.10
server:
port: 80
package com.liangstar.springcloud.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.ibatis.annotations.Mapper;
import java.io.Serializable;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Mapper
public class Payment implements Serializable {
private Long id;
private String serial;
}
package com.liangstar.springcloud.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);
}
}
1.order订单微服务调用的是8001
2.两个端口两个服务,不需要 dao,service,impl的实现
3.两个服务间的调用
4.原始web阶段为httpClient
5.现在需要使用restTemplate给httpClient进行封装,实现横向微服务调用
RestTemplate提供了多种便捷访问远程Http服务的方法是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集
完成80–>8001远程调用
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html
调用起来三个方法
(url,requestMap,ResponseBean.class)这三个参数分别代表为Rest请求地址、请求参数、Http响应转换被转换成的对象类型。
注入到spring容器中
package com.liangstar.springcloud.controller;
import com.liangstar.springcloud.entities.CommonResult;
import com.liangstar.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.PostMapping;
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:8081";
@Resource
private RestTemplate restTemplate;
@PostMapping(value="/consumer/payment/create")
public CommonResult<Payment> create(Payment payment){
log.info("消费者consumer创建启动");
return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
}
@GetMapping(value="/consumer/payment/get/{id}")
public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
log.info("消费者consumer查询启动");
return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
}
}
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>cloud2023</artifactId>
<groupId>com.atliangstar.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud_api_commons</artifactId>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</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>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
</project>
使用mvn clear /install、或者如图:
生成个jar包
在8001与80pom文件中引用,本地仓库依赖
com.atliangstar.springcloud
cloud_api_commons
${project.version}
在clean失败时,及时分析错误,保证构建成功
https://developer.aliyun.com/mvn/guide
及时去查看依赖即可!