创建子项目OrderService,并服务注册到Eureka。
OrderService子项目作为订单信息的服务提供方,通过本项目,可以实现对基于Docker运行的mysql数据库表的读取,并基于RestTemplate技术实现对UserService服务的远程调用。
1、在父项目上点击鼠标右键选择new→Module:
过程同上,略过不表,子项目的文件结构如下:
2、在pom.xml中引入依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
3、在application.yml中配置eureka地址:
server:
port: 8088
spring:
datasource:
url: jdbc:mysql://localhost:3306/mysql?useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
application:
name: orderservice
eureka:
client:
service-url: #eureka服务地址
defaultZone: http://127.0.0.1:10086/eureka
4、在SpringBoot启动类中添加RestTemplate并注入Spring容器:
@MapperScan("cn.it32.order.mapper")
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
/**
* 创建RestTemplate并注入Spring容器
*/
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
5、编写UserController类:
package cn.it32.order.web;
import cn.it32.order.pojo.Order;
import cn.it32.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* Created by on 2022/10/20.
*/
@RestController
@RequestMapping("order")
public class OrderController {
@Autowired
private OrderService orderService;
@GetMapping("{orderId}")
public Order queryOrderByUserId(@PathVariable("orderId") Long orderId) {
// 根据id查询订单并返回
return orderService.queryOrderById(orderId);
}
}
6、编写业务处理逻辑OrderService类:
package cn.it32.order.service;
import cn.it32.order.mapper.OrderMapper;
import cn.it32.order.pojo.Order;
import cn.it32.order.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
* Created by DYB on 2022/10/20.
*/
@Service
public class OrderService {
@Autowired
private OrderMapper orderMapper;
/* @Autowired
private UserClient userClient;
public Order queryOrderById(Long orderId) {
// 1.查询订单
Order order = orderMapper.findById(orderId);
// 2.用Feign远程调用
User user = userClient.findById(order.getUserId());
// 3.封装user到Order
order.setUser(user);
// 4.返回
return order;
}*/
@Autowired
private RestTemplate restTemplate;
public Order queryOrderById(Long id) {
// 1.查询订单
Order order = orderMapper.findById(id);
// 2.利用RestTemplate发起http请求,查询用户
// 2.1.url路径
String url = "http://userservice/user/" + order.getUser_id();
// 2.2.发送http请求,实现远程调用
User user = restTemplate.getForObject(url, User.class);
// 3.封装user到Order
order.setUser(user);
// 4.返回
return order;
}
}
7、编写数据库访问接口OrderMapper类:
package cn.it32.order.mapper;
import cn.it32.order.pojo.Order;
import org.apache.ibatis.annotations.Select;
/**
* Created by on 2022/10/20.
*/
public interface OrderMapper {
@Select("select * from tb_order where id = #{id}")
Order findById(Long id);
}
8、编写实体类POJO:
@Data
public class Order {
private Long id;
private Long price;
private String name;
private Integer num;
private Long user_id;
private User user;
}
public class User {
private Long id;
private String username;
private String address;
}
9、运行,并访问数据库:
服务请求路径为"/order",后带变量id。
从上图可以看出,orderservice本身访问到了order表,同时,也通过远程方式调到了userservice服务,取回了user表中的数据(最后三个字段)。