• SpringCloud进阶-消费者模块


    一、消费者订单模块(上)

    在这里插入图片描述

    1、创建名为cloud-consumer-order80的消费者订单模块

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    2、改pom.xml

    order消费者模块下的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>cloud2022</artifactId>
            <groupId>com.atguidu.springcloud</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>cloud-consumer-order80</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
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    3、application.yml

    在这里插入图片描述
    application.yml:

    server:
      port: 80
    
    • 1
    • 2

    4、主启动类

    在这里插入图片描述
    OrderMain80.java:

    package com.atguigu.springcloud;
    
    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);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    5、业务类

    1、实体类需要使用,entities:
    在这里插入图片描述

    Payment.java

    package com.atguigu.springcloud.entities;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    import java.io.Serializable;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Payment implements Serializable {
        private Long id;
        private String serial;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    CommonResult.java

    package com.atguigu.springcloud.entities;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class CommonResult<T>{
        private Integer code;
        private String message;
        private T data;
    
        /**
         * data可能为null
         * @param code
         * @param message
         */
        public CommonResult(Integer code, String message){
            this(code, message, null);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    2、消费者需要一个controller即可
    在这里插入图片描述
    在这里插入图片描述
    3、RestTemplate
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    6、RestTemplate

    1、新建一个ApplicationContextConfig类,在config包下
    在这里插入图片描述
    2、修改ApplicationContextConfig类
    在这里插入图片描述

    ApplicationContextConfig.java

    package com.atguigu.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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    3、在OrderController里使用
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    OrderController.java

    package com.atguigu.springcloud.controller;
    
    import com.atguigu.springcloud.entities.CommonResult;
    import com.atguigu.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 {
        @Resource
        private RestTemplate restTemplate;
    
        public static final String PAYMENT_URL = "http://localhost:8001";
    
        @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);
        }
    
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    在这里插入图片描述
    4、测试
    启动8001和80,
    8001 ok
    在这里插入图片描述
    80端口ok:
    在这里插入图片描述

    在这里插入图片描述

    二、消费者订单模块(下)

    1、完善写操作

    浏览器 - http://localhost/consumer/payment/create?serial=111
    
    虽然,返回成功,但是观测数据库中,并没有创建serial为111的行。
    
    解决之道:在cloud-provider-payment8001工程的PaymentController中添加@RequestBody注解。
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    在这里插入图片描述
    PaymentController.java:

    package com.atguigu.springcloud.controller;
    
    import com.atguigu.springcloud.entities.CommonResult;
    import com.atguigu.springcloud.entities.Payment;
    import com.atguigu.springcloud.service.PaymentService;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.web.bind.annotation.*;
    import javax.annotation.Resource;
    
    /**
     */
    @RestController
    @Slf4j
    @RequestMapping("/payment")
    public class PaymentController{
        @Resource
        private PaymentService paymentService;
    
        @PostMapping(value = "/create")
        public CommonResult create(@RequestBody Payment payment)
        {
            int result = paymentService.create(payment);
            log.info("*****插入结果:"+result);
    
            if(result > 0)
            {
                return new CommonResult(200,"插入数据库成功",result);
            }else{
                return new CommonResult(444,"插入数据库失败",null);
            }
        }
    
        @GetMapping(value = "/get/{id}")
        public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id)
        {
            Payment payment = paymentService.getPaymentById(id);
    
            if(payment != null)
            {
                return new CommonResult(200,"查询成功",payment);
            }else{
                return new CommonResult(444,"没有对应记录,查询ID: "+id,null);
            }
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    在这里插入图片描述
    在这里插入图片描述
    2、开启Run DashBoard
    在这里插入图片描述

    通过修改idea的workspace.xml的方式来快速打开Run Dashboard窗口(这个用来显示哪些Spring Boot工程运行,停止等信息。
    我idea 2020.1版本在名为Services窗口就可以显示哪些Spring Boot工程运行,停止等信息出来,所以这仅作记录参考)。
    
    • 1
    • 2

    2、开启Run DashBoard

    • 打开工程路径下的.idea文件夹的workspace.xml

    • <component name="RunDashboard">中修改或添加以下代码:

    <option name="configurationTypes">
    	<set>
    		<option value="SpringBootApplicationConfigurationType"/>
        </set>
    </option>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    由于idea版本差异,可能需要关闭重启。
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    目前我的idea里没有这个东西,据说现在是下方的services

  • 相关阅读:
    [云原生k8s] k8s管理工具kubectl详解(一)
    Dubbo源码(五) - 服务目录
    【redis总结】
    ThreadLocal夺命11连问
    BGP进阶:BGP 综合实验一
    vins-course运行
    函数柯里化的简单实现和应用
    Java高级——解释执行
    国外LEAD收款渠道介绍:Wise收款教程
    Mysql 索引与事务
  • 原文地址:https://blog.csdn.net/qq_40733968/article/details/125478116