栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

SpringCloud学习(二):消费者订单模块的构建

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringCloud学习(二):消费者订单模块的构建

目录

一、建module

二、修改pom.xml文件

三、编写yml文件

四、编写主启动类

五、编写业务代码

六、启动测试


一、建module

创建一个普通maven模块

二、修改pom.xml文件


    
        cloud
        com.shang.cloud
        1.0-SNAPSHOT
    
    4.0.0

    cloud-consumer-order80

    
        
        
            org.springframework.cloud
            spring-cloud-starter-zipkin
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        




        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

三、编写yml文件
server:
  port: 90

        其实这里使用80端口会更好,因为http默认访问80端口,这样消费者就不用在访问的时候加上端口号了。但是我的80端口被Nginx占用,来回禁用也挺麻烦,于是就改成了90端口。

四、编写主启动类
package com.shang.cloud;

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);
    }
}

五、编写业务代码

由于消费者只需要调用支付模块的方法,因此只需要entity和controller即可。

entity直接复用支付模块的就行了。

package com.shang.cloud.controller;

import com.shang.cloud.entities.CommonResult;
import com.shang.cloud.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 {

    public static final String PAYMENT_URL = "http://localhost:8001";

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/create")
    public CommonResult create(Payment payment){
        return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment, CommonResult.class);
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult getPayment(@PathVariable("id") Long id){
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id, CommonResult.class);
    }
}

注解配置applicationContext

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

六、启动测试

可以看到,通过消费者订单模块,我们也查到了内容。

 

数据也能成功插入 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/344120.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号