企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
**1. 在父工程下构建消费端模块:cloud-consumer-dept-80** ![](https://img.kancloud.cn/6a/12/6a1229901270e4eb564ae1cd6ada7038_1455x141.jpg) ![](https://img.kancloud.cn/2a/24/2a24be50d95876bfab1d27a1a4bd1412_1475x308.jpg) **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"> <parent> <!-- 1. 子模块引入父工程进行统一版本控制 --> <artifactId>rest-cloud-parent</artifactId> <groupId>org.atguigu.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-consumer-dept-80</artifactId> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <!-- 2. 当前子模块没有写版本时,则用父工程中指定的版本--> <dependencies> <!-- 3. 引入自定义的模块,便可以在当前模块中调用自定义模块的API了,就是引入了自己的JAR包 --> <dependency> <groupId>org.atguigu.springcloud</groupId> <artifactId>cloud-api</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 修改后立即生效,热部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build> </project> ``` **3. 你会在父工程的`pom.xml`中看到引入了当前的模块** ```xml <modules> <module>cloud-api</module> <module>cloud-provider-dept-8001</module> <module>cloud-consumer-dept-80</module> </modules> ``` **4. 当前模块的`resources/application.yml`** ```xml server: port: 80 # 访问端口为80 #服务端访问地址 rest_url_prefix: http://localhost:8001 ``` **5. 在当前模块注册TestTemplate组件,方便以REST API方式调用服务端提供的服务** ```java @Configuration public class ConfigBean { /** * RestTemplate提供了多种便捷访问远程Http服务的方法, * 是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集 */ @Bean public RestTemplate getRestTemplate() { return new RestTemplate(); } } ``` **6. 在当前模块的controller层以REST API方式调用服务端提供的服务** ```java @RestController @RequestMapping("/dept") public class DeptController { /** * 服务端访问地址 */ @Value("${rest_url_prefix}") private String REST_URL_PREFIX; @Autowired private RestTemplate restTemplate; @RequestMapping(value = "/add") public boolean add(Dept dept) { //public <T> T postForObject(String url, Object request, Class<T> responseType, Object... uriVariables) //url:请求地址 //request:请求实体参数 //responseType:服务响应的数据类型 //uriVariables:请求的非实体参数 return restTemplate.postForObject(REST_URL_PREFIX + "/add", dept, Boolean.class); } @RequestMapping(value = "/get/{id}") public Dept get(@PathVariable("id") Long id) { return restTemplate.getForObject(REST_URL_PREFIX + "/get" + id, Dept.class); } @RequestMapping(value = "/list") public List<Dept> list() { return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class); } } ``` **7. 当前模块的启动类** ```java @SpringBootApplication public class DeptConsumer80App { public static void main(String[] args) { SpringApplication.run(DeptConsumer80App.class, args); } } ``` **8. 验证** 先启动 8001 服务端,然后启动 80 消费端,访问 80 消费端 http://localhost:80/consumer/dept/list ,得出如下结果则构建成功! ```json [{"deptno":1,"dname":"开发部","db_source":"clouddb01"},{"deptno":2,"dname":"人事部","db_source":"clouddb01"},{"deptno":3,"dname":"财务部","db_source":"clouddb01"},{"deptno":4,"dname":"市场部","db_source":"clouddb01"},{"deptno":5,"dname":"运维部","db_source":"clouddb01"}] ``` 当访问消费端的 http://localhost:80/consumer/dept/list 就会调用服务端的 http://localhost:8081/dept/list 服务。