- 问题背景
- 项目搭建
- Lyric: 还我骷髅头 这故事
从零开始学springcloud微服务项目
注意事项:
- 约定 > 配置 > 编码
- IDEA版本2021.1
- 这个项目,我分了很多篇章,每篇文章一个操作步骤,目的是显得更简单明了
- controller调service,service调dao
1 右键新增module
2 点击next
3 输入项目名,cloud-consumer-order80,点击finish
4 替换pom文件
springcloud2022 com.yg 1.0-SNAPSHOT 4.0.0 cloud-consumer-order80 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
5 在resources下新增application.yml文件
server: port: 80
6 新增启动类
package com.yg.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainApp80 {
public static void main(String[] args) {
SpringApplication.run(MainApp80.class);
}
}
7 新增RestTemplate配置类,使用@Bean注入到spring容器,才能使用自动注入的方式
package com.yg.springcloud.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 restTemplate()
{
return new RestTemplate();
}
}
8 添加entities
package com.yg.springcloud.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@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; @Data @AllArgsConstructor @NoArgsConstructor public class CommonResult{ private Integer code; private String message; private T data; public CommonResult(Integer code, String message) { this(code, message, null); } }
9 添加controller
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;
@RestController
public class OrderController {
public static final String PaymentSrv_URL = "http://localhost:8001";
@Autowired
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/create") //客户端用浏览器是get请求,但是底层实质发送post调用服务端8001
public 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 启动MainApp80和PaymentMain8001,选择服务名可以切换日志打印
12 打开postman调用:http://localhost/consumer/payment/get/1
13 postman调用:http://localhost/consumer/payment/create?serial=aa
作为程序员第 174 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …



