springcloud(二)
创建支付Module模块
点击父项目 -> new -> new Module
创建成功查看父pom文件有:
<modules> <module>cloud-provider-payment-8001</module></modules>
说明创建成功
改pom文件
<?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>cloud2020</artifactId> <groupId>com.zzp.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-provider-payment-8001</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>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <!--mysql-connector-java--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--jdbc--> <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> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies></project>
创建yml文件(application.yml)
server: port: 8001 # 端口号spring: application: name: cloud-payment-service # 项目名 datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型 driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包 url: jdbc:mysql://localhost:3306/springcloud?serverTimezone=GMT+8&useUnicode=true&characterEncoding=utf-8&useSSL=false username: "root" password: "123456"mybatis: mapperLocations: classpath:mapper/*.xml type-aliases-package: com.zzp.springcloud.entities # 所有Entity别名类所在包
创建表sql
CREATE TABLE `payment` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增ID', `serial` varchar(64) NOT NULL DEFAULT '' COMMENT '序列号', PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='支付流水号表';
创建mapper文件
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.zzp.springcloud.dao.PaymentDao"> <insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id"> insert into payment(serial) values(#{serial}); </insert> <resultMap id="BaseResultMap" type="Payment"> <id column="id" property="id" jdbcType="BIGINT"></id> <id column="serial" property="serial" jdbcType="VARCHAR"></id> </resultMap> <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap"> select * from payment where id = #{id}; </select></mapper>
dao类
package com.zzp.springcloud.dao;import com.zzp.springcloud.entities.Payment;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;/** * @author zzp * @create 2021-02-10 */@Mapperpublic interface PaymentDao { int create(Payment payment); Payment getPaymentById(@Param("id") Long id);}
主实体类:
package com.zzp.springcloud.entities;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import java.io.Serializable;/** * @author zzp * @create 2021-02-10 */@Data@AllArgsConstructor@NoArgsConstructorpublic class Payment implements Serializable { /** * 自增id */ private Long id; private String serial;}
响应实体类
package com.zzp.springcloud.entities;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;/** * @author zzp * @create 2021-02-10 */@Data@AllArgsConstructor@NoArgsConstructorpublic class CommonResult<T> { private Integer code; private String msg; private T data; public CommonResult(Integer code,String msg){ this(code,msg,null); }}
服务类
package com.zzp.springcloud.service;import com.zzp.springcloud.entities.Payment;import org.apache.ibatis.annotations.Param;/** * @author zzp * @create 2021-02-10 */public interface PaymentService { int create(Payment payment); Payment getPaymentById(@Param("id") Long id);}
package com.zzp.springcloud.service.impl;import com.zzp.springcloud.dao.PaymentDao;import com.zzp.springcloud.entities.Payment;import com.zzp.springcloud.service.PaymentService;import org.springframework.stereotype.Service;import javax.annotation.Resource;/** * @author zzp * @create 2021-02-10 */@Servicepublic class PaymentServiceImpl implements PaymentService { @Resource private PaymentDao paymentDao; @Override public int create(Payment payment) { return paymentDao.create(payment); } @Override public Payment getPaymentById(Long id) { return paymentDao.getPaymentById(id); }}
控制类
package com.zzp.springcloud.controller;import com.zzp.springcloud.entities.CommonResult;import com.zzp.springcloud.entities.Payment;import com.zzp.springcloud.service.PaymentService;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 javax.annotation.Resource;/** * @author zzp * @create 2021-02-10 */@RestController@Slf4jpublic class PaymentController { @Resource private PaymentService paymentService; @PostMapping(value = "/payment/create") public CommonResult create(Payment payment){ int count = paymentService.create(payment); log.info("*******插入结果:{}",count); if(0 < count ){ return new CommonResult(200,"插入数据库成功",count); }else { return new CommonResult(404,"插入数据库失败",null); } } @GetMapping(value = "/payment/get/{id}") public CommonResult getPaymentById(@PathVariable("id") Long id){ Payment payment = paymentService.getPaymentById(id); log.info("*******查询结果:{}",payment); if(null != payment ){ return new CommonResult(200,"查询成功",payment); }else { return new CommonResult(404,"没有对应记录,查询ID: " id,null); } }}
启动类
package com.zzp.springcloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * @author zzp * @create 2021-02-10 */@SpringBootApplicationpublic class PaymentMain8001 { public static void main(String[] args) { SpringApplication.run(PaymentMain8001.class,args); }}
启动
测试
日记
成功
热部署设置
测试用,上线要删除
在cloud-payment-service(8001)的pom文件添加依赖
<!--热部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency>
在父pom添加
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <addResources>true</addResources> </configuration> </plugin> </plugins> </build>
idea开通自动设置
File -> Setting ->Build,Exection,Deployment -> Compiler
更新热部署快捷键
ctrl shift Alt /
重启idea
创建消费者订单Modul模块
改pom文件
<?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>cloud2020</artifactId> <groupId>com.zzp.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-consumer-order-80</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>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文件(application.yml)
server: port: 80 # 端口号
启动类
package com.zzp.springcloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @author zzp* @create 2021-02-12*/@SpringBootApplicationpublic class OrderMain80 { public static void main(String[] args) { SpringApplication.run(OrderMain80.class,args); }}
实体参考cloud-payment-service项目
创建RestTemplate 用来调用cloud-payment-service服务
package com.zzp.springcloud.config;import org.springframework.context.annotation.Bean;import org.springframework.stereotype.Component;import org.springframework.web.client.RestTemplate;/** * @author zzp * @create 2021-02-12 */@Componentpublic class RestTemplateConfig { @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); }}
控制层
package com.zzp.springcloud.controller;import com.zzp.springcloud.entities.CommonResult;import com.zzp.springcloud.entities.Payment;import lombok.extern.slf4j.Slf4j;import org.springframework.http.ResponseEntity;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;import javax.annotation.Resource;/** * @author zzp * @create 2021-02-12 */@RestController@Slf4jpublic class OrderController { public static final String PAYMENT_URL = "http://127.0.0.1:8001"; @Resource private RestTemplate restTemplate; @GetMapping(value = "/consumer/payment/create") public ResponseEntity<CommonResult> create(Payment payment){ return restTemplate.postForEntity(PAYMENT_URL "/payment/carete",payment,CommonResult.class); } @GetMapping(value = "/consumer/payment/get/{id}") public ResponseEntity<CommonResult> getPayment(@PathVariable("id") Long id){ return restTemplate.getForEntity(PAYMENT_URL "/payment/get/" id,CommonResult.class); } }
启动两个服务
查询成功
但是请求http://127.0.0.1/consumer/payment/create?serial=88822 会失败
记得在payment服务的控制类加上@RequestBody
@PostMapping(value = "/payment/create") public CommonResult create(@RequestBody Payment payment){
工程重构
把payment和order相同的实体放入一个公共的工程里
改pom文件
<?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>cloud2020</artifactId> <groupId>com.zzp.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-api-commons</artifactId> <dependencies> <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>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.1.0</version> </dependency> </dependencies></project>
把实体类放入commons
在payment和order项目pom文件添加
<!-- 引入自定义的API通用包 实体类 --> <dependency> <groupId>com.zzp.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency>
删除自身的entities目录
启动两服务测试
赞 (0)