一、新建项目
1.选中父工程,右键为父工程添加子模块
2.修改子模块的 pom.xml 配置文件,为其添加必要的库
<dependencies>
<!--web 开发标配-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--web 开发标配-->
<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>
3.为子模块新增 yml 配置文件
server:
# 80 接口是浏览器默认访问端口,这样可以让用户更加方便,无需手动输入端口号
# 但是新版 idea 中如果直接使用 80 端口可能会出现问题,需要解决,所以这里先暂时用 8000
port: 8000
4.添加启动类
package com.lanyue.order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
二、编写代码
注意:
因为客户端(用户)是服务使用方,因此它不需要,也不应该具备操作后台数据库、左右业务功能的能力,他只需要调用后台的其他服务即可(购物、支付、退款 ......);所以客户端只需要有 Controller 层即可,但是它又用到了支付模块的一些实体类和公用工具类,因此需要对应的工具包和实体类包及其代码
1.编写通用客户端返回工具类,JsonResult
package com.lanyue.order.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* 支付实体类
*/
// 替换 get set 方法
@Data
// 全参构造函数
@AllArgsConstructor
// 空参构造函数
@NoArgsConstructor
// 实现序列化接口
public class Payment implements Serializable {
private Long id;
private String serial;
}
2.编写 Payment 实体类(与第一个模块的实体类完全一样,这里是为了减少模块间的依赖,直接重新写一份)
package com.lanyue.order.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* 支付实体类
*/
// 替换 get set 方法
@Data
// 全参构造函数
@AllArgsConstructor
// 空参构造函数
@NoArgsConstructor
// 实现序列化接口
public class Payment implements Serializable {
private Long id;
private String serial;
}
3.新建一个配置文件
package com.lanyue.order.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
4.编写客户端访问 controller
package com.lanyue.order.controller;
import com.lanyue.order.entities.Payment;
import com.lanyue.order.util.JsonResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
public class OrderController {
// 被调用模块 Payment 的基础访问路径
public static final String PAYMENT_URL = "http://localhost:8080/springcloud";
// 一个 controller 调用另一个服务的 controller 本质上是使用 HttpClient/HttpURLConnection 进行接口的调用
// 但是后面出现了一些对这基本工具的功能封装,例如 RestTemplate,Feign
@Resource
private RestTemplate restTemplate;
// 调用 Payment 模块的 controller 接口
@PostMapping("/consumer/payment/insert")
public JsonResult<Payment> insert(@RequestBody Payment payment){
return restTemplate.postForObject(PAYMENT_URL + "/payment/insert", payment, JsonResult.class);
}
// 调用 Payment 模块的 controller 接口
@GetMapping("/consumer/payment/selectPaymentById")
public JsonResult<Payment> selectPaymentById(@RequestParam("id") String id){
return restTemplate.getForObject(PAYMENT_URL + "/payment/selectPaymentById" + "?id=" + id, JsonResult.class);
}
}
5.测试
三、友情小提示
因为 springcloud 项目会包含很多 springboot 项目,这时候我们开启一个项目需要手动选中所有的 springboot 项目,为了方便我们会使用到 RunDashboard 工具统一启动/关闭所有的 springboot项目,如下图所示:
如果 IDEA 中没有显示这个组件的话,我们选中最外层的 springboot(parent 项目),然后右键选择 show in explorer 或 open in explorer;然后进入项目中的 .idea 隐藏文件夹,修改 workspace.xml 配置文件,添加如下组件;最后选中 view ==> tool windows ==> services 或 RunDashboard
<component name="RunDashboard">
<option name="configurationTypes">
<set>
<option value="SpringBootApplicationConfigurationType" />
</set>
</option>
</component>