springcloud微服务(三)-支付模块

在没有微服务的情况下,生产者和消费者的模型如下:

一、创建子模块-支付模块payment8001

二、修改pom文件

<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.14</version>
        </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>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

三、配置application.yml文件

server:
  port: 8001

spring:
  application:
    name: cloud-payment-service

  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssm_crud?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: 123456

mybatis:
  type-aliases-package: com.atorientsec.springcloud.entities
  mapper-locations: classpath:mapper/*.xml

3.1 每个module都需要一个端口号,端口代表一个module(应用程序)

3.2 spring.application.name 应用的名字

3.3 spring.datasource 代表spring整合的数据库

3.4 mybatis: type-aliases-package给每个实体类起别名,类名的小写

        mapper-locations:mapper接口与xml文件关联的路径

四、创建主程序类

@SpringBootApplication
public class PaymentMain8001 {
    public static void main(String[] args)
    {
        SpringApplication.run(PaymentMain8001.class,args);
    }
}

总结:创建微服务的步骤:创建module->改pom->创建yml文件->创建主类

下面开始业务类

一、创建实体

由于与web端交互,应答的是json消息体,而且web端不会解析每一个不同的消息体。所以我们需要创建一个公共的子模块,来保存我们的所有的实体类。先打成jar包,其他的子模块pom引入此jar包。这样其他的子模块就不需要创建实体类了。

1.1 CommonResult类就是我们与web端交互的类,此类的字段有状态码,消息,以及数据

@Data
@NoArgsConstructor
public class CommonResult<T> {
    private Integer code;
    private String message;
    private T data;

    public CommonResult(Integer code,String message)
    {
        this.code=code;
        this.message=message;
    }
    public CommonResult(Integer code,String message,T data)
    {
        this(code, message);
        this.data = data;
    }
}

1.2 Payment实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Payment implements Serializable {
    private Long id;
    private String serial;
}

注:实体类的序列化 implements Serializable

序列化就是对实例对象的状态(State 对象属性而不包括对象方法)进行通用编码(如格式化的字节码)并保存,以保证对象的完整性和可传递性。

简而言之:序列化,就是为了在不同时间或不同平台的JVM之间共享实例对象

二、修改payment8001的pom文件引入cloud-api-commons依赖

<dependency>
    <groupId>otc.orientsec</groupId>
    <artifactId>cloud-api-commons</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

三、dao层

@Mapper
public interface PaymentMapper {
    Payment getPaymentById(@Param("id") Long id);
    int addPayment(Payment payment);
}

 3.1 @Mapper注解

@Mapper 一般我们用在接口上,使用 @Mapper,最终 Mybatis 会有一个拦截器,会自动的把 @Mapper 注解的接口生成动态代理类。

3.2 @Param注解

采用#{}的方式把@Param注解括号内的参数进行引用

3.3 Mapper接口对应的XML文件

<mapper namespace="com.atorientsec.springcloud.mapper.PaymentMapper">
    <!--
     Payment getPaymentById(@Param("id") Integer id);
    int addPayment(Payment payment);
    -->
    <resultMap id="paymentMap" type="payment">
        <id property="id" column="id" jdbcType="BIGINT"></id>
        <id property="serial" column="serial" jdbcType="VARCHAR"></id>
    </resultMap>
    <select id="getPaymentById" parameterType="Long" resultMap="paymentMap">
        select id,serial from ssm_crud.payment where id=#{id};
    </select>

    <insert id="addPayment" parameterType="payment">
        insert into ssm_crud.payment(serial) values(#{serial});
    </insert>
</mapper>

四、services层

@Service
public class PaymentServices {

    @Autowired
    private PaymentMapper paymentMapper;

    public Payment getPaymentById(Long id)
    {
        return paymentMapper.getPaymentById(id);
    }
    public int addPayment( Payment payment)
    {
        return paymentMapper.addPayment(payment);
    }

}

五、controller层

@RestController
@Slf4j
public class PaymentController {

    @Autowired
    private PaymentServices paymentServices;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id)
    {
         Payment payment=paymentServices.getPaymentById(id);
         log.info("********查询结果*********:"+payment);
         if(payment != null)
         {
             return new CommonResult<>(200,"查询数据成功,serverport:"+serverPort,payment);
         }else
         {
             return new CommonResult<>(444,"查询到对应的记录,查询ID:"+id);
         }

    }
    @PostMapping("/payment/create")
    public CommonResult addPayment(@RequestBody Payment payment)
    {

       int result = paymentServices.addPayment(payment);
       log.info("******插入结果:"+result);
       if(result>0)
       {
           return new CommonResult(200,"插入成功");
       }else
       {
           return new CommonResult(445,"插入失败");
       }
    }
}

5.1 可以看到返回类型都是公共的CommonResult类型的,web端容易解析

5.2 @RequestBody注解

作用:@RequestBody注解用于读取http请求的内容(字符串),通过springmvc提供的HttpMessageConverter接口将读到的内容(json数据)转换为java对象并绑定到Controller方法的参数上。

5.3 @Value()注解:

@Value("${XXX}"):注解从配置文件读取值的用法

六、Jemeter测试接口

1. 添加 配置元件 ->HTTP信息头管理器

解决方案:就是在  HTTP信息头管理器

配置Content-Type:application/json

2. 添加->后置处理器->BeanShell监听器

 解决查询时应答体中文乱码的问题。

3. 结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值