宽屏
1.继续上篇文章,我们开始编写服务消费者,接着上次的项目,我们新建一个module。
2.编写springcloud-consumer-dept-80的pom.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>springcloud</artifactId> <groupId>com.allen</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-consumer-dept-80</artifactId> <!-- 实体 + web --> <dependencies> <dependency> <groupId>com.allen</groupId> <artifactId>springcloud-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
3.在springcloud-consumer-dept-80的resources目录下新建编写application.yml文件,配置端口号
server: port: 80
4.新建com.allen.springcloud.config包,在本包下面新建ConfigBean类,用来注入RestTemplte。
package com.allen.springcloud.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class ConfigBean { @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } }
5.新建controller包,创建DeptConsumerController类,远程调用接口。
package com.allen.springcloud.controller; import com.allen.springcloud.pojo.Dept; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.List; @RestController public class DeptConsumerController { //理解:消费者,不应该有service层 //RestTemplate 提供我们直接调用就可以了,注册到spring中 //(url, 实体:Map , Class<T> responseType) @Autowired private RestTemplate restTemplate;//提供多种便捷访问远程http服务的方法,简单的Restful服务模板 private static final String REST_URL_PREFIX = "http://localhost:8001"; @RequestMapping("/consumer/dept/add") public boolean add(Dept dept){ return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class); } @RequestMapping("/consumer/dept/get/{id}") public Dept get(@PathVariable("id") Long id){ return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class); } @RequestMapping("/consumer/dept/list") public List<Dept> list(){ return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class); } }
6.编写启动类DeptConsumer_80
package com.allen.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DeptConsumer_80 { public static void main(String[] args) { SpringApplication.run(DeptConsumer_80.class, args); } }
7.先启动提供者springcloud-provider-dept-8001,在启动消费者springcloud-consumer-dept-80
8.测试接口