SpringCloud(2) - Rest学习环境搭建
2021-04-09 17:28
标签:utc odi obj 简单的 sch tar lombok 格林威治 yaml 使用一个 Dept 部门模块做一个微服务通用案例 Consumer消费者(Client)通过 REST 调用 Provider 提供者(Server)提供的服务。 使用最后这两个 ● 新建父工程项目 microservicecloud, 切记 packaging 是 pom 模式 ● 主要是定义 POM 文件,将后续各个子模块公用的 jar 包等统一提取出来,类似一个抽象父类 1)创建一个普通的maven项目 2)删除src目录 3)在pom.xml中修改打包方式为pom 4)导入依赖 数据库:
1)新建子模块(普通的maven) 2)在该模块中导入需要的依赖 3)新建实体类 1)新建子模块(普通的maven模块)服务提供者,springcloud-provider-dept-8001 2)在该模块中导入需要的依赖 3)添加编写 application.yaml 配置 4)创建 mapper 接口:DeptMapper 5)编写对应的DeptMapper.xml 6)创建 service 接口:DeptSerivce 7)添加 DeptServiceImpl 实现类 8)创建 Controller 接口:用来提供RestFul服务 9)添加启动类 DeptProvider_8001 1)新建子模块(普通的maven模块)服务消费者:springcloud-consumer-dept-80 2)在该模块中导入需要的依赖 3)创建 application.yaml 配置端口号 4)写一个配置类来将 RestTemplate 放入 IOC 容器中 5)写一个 Controller 接口 DeptConsumerController 来给前端提供访问 6)编写主启动类 DeptConsumer_80 这样,服务消费者通过 RestTemplate 就可以发起请求获取到远程的服务了 SpringCloud(2) - Rest学习环境搭建 标签:utc odi obj 简单的 sch tar lombok 格林威治 yaml 原文地址:https://www.cnblogs.com/Dm920/p/13174613.html一、总体介绍
一个父工程中创建多个 Module 子模块:MicroServiceCloud父工程(Project)下初次带着三个子模块(Module)
- microservicecloud-api【封装的整体 entity / 接口 / 公共配置等】
- microservicecloud-provider-dept-8001【服务提供者】
- microservicecloud-consumer-dept-80 【服务消费者】
二、SpringCloud版本选择
1)大版本说明
Spring Boot
Spring Cloud
关系
1.2.x
Angle版本(天使)
兼容Spring Boot 1.2.x
1.3.x
Brixton版本(布里克斯顿)
兼容Spring Boot 1.3.x,也兼容Spring Boot 1.4.x
1.4.x
Camden版本(卡姆登)
兼容Spring Boot 1.4.x,也兼容Spring Boot 1.5.x
1.5.x
Dalston版本(多尔斯顿)
兼容Spring Boot 1.5.x,不兼容Spring Boot 2.0.x
1.5.x
Edgware版本(埃奇韦尔)
兼容Spring Boot 1.5.x,不兼容Spring Boot 2.0.x
2.0.x
Finchley版本(芬奇利)
兼容Spring Boot 2.0.x,不兼容Spring Boot 1.5.x
2.1.x
Greenwich版本(格林威治)
2)实际开发版本关系
spring-boot-starter-parent
spring-cloud-dependencies
版本号
发布日期
版本号
发布日期
1.5.2.RELEASE
2017年3月
Dalston.RC1
2017年
1.5.9.RELEASE
Nov,2017
Edgware.RELEASE
Nov,2017
1.5.16.RELEASE
Sep,2018
Edgware.SR5
Oct,2018
1.5.20.RELEASE
Apr,2019
Edgware.SR5
Oct,2018
2.0.2.RELEASE
May,2018
Finchley.BUILD-SNAPSHOT
2018年
2.0.6.RELEASE
Oct,2018
Finchley.SR2
Oct,2018
2.1.4.RELEASE
Apr,2019
Greenwich.SR1
Mar,2019
三、创建父工程 - springcloud
packaging>pompackaging>
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.0modelVersion>
groupId>com.cyangroupId>
artifactId>springcloudartifactId>
version>1.0-SNAPSHOTversion>
packaging>pompackaging>
properties>
project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
maven.compiler.source>1.8maven.compiler.source>
maven.compiler.target>1.8maven.compiler.target>
junit.version>4.12junit.version>
log4j.version>1.2.17log4j.version>
lombok.version>1.16.18lombok.version>
properties>
dependencyManagement>
dependencies>
dependency>
groupId>org.springframework.cloudgroupId>
artifactId>spring-cloud-dependenciesartifactId>
version>Greenwich.SR1version>
type>pomtype>
scope>importscope>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-dependenciesartifactId>
version>2.1.4.RELEASEversion>
type>pomtype>
scope>importscope>
dependency>
dependency>
groupId>mysqlgroupId>
artifactId>mysql-connector-javaartifactId>
version>5.1.47version>
dependency>
dependency>
groupId>com.alibabagroupId>
artifactId>druidartifactId>
version>1.1.10version>
dependency>
dependency>
groupId>org.mybatis.spring.bootgroupId>
artifactId>mybatis-spring-boot-starterartifactId>
version>1.3.2version>
dependency>
dependency>
groupId>ch.qos.logbackgroupId>
artifactId>logback-coreartifactId>
version>1.2.3version>
dependency>
dependency>
groupId>junitgroupId>
artifactId>junitartifactId>
version>${junit.version}version>
dependency>
dependency>
groupId>log4jgroupId>
artifactId>log4jartifactId>
version>${log4j.version}version>
dependency>
dependency>
groupId>org.projectlombokgroupId>
artifactId>lombokartifactId>
version>${lombok.version}version>
dependency>
dependencies>
dependencyManagement>
project>
四、实体类模块 - springcloud-api
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>springcloudartifactId>
groupId>com.cyangroupId>
version>1.0-SNAPSHOTversion>
parent>
modelVersion>4.0.0modelVersion>
artifactId>springcloud-apiartifactId>
dependencies>
dependency>
groupId>org.projectlombokgroupId>
artifactId>lombokartifactId>
dependency>
dependencies>
project>
@Data
@NoArgsConstructor
@Accessors(chain = true) // 链式写法
public class Dept implements Serializable { // Dept实体诶 ORM 类表映射关系
// 主键
private Long deptno;
private String dname;
// 当前数据库,微服务 一个服务对应一个数据库,同一个信息 可能存在不同的数据库
private String db_source;
public Dept(String dname) {
this.dname = dname;
}
}
五、服务提供者模块 - springcloud-provider-dept
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>springcloudartifactId>
groupId>com.cyangroupId>
version>1.0-SNAPSHOTversion>
parent>
modelVersion>4.0.0modelVersion>
artifactId>springcloud-provider-dept-8001artifactId>
dependencies>
dependency>
groupId>cn.codeweigroupId>
artifactId>springcloud-apiartifactId>
version>1.0-SNAPSHOTversion>
dependency>
dependency>
groupId>mysqlgroupId>
artifactId>mysql-connector-javaartifactId>
dependency>
dependency>
groupId>com.alibabagroupId>
artifactId>druidartifactId>
dependency>
dependency>
groupId>org.mybatis.spring.bootgroupId>
artifactId>mybatis-spring-boot-starterartifactId>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-webartifactId>
dependency>
dependency>
groupId>junitgroupId>
artifactId>junitartifactId>
scope>testscope>
dependency>
dependency>
groupId>ch.qos.logbackgroupId>
artifactId>logback-coreartifactId>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-testartifactId>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-devtoolsartifactId>
dependency>
dependencies>
project>
server:
port: 8001
# mybatis配置
mybatis:
type-aliases-package: com.cyan.pojo
mapper-locations: classpath:mapper/*.xml
configuration: # 二级缓存
cache-enabled: true
# 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?useSSL=false&serverTimezone=UTC
username: root
password: 123456
@Mapper
@Repository
public interface DeptDao {
public boolean addDept(Dept dept);
public Dept queryById(@Param("deptno") Long deptno);
public List
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.cyan.dao.DeptDao">
insert id="addDept" parameterType="Boolean">
INSERT INTO dept(dname,db_source)
VALUES (#{dname,jdbcType=VARCHAR},DATABASE())
insert>
select id="queryById" parameterType="Long" resultType="Dept">
SELECT * FROM dept WHERE deptno = #{deptno};
select>
select id="queryAll" resultType="Dept">
SELECT * FROM dept;
select>
mapper>
public interface DeptService {
public boolean addDept(Dept dept);
public Dept queryById(Long deptno);
public List
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
public DeptDao deptDao8001;
@Override
public boolean addDept(Dept dept) {
return deptDao8001.addDept(dept);
}
@Override
public Dept queryById(Long deptno) {
return deptDao8001.queryById(deptno);
}
@Override
public List
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@PostMapping("/dept/add")
public boolean addDept(Dept dept) {
return deptService.addDept(dept);
}
@GetMapping("/dept/get/{id}")
public Dept queryDept(@PathVariable("id") Long deptno) {
return deptService.queryById(deptno);
}
@GetMapping("/dept/list")
public List
@SpringBootApplication
public class DeptProvider_8001 {
public static void main(String[] args) {
SpringApplication.run(DeptProvider_8001.class, args);
}
}
六、服务消费者模块 - springcloud-consumer-dept
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>springcloudartifactId>
groupId>com.cyangroupId>
version>1.0-SNAPSHOTversion>
parent>
modelVersion>4.0.0modelVersion>
artifactId>springcloud-consumer-deptartifactId>
dependencies>
dependency>
groupId>com.cyangroupId>
artifactId>springcloud-apiartifactId>
version>1.0-SNAPSHOTversion>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-webartifactId>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-devtoolsartifactId>
dependency>
dependencies>
project>
server:
port: 80
@Configuration
public class ConfigBean { // 相当于spring中的 applicationContext.xml 来把对象放入IOC中
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
@RestController
public class DeptConsumerController {
@Autowired
private RestTemplate restTemplate; // 提供多种便捷远程访问http服务的方法,简单的Restful模板
// 前缀固定的,把请求的前缀定义出来,避免重复写
private static final String REST_URL_PREFIX = "http://localhost:8001/";
// 消费者不需要service层,直接通过REST远程过程调用
@RequestMapping("/consumer/dept/add")
public boolean add(Dept dept) {
return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, boolean.class);
}
@RequestMapping("/consumer/dept/get/{id}")
public Dept get(@PathVariable("id") Long id) {
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
}
@RequestMapping("/consumer/dept/list")
public List
@SpringBootApplication
public class DeptConsumer_80 {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer_80.class,args);
}
}
文章标题:SpringCloud(2) - Rest学习环境搭建
文章链接:http://soscw.com/index.php/essay/73415.html