首页 > 其他 > 详细

04_4-cloud-consumer-order80 微服务消费者订单Module模块

时间:2020-08-12 09:48:32      阅读:57      评论:0      收藏:0      [点我收藏+]

1、建 cloud-consumer-order80

技术分享图片

技术分享图片

2、改 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.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>

</denpendencies>
3、写 yml

技术分享图片

server:
    port: 80
4、主启动

技术分享图片


@SpringBootApplication
public class OrderMain80 {

    public static void main(String[] args) {
        SpringApplication.run(OrderMain80.class, args);
    }
}
5、业务类
  • 1、entities 包下的两个类——CommonResult、Payment
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
    
    private Long id;
    private String serial;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {

    private Integer code;
    private String message;
    private T       data;
    
    public CommonResult(Integer code, String message) {
        this(code, message, null);
    }
}
  • 2、config配置类——RestTemplate 类
    技术分享图片
@Configuration
public class ApplicationContextConfig {
    
    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}
  • 3、controller
    技术分享图片
@RestController
@Slf4j
public class OrderController {
    // 这里的 url 根据自己的来写
    public static final String PAYMENT_URL = "http://localhost:8001";
    
    @Resource
    private RestTemplate restTemplate;
    
    @GetMapping("/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment) {
        return restTemplate.postForObject(PAYMENT_URL + "/payment/create", 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);
    }
}
6、测试

技术分享图片

技术分享图片

04_4-cloud-consumer-order80 微服务消费者订单Module模块

原文:https://www.cnblogs.com/zwxo1/p/13466784.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!