微服务场景模拟
首先,我们需要模拟一个服务调用的场景,搭建两个工程:learn-service-provider(服务提供方)和learn-service-consumer(服务调用方)。方便后面学习微服务架构
服务提供方:使用mybatis操作数据库,实现对数据的增删改查;并对外提供rest接口服务。
服务消费方:使用restTemplate远程调用服务提供方的rest接口服务,获取数据。
服务提供者
我们新建一个项目:learn-service-provider,对外提供根据id查询用户的服务。
Spring脚手架创建工程
借助于Spring提供的快速搭建工具:
next-->填写项目信息:
next --> 添加web依赖:
添加mybatis依赖:
Next --> 填写项目位置:
生成的项目结构,已经包含了引导类(ServiceProviderApplication):
依赖也已经全部自动引入:
<?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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent>
<groupId>com.learn.service</groupId>
<artifactId>learn-service-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--jdbc的启动器,默认使用HikariCP连接池-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--不要忘记数据库驱动,因为springboot不知道我们使用的什么数据库,这里选择mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 通用mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
当然,因为要使用通用mapper,所以我们需要手动加一条依赖:
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.4</version>
</dependency>
非常快捷啊!
编写代码
配置
属性文件,这里我们采用了yaml语法,而不是properties:
# 映射端口
server.port=8081
# 设置org.springframework包的日志级别为debug
#logging.level.org.springframework=debug
# 连接四大参数
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisdb
spring.datasource.username=root
spring.datasource.password=123456
# 可省略,SpringBoot自动推断
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.hikari.idle-timeout=60000
spring.datasource.hikari.maximum-pool-size=30
spring.datasource.hikari.minimum-idle=10
# mybatis 别名扫描
mybatis.type-aliases-package=cn.learn.service.pojo
# mapper.xml文件位置,如果没有映射文件,请注释掉
#mybatis.mapper-locations=classpath:mybatis/mappers/**/*.xml
实体类
package com.learn.service.pojo;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String sex;
private String address;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
}
UserMapper
package com.learn.service.mapper;
import com.learn.service.pojo.User;
import org.apache.ibatis.annotations.Mapper;
//@Mapper
public interface UserMapper extends tk.mybatis.mapper.common.Mapper<User>{
}
UserService
package com.learn.service.service;
import com.learn.service.mapper.UserMapper;
import com.learn.service.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User queryById(Long id){
try {
User user = this.userMapper.selectByPrimaryKey(id);
return user;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
UserController
添加一个对外查询的接口:
package com.learn.service.contoller;
import com.learn.service.pojo.User;
import com.learn.service.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.List;
@RestController
//@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("{id}")
public User queryUserById(@PathVariable("id")Long id){
User user = this.userService.queryById(id);
return user;
}
}
启动并测试
启动项目,访问接口:http://localhost:8081/user/1
package com.learn.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("com.learn.service.mapper")
public class ServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceProviderApplication.class, args);
}
}