SpringCloud Rest学习环境搭建:服务提供者
总体介绍
我们会使用一个Dept部门模块做一个微服务通用案例Consumer消费者(Client)通过REST调用Provider提供者(Server)提供的服务。
·回忆Spring,SpringMVC,MyBatis等以往学习的知识。。。
Maven的分包分模块架构复习
一个简单的Maven模块结构是这样的:
— app-parent:一个父项目(app-parent)聚合很多子项目〈app-util,app-dao,app-web.. .)
|— pom.xml
|
|— app-core
||—-pom.xml
|
|— app-web
||——pom.xml
……
一个父工程带着多个子Module子模块
MicroServiceCloud父工程(Project)下初次带着3个子模块(Module)
microservicecloud-api【封装的整体entity/接口/公共配置等】
microservicecloud-provider-dept-8001【服务提供者】
microservicecloud-consumer-dept-80【服务消费者】
动手开干!
4.2SpringCloud版本选择
大版本说明
创建父工程
- 新建父工程项目springcloud,切记Packageing是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>org.example</groupId>
<artifactId>springcloud</artifactId>
<version>1.0-SNAPSHOT</version>
<!--打包方式-->
<packaging>pom</packaging>
<name>springcloud Maven Webapp</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.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<junit.version>4.12</junit.version>
<lombok.version>1.18.12</lombok.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencyManagement>
<dependencies>
<!--springcloud的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--springboot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.4.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!--数据源druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<!--Springboot启动器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!--log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>springcloud</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_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-war-plugin</artifactId>
<version>3.2.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>
</plugins>
</pluginManagement>
</build>
</project>
创建pojo
import java.io.Serializable;
@Data
@NoArgsConstructor
@Accessors(chain = true)//链式写法
public class Dept implements Serializable {//实体类,orm 类表关系映射
private Long deptno;//主键
private String bname;//部门名称
//这个数据存在哪个数据库的字段 微服务,一个服务对应一个数据库,用一个信息可能存在不同的数据库
private String dbsource;
public Dept(String bname) {
this.bname = bname;
}
/*
链式写法
Dept dept=new Dept();
dept.setDeptNo(11).setDname('ssss')
* */
}
创建新模块springcloud-provider-dept-8001
导入依赖
<?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>springcloud</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-provider-dept-8001</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--拿到实体类,配置api module-->
<dependency>
<groupId>org.example</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</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>
<!--test-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--jetty-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<!--热部署工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
yml配置文件
server:
port: 8001
#mybatis配置
mybatis:
type-aliases-package: com.saxon.pojo
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
#spring配置
spring:
application:
name: spring-cloud-provider-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource #数据源
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db01?serverTimezone=UTC-8&useUnicode=true&characterEncoding=utf-8
username: root
password: 123456
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>
DeptDao.java
@Mapper
@Repository
public interface DeptDao {
public int addDept(Dept dept);
public Dept queryById(int id);
public List<Dept> queryAll();
}
DeptMapper.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="com.saxon.dao.DeptDao">
<resultMap id="DeptMap" type="com.saxon.pojo.Dept">
<id column="deptno" jdbcType="INTEGER" property="deptno"/>
<result column="bname" jdbcType="VARCHAR" property="bname"/>
<result column="db_source" jdbcType="VARCHAR" property="dbsource"/>
</resultMap>
<select id="queryById" parameterType="int" resultMap="DeptMap">
select * from dept where deptno = #{deptno}
</select>
<insert id="addDept" parameterType="Dept">
INSERT INTO dept(dname,db_source) VALUES (#{dname},DATABASE())
</insert>
<select id="queryAll" resultMap="DeptMap">
select * from dept
</select>
</mapper>
DeptService
public interface DeptService {
public int addDept(Dept dept);
public Dept queryById(int id);
public List<Dept> queryAll();
}
DeptServiceImpl
@Service
public class DeptServiceImpl implements DeptService{
@Autowired
private DeptDao deptDao;
@Override
public int addDept(Dept dept) {
return deptDao.addDept(dept);
}
@Override
public Dept queryById(int id) {
return deptDao.queryById(id);
}
@Override
public List<Dept> queryAll() {
return deptDao.queryAll();
}
}
DeptController
package com.saxon.controller;
import com.saxon.pojo.Dept;
import com.saxon.service.DeptService;
import com.saxon.service.DeptServiceImpl;
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 java.util.List;
@RestController // = Controller + ResponseBody
public class DeptController {
@Autowired
private DeptService deptService;
@PostMapping("/dept/add")
public int addDept(@RequestBody Dept dept){
return deptService.addDept(dept);
}
@GetMapping("/dept/get/{id}")
public Dept queryById(@PathVariable("id") int id){
return deptService.queryById(id);
}
@PostMapping("/dept/list")
public List<Dept> queryAll(){
return deptService.queryAll();
}
}
启动类
DeptProvider_8001.java
package com.saxon;
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);
}
}
创建consumer模块
新建springcloud-consumer-80模块,导入依赖
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
application.yml 配置文件
server:
port: 80
新建Config包在下面新建一个ConfigBean包注入 RestTemplate
package com.saxon.config;
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();
}
}
创建Controller包,编写DeptConsumerController类
package com.saxon.controller;
import com.saxon.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@RequestMapping("/consumer")
public class DeptConsumerController {
//理解:消费者,不应该有service层
//使用RestTemplate访问restful接口非常的简单粗暴且无脑
//(url,requestMap,ResponseBean.class) 这三个参数分别代表
//REST请求地址,请求参数,Http响应转换 被 转换成的对象类型
@Autowired
private RestTemplate restTemplate;
private static final String REST_URL_PREFIX = "http://localhost:8001";
@RequestMapping("/consumer/dept/add")
public int addDept(Dept dept){
return restTemplate.postForObject(REST_URL_PREFIX+ "/dept/add",dept,int.class);
}
@RequestMapping("/consumer/dept/get/{id}")
public Dept queryById(@PathVariable("id") int 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);
}
}
创建DeptConsumerApplication_80主启动类
@SpringBootApplication
public class DeptConsumerApplication_80 {
public static void main(String[] args) {
SpringApplication.run(DeptConsumerApplication_80.class);
}
}
访问:http://localhost/consumer/dept/list