Create a new Spring Cloud project

Spring Cloud is a distributed microservice framework. Two concepts, one distributed and one microservice, as the name suggests, the name is exactly the same as its meaning.


Before learning SpringCloud, you need to understand SpringBoot first, because it is based on SpringBoot, so let's start directly.


First we need a maven parent project. The following figure SpringCloud is a parent project, order and stock are two sub-modules


 


The project structure is as above, we create a new controller layer and a new file in each module, and the following are the codes of the two controllers. And the port number, you also need to inject the Bean


 


 


 

package com.controller;

import jakarta.annotation.Resource;
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;

/**
 * @Author 陈厚德
 * @Version 2.2
 */
@RestController
@RequestMapping("/order")
public class OrderController {


    @Resource
    RestTemplate restTemplate;


    @RequestMapping("/add")
    public  String hello(){
        System.out.println("下单成功我是8082端口我要去连接8083端口");
        String forObject = restTemplate.getForObject("http://localhost:8083/stock/chd", String.class);
        System.out.println("通信成功"+forObject);
        return "hello word"+forObject;
    }


}

The next step is to write the stock, just need to correspond to the path of another module

 

 


So far, I have written and played, and the following is the running effect. We can start both modules, SpringBoot is supported, so don't worry. Next we can enter the port number. Now I can enter the port number for testing


 


 

 


At this point, our Spring Cloud is completed, mainly as the communication between two modules, I hope it can help you friends

Guess you like

Origin blog.csdn.net/weixin_69218754/article/details/131076945