1、介绍
- 我们会使用一个Dept部门模块做一个微服务通用案例Consumer消费者(Client)通过REST调用Provider提供者(Server)提供的服务。
- 回顾Spring,SpringMVC,Mybatis等以往学习的知识。
- Maven的分包分模块架构复习。
一个父工程带着多个Moudule子模块
MicroServiceCloud父工程(Project)下初次带着3个子模块(Module)
- microservicecloud-api 【封装的整体entity/接口/公共配置等】
- microservicecloud-consumer-dept-80 【服务提供者】
- microservicecloud-provider-dept-8001 【服务消费者】
2、新建三个数据库
.
.
.
-- springcloud-db01
-- begin
CREATE DATABASE IF NOT EXISTS `springcloud-db01`;
USE `springcloud-db01`;
CREATE TABLE IF NOT EXISTS `dept` (
`deptno` int NOT NULL AUTO_INCREMENT,
`dname` varchar(50) NOT NULL DEFAULT '',
`db_source` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`deptno`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='部门表';
INSERT INTO `dept` (`deptno`, `dname`, `db_source`) VALUES
(1, '开发部', 'db01'),
(2, '财务部', 'db01'),
(3, '人事部', 'db01'),
(4, '市场部', 'db01'),
(5, '运维部', 'db01');
-- end
-- springcloud-db02
-- begin
CREATE DATABASE IF NOT EXISTS `springcloud-db02`;
USE `springcloud-db02`;
CREATE TABLE IF NOT EXISTS `dept` (
`deptno` int NOT NULL AUTO_INCREMENT,
`dname` varchar(50) NOT NULL DEFAULT '',
`db_source` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`deptno`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='部门表';
INSERT INTO `dept` (`deptno`, `dname`, `db_source`) VALUES
(1, '开发部', 'db02'),
(2, '财务部', 'db02'),
(3, '人事部', 'db02'),
(4, '市场部', 'db02'),
(5, '运维部', 'db02');
-- end
-- springcloud-db03
-- begin
CREATE DATABASE IF NOT EXISTS `springcloud-db03`;
USE `springcloud-db03`;
CREATE TABLE IF NOT EXISTS `dept` (
`deptno` int NOT NULL AUTO_INCREMENT,
`dname` varchar(50) NOT NULL DEFAULT '',
`db_source` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`deptno`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='部门表';
INSERT INTO `dept` (`deptno`, `dname`, `db_source`) VALUES
(1, '开发部', 'db03'),
(2, '财务部', 'db03'),
(3, '人事部', 'db03'),
(4, '市场部', 'db03'),
(5, '运维部', 'db03');
-- end
3、创建父工程
- 新建父工程项目springcloud,切记packaging是pom模式
- 主要是定义POM文件,将后续各个子模块公用的jar包等统一提取出来,类似一个抽象父类
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>com.godfrey</groupId>
<artifactId>spring-cloud-study</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
<spring-boot.version>2.3.5.RELEASE</spring-boot.version>
<mysql.version>8.0.22</mysql.version>
<druid.version>1.1.23</druid.version>
<mybatis.version>1.3.2</mybatis.version>
<lombok.version>1.18.16</lombok.version>
</properties>
<dependencyManagement>
<dependencies>
<!--springCloud的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--SpringBoot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--数据库-->
<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>
<!--Lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
4、新建springcloud-api模块
4.1 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>spring-cloud-study</artifactId>
<groupId>com.godfrey</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-api</artifactId>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
4.2 在com.godfrey.springcloud.pojo包下,新建实体类Dept
package com.godfrey.springcloud.pojo;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;
@Data
@NoArgsConstructor
@Accessors(chain = true)
public class Dept implements Serializable {
private Long deptno;
private String dname;
private String db_source;
public Dept(String dname) {
this.dname = dname;
}
}
5、新建服务提供者springcloud-provider-dept-8001模块
5.1 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>spring-cloud-study</artifactId>
<groupId>com.godfrey</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-provider-dept-8001</artifactId>
<dependencies>
<dependency>
<groupId>com.godfrey</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
5.2 配置文件application.yml
server:
port: 8001
# mybatis的配置
mybatis:
type-aliases-package: com.godfrey.springcloud.pojo
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
# spring的配置
spring:
application:
name: springcloud-provider-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource #数据库
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/springcloud-db01?serverTimezone=GMT%2B8
username: root
password: root123
在resources目录下Mybatis配置文件,开启耳机缓存,mybatis.config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="cacheEnabled" value="true"/>
</settings>
</configuration>
接下来,新建项目结构
.
5.3 DeptMapper
package com.godfrey.springcloud.mapper;
import com.godfrey.springcloud.pojo.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author godfrey
* @since 2020-11-16
*/
@Mapper
@Repository
public interface DeptMapper {
boolean addDept(Dept dept);
Dept queryById(Long id);
List<Dept> queryAll();
}
DeptMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.godfrey.springcloud.mapper.DeptMapper">
<insert id="addDept" parameterType="Dept">
insert into dept (deptno, dname, db_source)
values (#{dname}, DATABASE())
</insert>
<select id="queryById" resultType="com.godfrey.springcloud.pojo.Dept" parameterType="Long">
select *
from dept
where deptno = #{deptno};
</select>
<select id="queryAll" resultType="Dept">
select *
from dept;
</select>
</mapper>
5.4 DeptService
package com.godfrey.springcloud.service;
import com.godfrey.springcloud.pojo.Dept;
import java.util.List;
/**
* @author godfrey
* @since 2020-11-16
*/
public interface DeptService {
/**
* 增加部门
*
* @return boolean
*/
boolean addDept(Dept dept);
/**
* 通过id查询部门
*
* @return Dept
*/
Dept queryById(Long id);
/**
* 查询所有部门
*
* @return java.util.List<com.godfrey.springcloud.pojo.Dept>
*/
List<Dept> queryAll();
}
5.5 DeptController
package com.godfrey.springcloud.controller;
import com.godfrey.springcloud.pojo.Dept;
import com.godfrey.springcloud.service.DeptService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author godfrey
* @since 2020-11-16
*/
@RestController
public class DeptController {
private final DeptService deptService;
public DeptController(DeptService deptService) {
this.deptService = deptService;
}
@PostMapping("/dept/add")
public boolean addDept(Dept dept) {
return deptService.addDept(dept);
}
@GetMapping("/dept/get/{id}")
public Dept queryById(@PathVariable("id") Long id) {
Dept dept = deptService.queryById(id);
if (dept == null) {
throw new RuntimeException("Fail");
}
return dept;
}
@GetMapping("/dept/list")
public List<Dept> queryAll() {
return deptService.queryAll();
}
}
5.6 启动类
package com.godfrey.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DeptProvider_8001 {
public static void main(String[] args) {
SpringApplication.run(DeptProvider_8001.class, args);
}
}
6、新建消费者springcloud-consumer-dept-80模块
6.1 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>spring-cloud-study</artifactId>
<groupId>com.godfrey</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-consumer-dept-80</artifactId>
<dependencies>
<dependency>
<groupId>com.godfrey</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
6.2 配置文件application.yml
server:
port: 80
接下来,新建项目结构
.
6.3 配置类ConfigBean
package com.godfrey.springcloud.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* 配置类
*
* @author godfrey
* @since 2020-11-18
*/
@Configuration
public class ConfigBean {
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
6.4 DeptConsumerController
package com.godfrey.springcloud.controller;
import com.godfrey.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author godfrey
* @since 2020-11-18
*/
@RestController
public class DeptConsumerController {
private RestTemplate restTemplate;
private static final String REST_URL_PREFIX = "http://localhost:800";
@Autowired
DeptConsumerController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@PostMapping("/consumer/dept/add")
public boolean addDept(Dept dept) {
return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add/", dept, Boolean.class);
}
@RequestMapping("/consumer/dept/get/{id}")
public Dept queryById(@PathVariable("id") Long id) {
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
}
@RequestMapping("/consumer/dept/list")
public List<Dept> queryAll() {
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
}
}
6.5 启动类
package com.godfrey.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DeptConsumer_80 {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer_80.class, args);
}
}
7、测试
服务提供者:
服务消费者
8、集群
根据springcloud-provider-dept-8001模块,新建两个模块springcloud-provider-dept-8002和springcloud-provider-dept-8003即可,随后修改相应的配置文件和接口即可
为了使消费者轮询访问8001~3,springcloud-consumer-dept-80模块下controller修改为如下:
package com.godfrey.springcloud.controller;
import com.godfrey.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author godfrey
* @since 2020-11-18
*/
@RestController
public class DeptConsumerController {
private RestTemplate restTemplate;
private static final String REST_URL_PREFIX = "http://localhost:800";
private AtomicInteger num = new AtomicInteger(3);
@Autowired
DeptConsumerController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@PostMapping("/consumer/dept/add")
public boolean addDept(Dept dept) {
return restTemplate.postForObject(REST_URL_PREFIX + (num.getAndIncrement() % 3 + 1) + "/dept/add/", dept, Boolean.class);
}
@RequestMapping("/consumer/dept/get/{id}")
public Dept queryById(@PathVariable("id") Long id) {
return restTemplate.getForObject(REST_URL_PREFIX + (num.getAndIncrement() % 3 + 1) + "/dept/get/" + id, Dept.class);
}
@RequestMapping("/consumer/dept/list")
public List<Dept> queryAll() {
return restTemplate.getForObject(REST_URL_PREFIX + (num.getAndIncrement() % 3 + 1) + "/dept/list", List.class);
}
}
启动测试刷新浏览器,得到如下即环境搭建成功