创建SpringBoot分布式项目

 一、创建父项目

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

pom.xml添加
<packaging>pom</packaging>

 

 

二、创建子项目   order、stock

 

 

 

 

 

 

 

 

 

子项目pom.xml加上

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
<!--            <version>2.5.5</version>-->
        </dependency>
    </dependencies>

 

设置端口  application.yml

order

server:
  port: 8010

 

stock

server:
  port: 8011

 

启动项目

package com.wsm.order;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class,args);
    }

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder){
        RestTemplate restTemplate = builder.build();
        return restTemplate;
    }
}

 

 

package com.wsm.stock;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class StockApplication {

    public static void main(String[] args) {

        SpringApplication.run(StockApplication.class,args);
    }
}

 

 

package com.wsm.stock.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/stock")
public class StockController {

    @RequestMapping("/reduct")
    public String reduct(){
        System.out.println("扣减库存");
        return "bbbbbccc";
    }
}

 

 

package com.wsm.order.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.swing.*;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/add")
    public String add(){
        System.out.println("aaaaaaaaaaaaa");
        String msg = restTemplate.getForObject("http://localhost:8011/stock/reduct", String.class);
        return "hello world"+msg;
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2021-10-19 22:43  残星  阅读(407)  评论(0编辑  收藏  举报