springcloud的学习---支付模块消费者

很简单,因为消费者不需要再去编写底层代码只需要调用,所以只要有控制类
在这里插入图片描述
application.yml

	server:
  port: 80

从这里开始讲步骤,因为我们这里需要调用8001的端口,所以我们需要使用RestTemplate来调用。
1,先写个配置类,ApplicationContextConfig

package com.zsp.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();
    }

}
 

2,到OrderController去调用


package com.zsp.springcloud.controller;

import com.zsp.springcloud.entities.CommonResult;

import com.zsp.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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;

@RestController
@Slf4j
public class OrderController {
    @Autowired
    RestTemplate template;
    private static final String url="http://localhost:8001/";
    @GetMapping("/consumer/payment/create")
    public CommonResult creat(Payment payment){
        return template.postForObject(url+"/payment/create",payment,CommonResult.class);
    }
    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult creat(@PathVariable("id") Long id){
        return template.getForObject(url+"/payment/get/"+id,CommonResult.class);
    }
}

这里顺便把主启动类一起贴出来把,虽然并没有什么东西:

package com.zsp.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);
    }
}

 

还有两个实体类:回头要重构把这两个去掉


@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {
    private Integer code;
    private String message;
    private T  date;
    public CommonResult(Integer code,String message){
    this(code,message,null);
    }
}

package com.zsp.springcloud.entities;

import lombok.Data;

import java.io.Serializable;

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值