SpringCloud 练习笔记.2 Rest环境搭建Consumer
主要是使用 RestTemplate 提供的方法来调用 provider的接口
新建 springcloud-consumer-dept-80 的子模块
<?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-ung</artifactId>
<groupId>com.ung</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-consumer-dept-80</artifactId>
<dependencies>
<dependency>
<groupId>com.ung</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>
配置文件 application.yml
server:
port: 80
配置类 ConfigBean 配置 RestTemplate
package com.ung.springcloud.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ConfigBean {
//配置RestTemplate
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
控制层接口 DeptConsumerController
package com.ung.springcloud.controller;
import com.ung.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.List;
/**
* @Author ung
* @Description: 消费者的控制器
*/
@RestController
public class DeptConsumerController {
//使用RestFul风格来调用
//提供多种便捷访问远程http服务的方法,简单的RestFul风格的模板
@Autowired
private RestTemplate restTemplate;
private static final String REST_URL_PERFIX = "http://localhost:8001";
@GetMapping("/consumer/dept/get/{id}")
public Dept getById(@PathVariable Long id) {
return restTemplate.getForObject(REST_URL_PERFIX + "/dept/queryById/" + id, Dept.class);
}
@PostMapping("/consumer/dept/add")
public Boolean add(@RequestBody Dept dept) {
return restTemplate.postForObject(REST_URL_PERFIX + "/dept/addDept", dept, Boolean.class);
}
@GetMapping("/consumer/dept/list")
public List<Dept> list() {
return restTemplate.getForObject(REST_URL_PERFIX + "/dept/list", List.class);
}
}
最后启动类 DeptConsumer_80
package com.ung.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);
}
}
启动服务
访问链接调用
http://localhost:80/consumer/dept/list 或 http://localhost/consumer/dept/list ,80端口可以省略
http://localhost:80/consumer/dept/get/8
Post请求
http://localhost:80/consumer/dept/add
{
"dname": "维修部4"
}
可以成功实现 comsumer 服务调用 provider的服务
这样服务之间就解耦合了,分布式调用可以实现
但是问题出现, 调用的地址域名 都是写死的,那就又关联性很强
private static final String REST_URL_PERFIX = "http://localhost:8001";
这个问题怎么办?
使用服务注册与发现的Eureka