微服务架构——笔记(2)
一、客户客户端模块
文章来源B站视频
尚硅谷SpringCloud框架开发教程(SpringCloudAlibaba微服务分布式架构丨Spring Cloud)教程
本次笔记内容为消费者订单Module模块
1.1 项目名称、目录结构
1.2 Pom.xml
<?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><groupId>com.atliangstar.springcloud</groupId><artifactId>cloud2023</artifactId><version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion><artifactId>could_coustmer_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>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.0</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.10</version></dependency>
</dependencies></project>
1.3 application.yml文件
server:port: 80
1.4 entities
1.4.1 Payment.java
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;
}
1.4.2 CommonResult.java
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.5 两服务间调用
1.order订单微服务调用的是8001
2.两个端口两个服务,不需要 dao,service,impl的实现
3.两个服务间的调用
4.原始web阶段为httpClient
5.现在需要使用restTemplate给httpClient进行封装,实现横向微服务调用
1.5.1 RestTemplate
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容器中
1.6 controller
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";@Resourceprivate 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);}
}
1.7 测试
二、项目重构