SpringCloud--消费模块
- 消费者模块搭建:
-
<?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>SpringCloud</artifactId> <groupId>com.model</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud001-provider-payment8001</artifactId> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <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>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</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> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies> </project>
-
server: port: 80 spring: application: name: springcloud002-consumer-order80
-
package com.model; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/9/6 13:30 */ @SpringBootApplication public class Order80Main { public static void main(String[] args) { SpringApplication.run(Order80Main.class,args); } }
-
-
完成远程服务的调用
-
package com.model.controller; import com.model.pojo.CommonResult; import com.model.pojo.Payment; import com.model.service.PaymentService; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/9/5 23:28 */ @RestController @Slf4j @RequestMapping("/payment") public class PaymentController { @Resource private PaymentService paymentService; @PostMapping("/create") // @RequestBody : Post请求时接受json格式的参数 public CommonResult create(@RequestBody Payment payment){ Integer result = paymentService.create(payment); log.info("插入结果:"+result); if (result>0){ return new CommonResult(200,"插入成功",result); } return new CommonResult(444,"插入失败",null); } @GetMapping("/getById/{id}") public CommonResult getPaymentById(@PathVariable("id")Long id){ Payment payment = paymentService.getPaymentById(id); log.info("查询结果:"+payment.toString()); if (payment!=null){ return new CommonResult(200,"查询成功",payment); } return new CommonResult(444,"查询失败",null); } }
-
package com.model.controller; import com.model.pojo.CommonResult; import com.model.pojo.Payment; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/9/6 14:13 */ @RestController @Slf4j @RequestMapping("/consumer") public class OrderController { public static final String PAYMENT_URL="http://localhost:8001"; @Resource private RestTemplate restTemplate; // 通过消费者订单模块,调用支付模块的controller @GetMapping("/payment/create") public CommonResult<Payment> create(Payment payment){ log.info("参数:"+payment); return restTemplate.postForObject(PAYMENT_URL+"/payment/create", payment, CommonResult.class); } @GetMapping("/payment/getById/{id}") public CommonResult<Payment> get(@PathVariable()Long id){ return restTemplate.getForObject(PAYMENT_URL+"/payment/getById/"+id, CommonResult.class); } }
-