SpringCloud学习(二):消费者订单模块的构建
2023-03-14 22:46:55 时间
一、建module
创建一个普通maven模块
二、修改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> <artifactId>cloud</artifactId> <groupId>com.shang.cloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-consumer-order80</artifactId> <dependencies> <!--包含了sleuth+zipkin--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- <dependency><!– 引入自己定义的api通用包,可以使用Payment支付Entity –>--> <!-- <groupId>com.atguigu.springcloud</groupId>--> <!-- <artifactId>cloud-api-commons</artifactId>--> <!-- </dependency>--> <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>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> </project>
三、编写yml文件
1. server: 2. port: 90
其实这里使用80端口会更好,因为http默认访问80端口,这样消费者就不用在访问的时候加上端口号了。但是我的80端口被Nginx占用,来回禁用也挺麻烦,于是就改成了90端口。
四、编写主启动类
package com.shang.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class OrderMain80 { public static void main(String[] args) { SpringApplication.run(OrderMain80.class, args); } }
五、编写业务代码
由于消费者只需要调用支付模块的方法,因此只需要entity和controller即可。
entity直接复用支付模块的就行了。
package com.shang.cloud.controller; import com.shang.cloud.entities.CommonResult; import com.shang.cloud.entities.Payment; import lombok.extern.slf4j.Slf4j; 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 @Slf4j public class OrderController { public static final String PAYMENT_URL = "http://localhost:8001"; @Autowired private RestTemplate restTemplate; @GetMapping("/consumer/payment/create") public CommonResult<Payment> create(Payment payment){ return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment, CommonResult.class); } @GetMapping("/consumer/payment/get/{id}") public CommonResult<Payment> getPayment(@PathVariable("id") Long id){ return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id, CommonResult.class); } }
注解配置applicationContext
package com.shang.cloud.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(); } }
六、启动测试
可以看到,通过消费者订单模块,我们也查到了内容。
数据也能成功插入
相关文章
- 新版正进入冲刺阶段!Windows 11今年迎来这些大变化
- Windows 11飞速进化:近期新变化盘点
- 微软Windows 11 发布 Build 22579 更新:带来新的开始菜单功能,你的手机,媒体播放器升级
- 微软竟然在文件夹里插广告!真把人恶心到了
- DirectStorage API 登陆 Windows,PC 游戏快速加载新时代
- OpenHarmony啃论文成长计划---Flatbuffers应用于MQTT协议
- 终于加入多标签支持!Windows 11新文件管理器上手
- 微软正重新打磨 Windows 11 的 Windows 搜索界面,Windows 10 用户也有份
- 微软承诺将使Windows 11的右键菜单调出速度更快
- 特写:Windows 11文件管理器中的标签页功能
- OpenHarmony啃论文成长计划---浅谈序列化规范
- 微软重申承诺解决Windows 11应用程序问题
- 微软 Windows 11 Dev 预览版 22567 发布:改进 Microsoft 365 订阅管理、智能 App 控制
- Windows 8 有一只隐藏款的猫
- 微软证实近期例行更新后Windows Server计算机反而更不安全
- 微软放大招了!Windows 11 Build 22557新版详细体验
- OpenHarmony啃论文成长计划—综述视角解读压缩编码
- Windows 11任务管理器“效率模式”的一些有趣细节
- 微软 Windows 11 预览版 Snap 多任务体验:新的视觉 UI 和动画改进
- 实用经典功能回归!Windows 11文件管理器迎来大改