案例代码:https://gitee.com/flymini/cloud/tree/master/rest-cloud
****
[TOC]
# 1. 项目结构
```
|—— rest-cloud 项目名
| |—— rest-0parent 父工程
| |—— rest-consumer-dept8002 消费端
| |—— rest-server-dept8000 服务端
```
![](https://img.kancloud.cn/88/eb/88eb21d99929772112169b3570186c61_1598x279.png)
下面实现消费端通过 REST 的方式调用服务端的接口。
<br/>
# 2. 创建父工程
父工程没什么好说的,就一个`pom.xml`文件。
```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">
<groupId>accu.note.record</groupId>
<artifactId>rest-0parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging> <!-- 1. 必须指定为为pom -->
<modelVersion>4.0.0</modelVersion>
<!-- 2. 引入子模块 -->
<modules>
<module>../rest-server-dept8000</module>
<module>../rest-consumer-dept8002</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<cloud.version>2022.0.3</cloud.version>
<boot.version>3.1.0</boot.version>
<lombok.version>1.18.26</lombok.version>
</properties>
<!-- 3. 统一对子模块用到的依赖进行版本控制,但不会引入jar包 -->
<dependencyManagement>
<dependencies>
<!-- Spring Cloud 版本控制坐标 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Spring Boot 版本控制坐标-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 父工程引入,则子模块也会被引入 -->
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>
</project>
```
<br/>
# 3. 创建服务端
**1. mave坐标**
```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">
<!-- 1. 子模块引入父工程进行统一版本控制 -->
<parent>
<groupId>accu.note.record</groupId>
<artifactId>rest-0parent</artifactId>
<version>1.0</version>
<relativePath>../rest-0parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>rest-server-dept8000</artifactId>
<dependencies>
<!-- 可以看到我并没有指定版本<version>也没有问题,那是因为: -->
<!-- 1. 当子模块没有指定版本时,以父工程指定的版本为准 -->
<!-- 2. 当子模块指定版本时,以子模块指定的版本为准 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--<version>3.1.0</version>-->
</dependency>
</dependencies>
</project>
```
**2. 配置`application.yml`**
```yml
server:
port: 8000
```
**3. 创建一个服务接口**
```java
@RestController
public class DemoController {
@GetMapping("/demo")
public String getDemo() {
return "[rest-server-dept8000|demo]";
}
}
```
**4. 启动类**
```java
@SpringBootApplication
public class Dept8000Application {
public static void main(String[] args) {
SpringApplication.run(Dept8000Application.class, args);
}
}
```
<br/>
# 4. 创建消费端
**1. mave坐标**
```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>
<groupId>accu.note.record</groupId>
<artifactId>rest-0parent</artifactId>
<version>1.0</version>
<relativePath>../rest-0parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>rest-consumer-dept8002</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
```
**2. 配置`application.yml`**
```yml
server:
port: 8002
```
**3. 注册TestTemplate**
```java
@Configuration
public class BeanConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
```
**4. 创建controller**
```java
@RestController
@RequiredArgsConstructor
public class ConsumeController {
final RestTemplate restTemplate;
@GetMapping("/demo")
public String getDemo() {
//通过RestTemplate调用服务端服务
return restTemplate.getForObject("http://127.0.0.1:8000/demo", String.class);
}
}
```
**5. 启动类**
```java
@SpringBootApplication
public class Dept8002Application {
public static void main(String[] args) {
SpringApplication.run(Dept8002Application.class, args);
}
}
```
**6. 验证消费端能否访问到服务端**
1. 启动服务端
2. 启动消费端
3. 访问消费端:http://127.0.0.1:8002/demo ,输出如下,说明访问到服务端了。
![](https://img.kancloud.cn/cf/25/cf25d838dbc1d6b78e272b64f6fe8cca_1963x190.png)