一、实现目标
module[user-service]和modul[order-service]通过RestTemplate进行通信
二、[user-service] 提供查询用户接口
2.1 maven配置
<?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>ac-mall-cloud</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.ac</groupId>
<artifactId>user-service</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2.2 新建application.yml配置文件,并配置启动端口8010
server:
port: 8010
spring:
application:
name: user-service
2.3 创建启动类UserApplication,并配置@SpringBootApplication注解
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class);
}
}
2.4 创建实体类User,用@Data提供get、set方法
@Data
public class User {
private String id;
private String userName;
}
2.5 创建UserDao,模拟从数据库取数据
@Repository
public class UserDao {
public User get(String id){
if("1".equals(id)){
User user = new User();
user.setId("1");
user.setUserName("AC");
return user;
}
return null;
}
}
2.6 创建IUserService接口和实现类
public interface IUserService {
User getUser(String id);
}
@Service
public class UserServiceImpl implements IUserService {
@Autowired
UserDao userDao;
public User getUser(String id) {
return userDao.get(id);
}
}
2.7 创建UserController提供服务
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
IUserService userService;
@GetMapping("/{userId}")
public User getUser(@PathVariable String userId){
return userService.getUser(userId);
}
}
2.8 启动module[user-service]
2.9 测试user接口
在浏览器中输入访问地址http://127.0.0.1:8010/users/1
三、[order-service] 下单并通过RestTemplate远程调用[user-service]用户接口
3.1maven配置
<?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>ac-mall-cloud</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.ac</groupId>
<artifactId>order-service</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
3.2 新建application.yml配置文件,并配置启动端口8020
server:
port: 8020
spring:
application:
name: order-service
3.3 创建启动类OrderApplication,并配置@SpringBootApplication注解,配置RestTemplate
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class);
}
@Bean
RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
3.4 创建实体类Order,用@Data提供get、set方法
@Data
public class Order {
private String id;
private String orderNo;
private Long priceFen;
private String img;
private Date createTime;
private String userId;
private String userName;
private String productId;
}
3.5 创建OrderDao,模拟将订单数据插入数据库
@Repository
public class OrderDao {
public void insert(Order order){
System.out.println("订单插入数据库成功");
}
}
3.6 创建与[user-service]对接的dto类UserDto,用@Data提供get、set方法
@Data
public class UserDto {
private String id;
private String userName;
}
3.7 创建IOrderService接口和实现类
public interface IOrderService {
Order makeOrder(String productId, String userId);
}
使用RestTemplate访问[user-service]接口
import com.ac.order.dao.OrderDao;
import com.ac.order.dto.UserDto;
import com.ac.order.entity.Order;
import com.ac.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.Date;
import java.util.UUID;
/**
* @author Alan Chen
* @description
* @date 2020/10/15
*/
@Service
public class OrderServiceImpl implements IOrderService {
@Autowired
OrderDao orderDao;
@Autowired
RestTemplate restTemplate;
final static String USER_SERVICE_URL="http://127.0.0.1:8010/users/{userId}";
public Order makeOrder(String productId, String userId) {
/**
* RestTemplate是java创造出来的,在java能够访问到网络资源的包是java.net.URLConnenction/Socket
* RestTemplate是对URLConnenction的封装
* apache--HttpClient 也是对URLConnenction/HttpURLConnenction的封装
* oKHttp 也封装了URLConnenction
* netty/rpc/grpc/thirt/tomcat
*/
// 1、根据用户ID调用用户服务接口数据,查询用户的名字
UserDto userDto = restTemplate.getForObject(USER_SERVICE_URL,UserDto.class,userId);
String userName=userDto.getUserName();
// 2、生成订单
Order order = new Order();
order.setId(UUID.randomUUID().toString());
order.setCreateTime(new Date());
order.setPriceFen(1600L);
order.setUserId(userId);
order.setUserName(userName);
order.setProductId(productId);
order.setOrderNo(UUID.randomUUID().toString());
// 3、保存数据库
orderDao.insert(order);
return order;
}
}
3.8 创建OrderController提供服务
@RestController
@RequestMapping("/orders")
public class OrderController {
@Autowired
IOrderService orderService;
@GetMapping("/{productId}/{userId}")
public Order saveOrder(@PathVariable String productId, @PathVariable String userId){
return orderService.makeOrder(productId,userId);
}
}
3.9 启动module[order-service]
3.10 测试order接口
在浏览器中输入访问地址http://127.0.0.1:8020/orders/1/1
成功返回订单信息,且订单信息里获取到[user-service]的用户信息了
控制台打印出订单插入数据库成功