很简单,因为消费者不需要再去编写底层代码只需要调用,所以只要有控制类
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;
}