RestTempalte是Spring Boot封装好的Http客户端工具。
搭建父工程
- 添加服务提供者,提供Rest HTTP接口
- 添加服务调用者,通过RestTemplate访问服务提供者的接口
- 服务提供者和调用者都放到一个父工程中
为了方便项目管理,这里把服务提供者和服务调用者放到同一个工程中
父工程不需要写代码,可以将src目录删除
添加Spring Boot依赖
org.springframework.boot spring-boot-starter-parent 2.2.8.RELEASE 1.8 org.springframework.boot spring-boot-starter org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine
搭建服务提供者
1.添加提供者模块(ProviderMainApp)
添加依赖
>org.springframework.boot spring-boot-starter-web
编写配置文件
server:
port: 8001
spring:
application:
# 当前应用的服务名称(也叫服务ID)
name: user-service
jackson:
time-zone: GMT+8`
启动类
package com.itheima;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
添加实体
package com.itheima.user.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Long id;
private String name;
private Integer age;
private Date updateTime;
}
编写controller
package com.itheima.user.controller;
import com.itheima.user.entity.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController
@RequestMapping("/user")
public class UserController {
@GetMapping("/{id}")
public User getById(@PathVariable("id") Long id){
User user = new User(id,"tom",18,new Date());
return user;
}
}
启动项目,打开http://localhost:8001/user/1
消费服务者搭建
添加模块
org.springframework.boot spring-boot-starter-web
添加配置文件
server:
port: 9001
spring:
application:
# 当前应用的服务名称(也叫服务ID)
name: service-consumer
jackson:
time-zone: GMT+8
添加启动类
package com.itheima;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
添加实体
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Long id;
private String name;
private Integer age;
private Date updateTime;
}
编写controller
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
@Autowired // 注入RestTemplate模板工具
private RestTemplate restTemplate;
@GetMapping("/user/{id}")
public User getUserById(@PathVariable("id") Long id) {
// 通过远程调用user-service提供的HTTP接口
String url = "http://localhost:8001/user/" + id;
// 第一个参数是接口调用地址 第二个参数是返回的类型
User user = restTemplate.getForObject(url, User.class);
return user;
}
}
访问http://localhost:9001/consumer/user/1
概括一下就是分布式服务必然要面临的问题:
- 服务管理
- 如何自动注册和发现服务
- 如何实现服务状态监管
- 如何实现服务动态路由
- 服务如何实现负载均衡
- 服务如何解决容灾问题
- 服务如何实现统一配置
以上的问题,我们都将在Spring Cloud中得到解决。



