简介
使用SpringBoot开发单体微服务,使用SpringCloud来协调治理各个微服务。SpringCloud为各个微服务之间提供服务注册、服务发现、配置管理、断路器、路由等功能。
搭建微服务模块
在构建项目时,一般会定义一个父工程,统一对所有的子工程进行管理,并且在父工程中定义依赖版本,提供版本仲裁。类似SpringBoot中的 spring-boot-starter-parent 工程一样。
创建父工程
创建一个工程,首先配置好 pom.xml。
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springcloudservice</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<mysql.version>8.0.25</mysql.version>
<druid.version>1.0.31</druid.version>
<mybatis.version>1.3.0</mybatis.version>
<logback.version>1.2.3</logback.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- SpringCloud相关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- SpringBoot相关 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.9.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- mysql驱动相关 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- 数据库连接池相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- mybatis相关 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- 日志相关 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- 单元测试相关 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
创建公共子模块
pom.xml如下
<?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>springcloudservice</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloudservice-api</artifactId>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
随后将该子模块 mvn clean ===> mvn install 安装到本地库,供其他模块使用。
创建服务提供者模块
pom.xml
如下:
<?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>springcloudservice</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloudservice-provider-user-8001</artifactId>
<dependencies>
<!-- 引入自定义的api包 -->
<dependency>
<groupId>org.example</groupId>
<artifactId>springcloudservice-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<!--使用内嵌的jetty-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
</dependencies>
</project>
application.properties
配置如下
server.port=8001
spring.datasource.url=jdbc:mysql://localhost/cloudDB01?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.config-location=classpath:/mybatis/mybatis.cfg.xml
# 配置type-aliases-package,我们在mapper.xml映射文件中不需要写出实体类的完整路径,只需要写出类名即可
mybatis.type-aliases-package=com.zxb.springcloud.bean
# 一定要写上 mapper-locations 配置
mybatis.mapper-locations=classpath:/mybatis/mapper/*.xml
# 如果在Spring的配置文件中指定了mybatis的配置文件位置,就不能在Spring的配置文件中写入mybatis的相关配置
#mybatis.configuration.map-underscore-to-camel-case=true
除了以上两个配置文件,还写了mybatis的配置文件,里面主要开启了缓存,日志输出以及驼峰命名适配的功能。
消费者接口如下:
package com.zxb.springcloud.controller;
import com.zxb.springcloud.bean.User;
import com.zxb.springcloud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserService userService;
@PostMapping(path = "/user/add")
public Boolean addUser(@RequestBody User user) {
return userService.addUser(user);
}
@GetMapping(path = "/user/del/{id}")
public Integer deleteUser(@PathVariable(value = "id") Integer id) {
return userService.deleteUser(id);
}
@PostMapping(path = "/user/update")
public void deleteUser(@RequestBody User user) {
userService.updateUser(user);
}
@GetMapping(path = "/user/get/{id}")
public User getUserById(@PathVariable(value = "id") Integer id) {
return userService.findById(id);
}
@GetMapping(path = "/user/get")
public List<User> getUserById() {
return userService.findAll();
}
}
创建消费者模块
创建好服务提供者模块,接下来要创建消费者模块了。
pom.xml
如下
<?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>springcloudservice</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloudservice-provider-user-8001</artifactId>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>springcloudservice-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<!--使用内嵌的jetty-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>
application.properties
这里暂时只需要先设置端口号即可
server.port=80
这里,因为不再是单体架构,牵扯到了多个微服务模块之间的调用。我们可以使用 RestTemplate
来进行调用实现。使用restTemplate访问restful接口非常的简单粗暴无脑。(url, requestMap, ResponseBean.class)这三个参数分别代表REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。
注入RestTemplate实例:
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
在消费者的接口中,可以这么写:
package com.zxb.springcloud.controller;
import com.zxb.springcloud.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
public class UserController_Consumer {
private static final String REST_URL = "http://localhost:8001";
@Autowired
private RestTemplate restTemplate;
@GetMapping(path = "/user/get/{id}")
public User getById(@PathVariable(value = "id") Integer id) {
return restTemplate.getForObject(REST_URL + "/user/get/" + id, User.class);
}
@PostMapping(path = "/user/add")
public boolean addUser(User user) {
return restTemplate.postForObject(REST_URL + "/user/add", user, Boolean.class);
}
@GetMapping(path = "/user/get")
public List<User> getAll() {
return restTemplate.getForObject(REST_URL + "/user/get", List.class);
}
@DeleteMapping(path = "/user/del/{id}")
public Integer deleteUserById(@PathVariable(value = "id") Integer id) {
return restTemplate.getForObject(REST_URL + "/user/del" + id, Integer.class);
}
}
测试
先服务提供者自测:发送 http://localhost:8001/user/get 请求
随后消费者调用服务提供者:发送 http://localhost/user/get 请求。
至此,微服务模块基本搭建完毕,后面使用 SpringCloud 来填充我们的项目。