欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Kite的学习历程之SpringCloud之Rest微服务构建之微服务消费者Moudle

程序员文章站 2022-06-06 14:54:04
...

Kite学习框架的第二十一天

注意我这里使用的端口为: 81

1 创建消费者Model

创建的目录结构
Kite的学习历程之SpringCloud之Rest微服务构建之微服务消费者Moudle

1.1 修改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>demo01_cloud</artifactId>
        <groupId>cn.kitey.springc;oud</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>

1.2 创建yml配置文件

只需要设置端口号即可

server:
  port: 81

1.3 创建实体类

Payment实体类从上一个工程复制过来

package cn.kitey.springcloud.entities;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 实体类
 */
@Data  //它包含getter、setter、NoArgsConstructor注解,即当使用当前注解时,会自动生成包含的所有方法;
@AllArgsConstructor    //全参构造
@NoArgsConstructor     //无参构造
public class Payment implements Serializable {
    private Long id;
    private String serial;
}


CommonResult实体类

package cn.kitey.springcloud.entities;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 信息返回给前端人员
 * @param <T>
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {
    //返回的404  500 等
    private Integer code;
    //返回的信息  例如错误信息
    private String message;

    private T data;

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


1.4 创建配置类

配置RestTemaplate,为了在controller中进行方法的调用

package cn.kitey.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 getRestTemplate(){
        return  new RestTemplate();
    }
}


1.4 创建控制类

这时需要在前面的8001工程的PaymentController中修改

@PostMapping("/payment/create")
    public CommonResult create(@RequestBody Payment payment){
        int result = paymentService.create(payment);
        log.info("******插入结果是" + result);
        if(result > 0){
            return new CommonResult(result, "200", "插入数据成功!");
        }else{
            return new CommonResult(444,"插入数据失败!", null);
        }
    }

创建的OrderController

package cn.kitey.springcloud.controller;

import cn.kitey.springcloud.entities.CommonResult;
import cn.kitey.springcloud.entities.Payment;
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.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@Slf4j
public class OrderController {

      //定义访问的url
    private static final String PAYMENT_URL = "http://localhost:8001";

    @Resource
    private RestTemplate restTemplate;

    /**
     * 数据的插入
     * @param payment
     * @return
     */
    @GetMapping("consumer/payment/create")
    public CommonResult<Payment> create(Payment payment){
        return restTemplate.postForObject(PAYMENT_URL+"payment/create",
                payment,CommonResult.class);
    }

    /**
     * 数据的查询
     * @param id
     * @return
     */
    @GetMapping("consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/" + id,
                CommonResult.class);
    }
}


1.5 进行测试

我们通过
http://localhost:81/consumer/payment/get/4 访问 查询数据
Kite的学习历程之SpringCloud之Rest微服务构建之微服务消费者Moudle

相关标签: 每天的学习笔记