文章目录
microservicecloud整体父工程Project
- 新建父工程microservicecloud,切记Packageing是pom模式,注意父工程一般不用模板
- 主要是定义POM文件,将后续各个子模块共用的jar包等统一提出来,类似一个抽象父类
POM
<?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.matthew.springcloud</groupId>
<artifactId>microservicecloud</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>microservicecloud-api</module>
<module>microservicecloud-provider-dept-8001</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.16.18</lombok.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.9.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
microservicecloud-api公共子模块Module
- 新建microservicecloud-api,创建完成后可以看到父工程的破灭文件发生变化,多出了module(子工程用到了模板)
POM
<?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>microservicecloud</artifactId>
<groupId>com.matthew.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>microservicecloud-api</artifactId>
<name>microservicecloud-api</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
Entity
- 新建部门Entity且配合lombok使用
package com.matthew.springcloud.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
* @Description TODO
* @Author Matthew
* @Date 2019/6/27 19:06
* @Version 1.0
*/
@AllArgsConstructor
@NoArgsConstructor
@Data
@Accessors(chain = true)
public class Dept implements Serializable { //Dept(Entity) orm mysql->Dept(table)类表关系映射 必须序列化
private Long deptno; //主键
private String dname; //部门名称
private String db_source; //来自哪个数据库,因为微服务架构可以一个服务对应一个数据库,同一个信息被存储到不同数据库中
public Dept(String dname) {
this.dname = dname;
}
}
注意:lombok在eclipse不好使,虽然视频里成功了但是我自己操作就是不好使所以我使用idea
microservicecloud-provider-dept-8001部门微服务提供者Module
- 新建microservicecloud-provider-dept-8001
POM
<?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>microservicecloud</artifactId>
<groupId>com.matthew.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>microservicecloud-provider-dept-8001</artifactId>
<name>microservicecloud-provider-dept-8001</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency><!-- 引入自己定义的api通用包,可以使用Dept部门Entity -->
<groupId>com.matthew.springcloud</groupId>
<artifactId>microservicecloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</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-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>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
YML
server:
port: 8001
mybatis:
config-location: classpath:mybatis/mybatis.xml # mybatis配置文件所在路径
type-aliases-package: com.matthew.springcloud.entities # 所有Entity别名类所在包
mapper-locations:
- classpath:mybatis/mapper/**/*.xml # mapper映射文件
spring:
application:
name: microservicecloud-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/clouddb01?serverTimezone=GMT # 数据库名称
username: root
password: root
dbcp2:
min-idle: 5 # 数据库连接池的最小维持连接数
initial-size: 5 # 初始化连接数
max-total: 5 # 最大连接数
max-wait-millis: 200 # 等待连接获取的最大超时时间
mybatis配置xml
在工程src/main/resources目录下新建mybatis文件夹后新建mybatis.xml文件
这里的位置一定要和yml配置的一样
<?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>
mysql创建部门数据库脚本
DROP DATABASE IF EXISTS cloudDB01;
CREATE DATABASE cloudDB01 CHARACTER SET UTF8;
USE cloudDB01;
CREATE TABLE dept
(
deptno BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
dname VARCHAR(60),
db_source VARCHAR(60)
);
INSERT INTO dept(dname, db_source)
VALUES ('开发部', DATABASE());
INSERT INTO dept(dname, db_source)
VALUES ('人事部', DATABASE());
INSERT INTO dept(dname, db_source)
VALUES ('财务部', DATABASE());
INSERT INTO dept(dname, db_source)
VALUES ('市场部', DATABASE());
INSERT INTO dept(dname, db_source)
VALUES ('运维部', DATABASE());
SELECT *
FROM dept;
DeotDao部门接口
package com.matthew.springcloud.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.matthew.springcloud.entities.Dept;
/**
* @Description TODO
* @Author Matthew
* @Date 2019/6/28 20:03
* @Version 1.0
*/
@Mapper
public interface DeptDao {
public boolean addDept(Dept dept);
public Dept findById(Long id);
public List<Dept> findAll();
}
Mapper
- 工程src/main/resources/mybatis目录下新建mapper文件夹后再建DeptMapper.xml
- 注意要和yml一样
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.matthew.springcloud.dao.DeptDao">
<select id="findById" resultType="com.matthew.springcloud.entities.Dept" parameterType="Long">
select deptno, dname, db_source
from dept
where deptno = #{deptno};
</select>
<select id="findAll" resultType="com.matthew.springcloud.entities.Dept">
select deptno, dname, db_source
from dept;
</select>
<insert id="addDept" parameterType="com.matthew.springcloud.entities.Dept">
INSERT INTO dept(dname, db_source)
VALUES (#{dname}, DATABASE());
</insert>
</mapper>
DeptService部门服务接口
package com.matthew.springcloud.service;
import com.matthew.springcloud.entities.Dept;
import java.util.List;
public interface DeptService {
public boolean add(Dept dept);
public Dept get(Long id);
public List<Dept> list();
}
DeptServiceImpl部门服务实现类
package com.matthew.springcloud.service.impl;
import com.matthew.springcloud.dao.DeptDao;
import com.matthew.springcloud.entities.Dept;
import com.matthew.springcloud.service.DeptService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* @Description TODO
* @Author Matthew
* @Date 2019/6/28 20:32
* @Version 1.0
*/
@Service
public class DeptServiceImpl implements DeptService {
@Resource
private DeptDao dao;
@Override
public boolean add(Dept dept) {
return dao.addDept(dept);
}
@Override
public Dept get(Long id) {
return dao.findById(id);
}
@Override
public List<Dept> list() {
return dao.findAll();
}
}
DeptController部门微服务提供者REST
package com.matthew.springcloud.controller;
import com.matthew.springcloud.entities.Dept;
import com.matthew.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @Description TODO
* @Author Matthew
* @Date 2019/6/28 20:36
* @Version 1.0
*/
@RestController
public class DeptController {
@Autowired
private DeptService service;
@PostMapping(value = "/dept/add")
public boolean add(@RequestBody Dept dept){
return service.add(dept);
}
@GetMapping(value = "/dept/get/{id}")
public Dept get(@PathVariable("id") Long id){
return service.get(id);
}
@GetMapping(value = "/dept/list")
public List<Dept> list(){
return service.list();
}
}
DeptProvider8001_App主启动类DeptProvider8001_App
package com.matthew.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Description TODO
* @Author Matthew
* @Date 2019/6/28 20:44
* @Version 1.0
*/
@SpringBootApplication
public class DeptProvider8001_App {
public static void main(String[] args) {
SpringApplication.run(DeptProvider8001_App.class, args);
}
}
测试
http://localhost:8001/dept/list
http://localhost:8001/dept/get/1
microservicecloud-consumer-dept-80部门微服务消费者Module
- 新建microservicecloud-consumer-dept-80
POM
<?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>microservicecloud</artifactId>
<groupId>com.matthew.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>microservicecloud-consumer-dept-80</artifactId>
<name>microservicecloud-consumer-dept-80</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<description>部门微服务消费者</description>
<dependencies>
<dependency><!-- 自己定义的api -->
<groupId>com.matthew.springcloud</groupId>
<artifactId>microservicecloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
YML
server:
port: 80
#设置devtools的端口号,因为服务端和消费端都用到了热部署如果不设置,会报 Unable to start LiveReload server警告
#指定端口,具体视自己情况而定,只要不重复就可以
spring:
devtools:
livereload:
port: 35730
ConfigBean
com.matthew.springcloud.cfgbeans包下ConfigBean的编写(类似spring里面的applicationContext.xml写入的注入Bean)
package com.matthew.springcloud.cfgbeans;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @Description TODO
* @Author Matthew
* @Date 2019/6/29 20:27
* @Version 1.0
*/
@Configuration
public class ConfigBean { //boot -->spring applicationContext.xml --->@Configuration ConfigBean = applicationContext.xml
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
/*@Bean
public UserService getUserService(){
return new UserServiceImpl();
}*/
//<bean id="userService" class="com.matthew.tmall.UserServiceImpl">
Controller
com.matthew.springcloud.cfgbeans包下新建DeptController——Consumer部门微服务消费者REST
RestTemplate
- 是什么
RestTemplate提供了多种便捷访问远程Http服务的方法, 是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集 - 官网地址
https://docs.spring.io/spring-framework/docs/4.3.7.RELEASE/javadocapi/org/springframework/web/client/RestTemplate.html - 使用
使用restTemplate访问restful接口非常的简单粗暴无脑。(url, requestMap, ResponseBean.class)这三个参数分别代表 REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。
package com.matthew.springcloud.controller;
import com.matthew.springcloud.entities.Dept;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.web.client.RestTemplate;
import java.util.List;
/**
* @Description TODO
* @Author Matthew
* @Date 2019/6/29 20:35
* @Version 1.0
*/
@RestController
public class DeptController_Consumer {
private static final String REST_URL_PREFIX = "http://localhost:8001";
/**
* 使用restTemplate访问restful接口非常的简单粗暴无脑。
* (url, requestMap, ResponseBean.class)这三个参数分别代表
* REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。
*/
@Autowired
private RestTemplate restTemplate;
@PostMapping("/consumer/dept/add")
public boolean add(Dept dept) {
return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
}
@GetMapping("/consumer/get/{id}")
public Dept get(@PathVariable("id") Long id){
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
}
@GetMapping("/consumer/list")
public List list(){
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
}
}
DeptConsumer80_App主启动类
package com.matthew.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Description TODO
* @Author Matthew
* @Date 2019/6/29 20:53
* @Version 1.0
*/
@SpringBootApplication
public class DeptConsumer80_App {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer80_App.class, args);
}
}
测试
http://localhost/consumer/dept/add?dname=test true
http://localhost/consumer/get/1 {“deptno”:1,“dname”:“开发部”,“db_source”:“clouddb01”}
http://localhost/consumer/list
[{"deptno":1,"dname":"开发部","db_source":"clouddb01"},{"deptno":2,"dname":"人事部","db_source":"clouddb01"},{"deptno":3,"dname":"财务部","db_source":"clouddb01"},{"deptno":4,"dname":"市场部","db_source":"clouddb01"},{"deptno":5,"dname":"运维部","db_source":"clouddb01"},{"deptno":6,"dname":"bigData2019","db_source":"clouddb01"},{"deptno":7,"dname":"test","db_source":"clouddb01"}]