catalogue
2. Use ieda to build the project
2.3 two subprojects call each other
1. Operating environment
(1)jdk 1.8+
(2)maven 3.2.x+
2. Use ieda to build the project
2.1 create parent project
(1) First, create a parent project to manage sub projects and provide dependencies for sub projects
① File - > New - > module opens the create panel
② maven or spring initializer can be selected. We choose spring initializer
③ Fill in the Group and Artifact information of the project, select packaging, only jar and war, and then rewrite pom The packaging in XML is pom.
④ Select a parent version that spring boot depends on
⑤ Overwrite the project name and select a workspace
(2) Modify parent project configuration information
① Modify pom The packaging method of the XML parent class. When it is modified to pom, the parent project will not be packaged when the mvn packaging command is run
(3) introduce the version of jar package
① Check the corresponding version problems to be introduced in spring cloud Alibaba through the github address. Address: https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
② version correspondence. When we introduce the version corresponding to Spring cloud Alibaba Version, the component version is also introduced.
(4) Select the imported version. After spring cloud alibaba is imported, the corresponding component version will also be imported
Spring cloud alibaba: 2.2.6.RELEASE
Spring Cloud Version: Spring Cloud Hoxton.SR9
Spring Boot Version: 2.3.3.RELEASE
(5) In POM jar is introduced into XML. Because spring boot needs to be introduced in the way of parent project, spring cloud Alibaba also needs to be introduced in the way of parent, but a POM There can only be one parent in XML, so you need to use dependency management to import; Parent can prepare the entry for introducing the company's own projects in the future.
pom.xml configuration information to manage the imported version by configuring the version number
<properties> <java.version>1.8</java.version> <spring.boot.version>2.3.3.RELEASE</spring.boot.version> <spring.cloud.alibaba.version>2.2.6.RELEASE</spring.cloud.alibaba.version> <spring.cloud.version>Hoxton.SR9</spring.cloud.version> </properties>
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.qingyun</groupId> <artifactId>springcloudalibaba</artifactId> <version>0.0.1-SNAPSHOT</version> <modules> <module>Order</module> <module>stock</module> </modules> <name>springcloudalibaba</name> <!--Packaging type in parent project is pom--> <packaging>pom</packaging> <description>Demo project for Spring Cloud</description> <properties> <java.version>1.8</java.version> <spring.boot.version>2.3.3.RELEASE</spring.boot.version> <spring.cloud.alibaba.version>2.2.6.RELEASE</spring.cloud.alibaba.version> <spring.cloud.version>Hoxton.SR9</spring.cloud.version> </properties> <dependencies> <!--spring boot Start dependent package--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <!--use dependencyManagement To manage version numbers and subproject dependencies parent After, you do not need to add a version number to achieve the same control of the version number--> <dependencyManagement> <dependencies> <!--spring boot rely on jar--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>${spring.boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!--spring cloud alibaba rely on jar--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring.cloud.alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!--spring cloud rely on jar--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring.cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.2 creating subproject works
(1) Create a maven project and select the parent project - > New - > module
(2) Use maven to create a project, and check the parent project
(3) create a subclass item under the parent item, and the POM of the subclass item Parent parent class item is introduced in XML by default
(4) In the same way, create a sub project stock
2.3 two subprojects call each other
(1) POM of two subprojects Introducing jar into XML
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springcloudalibaba</artifactId> <groupId>com.qingyun</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>Order</artifactId> <dependencies> <!--It inherits the parent project and does not need to add a version number--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
(2) Add spring boot startup class to Order item Order
/** * Program startup class */ @SpringBootApplication public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class,args); } }
(3) Inventory item Stock add spring boot startup class
@SpringBootApplication public class StockApplication { public static void main(String[] args) { SpringApplication.run(StockApplication.class,args); } }
(4) the Order item is written to the business logic of the controller layer
@Controller @RequestMapping("/order") public class OrderController { @RequestMapping("/add") public String add(){ System.out.println("checkout success "); return "add order "; } }
(5) Inventory item Stock write Controller layer business logic
@RestController @RequestMapping("/stock") public class StockController { @RequestMapping("/reduct") public String reduct(){ System.out.println("Deduct inventory"); return "Inventory deduction succeeded"; } }
(6) The Order system calls the inventory system Stock using RestTemplate. When the Order program starts, use Bean to create RestTemplate and put it into the spring container
/** * Program startup class */ @SpringBootApplication public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class,args); } //Create RestTemplate when program starts @Bean public RestTemplate restTemplate(RestTemplateBuilder builder){ RestTemplate build = builder.build(); return build; } }
(7) The two projects create application. In the resources directory respectively Properties configuration file, configured as different service ports
(8) In the Order business layer, use RestTemplate to call the business of the inventory module
@RestController @RequestMapping("/order") public class OrderController { //Inject RestTemplate @Autowired RestTemplate restTemplate; @RequestMapping("/add") public String add(){ System.out.println("checkout success "); String forObject = restTemplate.getForObject("http://localhost:8083/stock/reduct", String.class); return "add order "+forObject; } }
(9) Start two spring boot projects, and the browser accesses the background control layer