1.新建springboot项目
父项目pom依赖加上
<packaging>pompackaging>
<properties>
<java.version>1.8java.version>
properties>
创建两个子项目
1.order
添加pom依赖
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
application.yml
server:
port: 8080
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;
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
RestTemplate restTemplate;
@RequestMapping("/add")
public String add(){
System.out.println("下单成功!");
String msg = restTemplate.getForObject("http://localhost:8081/stock/reduct", String.class);
return "hello world"+msg;
}
}
启动类
@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;
}
}
2.stock
添加pom依赖
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
application.yml
server:
port: 8081
controller层
@RestController
@RequestMapping("/stock")
public class StockController {
@RequestMapping("/reduct")
public String reduct(){
System.out.println("扣减库存");
return "扣减库存";
}
}
启动类
@SpringBootApplication
public class StockApplication {
public static void main(String[] args) {
SpringApplication.run(StockApplication.class,args);
}
}
访问 http://localhost:8080/order/add
出现 hello world扣减库存
思考 String msg = restTemplate.getForObject(“http://localhost:8081/stock/reduct”, String.class);
需要维护ip地址 麻烦
版本对应很重要
Spring Cloud Alibaba:2.2.5.RELEASES
pring Boot :2.3.2.RELEASE
Spring Cloud:Hoxton.SR8
修改父工程的pom.xml
<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.0modelVersion>
<modules>
<module>stockmodule>
<module>ordermodule>
modules>
<groupId>com.tian.springcloudgroupId>
<artifactId>springcloudalibabaartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>springcloudalibabaname>
<description>SpringCloud Alibabadescription>
<packaging>pompackaging>
<properties>
<java.version>1.8java.version>
<spring.cloud.alibaba.version>2.2.5.RELEASEspring.cloud.alibaba.version>
<spring.boot.version>2.3.7.RELEASEspring.boot.version>
<spring.cloud.version>Hoxton.SR8spring.cloud.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>${spring.cloud.alibaba.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>${spring.boot.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring.cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>