SpringCloud笔记二:搭建项目基础框架
搭建框架
我们的SpringCloud微服务框架是父子工程,有一个父工程,剩下的都是子工程,我们可以新建一个公用Bean的子工程叫api,其他的子工程用到相同的Bean的时候调用api项目的Bean就可以了,实现了公用。
新建父工程
打开idea,新建一个Maven项目,如图:
后面的就不截图了,起个名字就可以了。
新建子工程api
我们新建一个子工程api,这个api子工程目的是,公用一些Bean
在我们的项目上右键,new,Module,然后选择SpringBoot吧,我喜欢SpringBoot
后面的名字看着起,需要的工具自己看着加,这个api我们需要Lombok就够了
项目新建完成之后,我们新建一个Bean,用于和数据库映射,其他的子项目也会公用这个Bean
自己新建,我的DeptEntity的Bean使用了Lombok,代码如下
package com.vae.springcloud.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;
@AllArgsConstructor //全参数的构造函数
@NoArgsConstructor //无参数的构造函数
@Data //get和set方法
@Accessors(chain = true) //链式访问,使用链式创建的set方法有返回值
@SuppressWarnings("serial") //忽略黄色警告
public class DeptEntity implements Serializable {
private Integer did;//部门id,主键
private String dname;//部门名称
private String dbsource;//来自哪个数据库的标识列
}
接下来就是要把这个Bean和数据库进行映射了,你可以使用JPA进行映射,也可以自己去手动创建,我把我的数据库贴出来,你可以复制使用
#数据库是shuyunquan,下面是表数据以及结构
create table deptentity
(
did int ,primary key,not null,
dname varchar(10) null,
dbsource varchar(30) null,
constraint deptentity_did_uindex
unique (did)
);
INSERT INTO shuyunquan.deptentity (did, dname, dbsource) VALUES (1, '软件部', database());
INSERT INTO shuyunquan.deptentity (did, dname, dbsource) VALUES (2, '研发部', database());
INSERT INTO shuyunquan.deptentity (did, dname, dbsource) VALUES (3, '工程部', database());
INSERT INTO shuyunquan.deptentity (did, dname, dbsource) VALUES (4, '市场部', database());
新建子工程提供者provider
其实,我们已经搭建好框架了,现在和以后的新建项目都和子工程一样,剩下的,就是新技术的学习了,我们先来一个提供者provider子项目,主要用于提供对部门的增删改查操作接口
新建provider,还是一样的,父工程右键new,Module
还是选择SpringBoot项目。我先把项目贴出个图出来,让你们知道怎么新建,位置
我们新建dao接口,去和Mybatis进行数据操作
package com.vae.springcloud.dao;
import com.vae.springcloud.entity.DeptEntity;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface DeptDAO {
public boolean addDept(DeptEntity deptEntity);
public DeptEntity findById(Integer id);
public List<DeptEntity> findAll();
public boolean deleteDept(Integer id);
public void updateDept(DeptEntity deptEntity);
}
有了dao,就要有Mybatis文件了
<?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>
<!--别名-->
<typeAliases>
<typeAlias type="com.vae.springcloud.entity.DeptEntity" alias="DeptEntity"></typeAlias>
</typeAliases>
</configuration>
这里需要注意的是我的mapper.xml没有写在Mybatis.xml里面,不要写,写了报错
下面是我的mapper的xml
<?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 -->
<mapper namespace="com.vae.springcloud.dao.DeptDAO">
<!--
数据库查询数据
insert、select、delete、update:sql语句类型
id:sql语句唯一标识
resultType:结果返回值类型-包名+类名 或 基本数据类型
parameterType:匹配字段值-包名+类名 或 基本数据类型
-->
<select id="findAll" resultType="DeptEntity">
select did,dname,dbsource from deptentity
</select>
<select id="findById" parameterType="DeptEntity" resultType="Long">
select did,dname,dbsource from deptentity where did=#{did}
</select>
<insert id="addDept" parameterType="DeptEntity">
insert into deptentity values(#{did},#{dname},database())
</insert>
<delete id="deleteDept" parameterType="Integer">
delete from deptentity where did = #{did}
</delete>
<update id="updateDept" parameterType="DeptEntity">
update deptentity set did=#{did},dname=#{dname},dbsource=#{dbsource} where did =#{did};
</update>
</mapper>
Mybatis文件和与dao对应的mapper.xml也有了,下面就是yml了
server:
port: 8001
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml #mybatis配置文件所在路径
type-aliases-package: com.vae.springcloud.entity #所有Entity别名类所在包
mapper-locations: classpath:mybatis/mapper/**/*.xml #mapper映射文件
spring:
application:
name: provider-dept
datasource:
# type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/shuyunquan?serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123
dbcp2:
min-idle: 5 # 数据库连接池的最小维持连接数
initial-size: 5 # 初始化连接数
max-total: 5 # 最大连接数
max-wait-millis: 200 # 等待连接获取的最大超时时间
我的提供者端口号是8001,还有Mybatis配置写了xml的所在位置,这个就是为什么我的Mybatis.xml里面没有写mapper.xml的原因,以后都这样写吧,就使用这种方式了。
现在dao有了,Mybatis配置文件有了,yml也有了,现在和dao对应的service接口新建一下
讲真,我现在还不是很清楚为什么有dao了还要有个对应的service,说的一个和数据沟通,一个和业务沟通,反正我现在理解的还不是很透彻。
package com.vae.springcloud.service;
import com.vae.springcloud.entity.DeptEntity;
import java.util.List;
public interface DeptService {
public boolean addDept(DeptEntity deptEntity);
public DeptEntity findById(Integer id);
public List<DeptEntity> findAll();
public boolean deleteDept(Integer id);
public void updateDept(DeptEntity deptEntity);
}
看看service接口的实现类
package com.vae.springcloud.service.impl;
import com.vae.springcloud.dao.DeptDAO;
import com.vae.springcloud.entity.DeptEntity;
import com.vae.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptDAO deptDAO;
@Override
public boolean addDept(DeptEntity deptEntity) {
return deptDAO.addDept(deptEntity);
}
@Override
public DeptEntity findById(Integer id) {
return deptDAO.findById(id);
}
@Override
public List<DeptEntity> findAll() {
return deptDAO.findAll();
}
@Override
public boolean deleteDept(Integer id) {
return deptDAO.deleteDept(id);
}
@Override
public void updateDept(DeptEntity deptEntity) {
deptDAO.updateDept(deptEntity);
}
}
最后,新建一个Controller
package com.vae.springcloud.controller;
import com.vae.springcloud.entity.DeptEntity;
import com.vae.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@PostMapping(value = "/dept/add")
public boolean add(@RequestBody DeptEntity deptEntity){
return deptService.addDept(deptEntity);
}
@GetMapping(value = "/dept/get/{id}")
public DeptEntity get(@PathVariable("id") Integer id){
return deptService.findById(id);
}
@GetMapping(value = "/dept/list")
public List<DeptEntity> get() throws Exception{
return deptService.findAll();
}
@GetMapping(value = "/dept/delete/{id}")
public boolean delete(@PathVariable("id") Integer id){
return deptService.deleteDept(id);
}
@GetMapping(value = "/dept/update")
public void update(@RequestBody DeptEntity deptEntity){
deptService.updateDept(deptEntity);
}
}
至此,提供者provider已经创建完了,运行项目,输入:http://localhost:8001/dept/list
可以出来部门列表就表明成功了
忘了说一件事,我贴出来我的provider的Maven的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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.vae.springcloud</groupId>
<artifactId>provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>provider</name>
<description>provider project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>com.vae.springcloud</groupId>
<artifactId>api</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
这里面的这个
<dependency>
<groupId>com.vae.springcloud</groupId>
<artifactId>api</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
就是我们的provider子项目调用了api子项目的Bean,版本号使用了${project.version},这样api版本升级了也会自动的变的,idea可以自动加这些内容的
新建消费者consumer
有了提供者提供接口,我们消费者只需要调用访问接口就可以了,其他的不需要
新建项目还是一样的,module,SpringBoot。我不讲了,直接贴结构
先贴yml配置
server:
port: 80
新建一个ConfigBean
package com.vae.springcloud.cfgbeans;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ConfigBean {
@Bean
public RestTemplate getRestTemplate()
{
return new RestTemplate();
}
}
这里面主要是使用了RestTemplate,我们将使用这个对Rest风格的接口的一个访问操作
新建Controller
package com.vae.springcloud.controller;
import com.vae.springcloud.entity.DeptEntity;
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 DeptController_Consumer {
private static final String REST_URL_PREFIX="http://localhost:8001";
@Autowired
private RestTemplate restTemplate;
@PostMapping(value = "/consumer/dept/add")
public boolean add(DeptEntity deptEntity){
return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add",deptEntity,Boolean.class);
}
@GetMapping(value = "/consumer/dept/get/{id}")
public DeptEntity get(@PathVariable("id") Integer id){
return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get"+id,DeptEntity.class);
}
@SuppressWarnings("unchecked")
@GetMapping(value = "/consumer/dept/list")
public List<DeptEntity> list() throws Exception{
return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list",List.class);
}
@GetMapping(value = "/consumer/dept/delete/{id}")
public boolean delete(@PathVariable("id") Integer id){
return restTemplate.getForObject(REST_URL_PREFIX+"/dept/delete"+id,Boolean.class);
}
@GetMapping(value = "/consumer/dept/update")
public void update(@RequestBody DeptEntity deptEntity){
restTemplate.postForObject(REST_URL_PREFIX+"/dept/update",deptEntity,Boolean.class);
}
}
最后,贴出我的Maven的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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.vae.springcloud</groupId>
<artifactId>consumer-80</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consumer-80</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Ribbon相关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.vae.springcloud</groupId>
<artifactId>api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-loadbalancer</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-loadbalancer</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.0.4.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13-beta-1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.8.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
保持provider项目的运行,我们启动consumer项目,输入http://localhost/consumer/dept/list
出现部门信息,就表明成功了
总结
这一章,使用了父子工程,新建了api,provider,consumer三个子工程,并且实现了provider和consumer的数据接口提供和访问。有了这个基础,我们开始正式进军微服务的大门了,下一章讲解Eureka