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

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、测试

posted @ 2020-08-12 08:52  hellozwx  阅读(234)  评论(0编辑  收藏  举报