码迷,mamicode.com
首页 > 编程语言 > 详细

SpringCloud(2) - Rest学习环境搭建

时间:2020-07-24 21:11:42      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:utc   odi   obj   简单的   sch   tar   lombok   格林威治   yaml   

一、总体介绍

使用一个 Dept 部门模块做一个微服务通用案例 Consumer消费者(Client)通过 REST 调用 Provider 提供者(Server)提供的服务。

一个父工程中创建多个 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

● 新建父工程项目 microservicecloud, 切记 packaging 是 pom 模式 

● 主要是定义 POM 文件,将后续各个子模块公用的 jar 包等统一提取出来,类似一个抽象父类

1)创建一个普通的maven项目

技术图片

2)删除src目录

3)在pom.xml中修改打包方式为pom

<!--打包方式 pom-->
<packaging>pom</packaging>

4)导入依赖

<?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.cyan</groupId>
    <artifactId>springcloud</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--打包方式-->
    <packaging>pom</packaging>

    <!--版本号-->
    <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>
            <!--springCloud依赖-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR1</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>5.1.47</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.10</version>
            </dependency>
            <!--Mybatis-Plus SpringBoot启动器-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.2</version>
            </dependency>
            <!--日志测试-->
            <dependency><!--logback-->
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>1.2.3</version>
            </dependency>
            <dependency><!--Junit-->
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
            </dependency>
            <dependency><!--log4j-->
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <!--Lombok-->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

四、实体类模块 - springcloud-api

数据库:

技术图片

1)新建子模块(普通的maven)

技术图片

2)在该模块中导入需要的依赖

<?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>com.cyan</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-api</artifactId>

    <!--当前Module根据父类依赖管理了配置,不需要再设置版本号-->
    <dependencies>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

</project>

3)新建实体类

@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

1)新建子模块(普通的maven模块)服务提供者,springcloud-provider-dept-8001

技术图片

2)在该模块中导入需要的依赖

<?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>com.cyan</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-provider-dept-8001</artifactId>

    <dependencies>
        <!-- 这个就是我们自己写的模块springcloud-api -->
        <dependency>
            <groupId>cn.codewei</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>
        <!-- 测试和日志 -->
        <!-- junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </dependency>
        <!-- test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
        </dependency>
        <!-- 热部署工具 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

</project>

3)添加编写 application.yaml 配置

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

4)创建 mapper 接口:DeptMapper

@Mapper
@Repository
public interface DeptDao {
    public boolean addDept(Dept dept);
    public Dept queryById(@Param("deptno") Long deptno);
    public List<Dept> queryAll();
}

5)编写对应的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.cyan.dao.DeptDao">

    <!--    public boolean addDept(Dept dept);  -->
    <insert id="addDept" parameterType="Boolean">
        INSERT INTO dept(dname,db_source)
        VALUES (#{dname,jdbcType=VARCHAR},DATABASE())
    </insert>

    <!--    public Dept queryById(Long deptno);    -->
    <select id="queryById" parameterType="Long" resultType="Dept">
        SELECT * FROM dept WHERE deptno = #{deptno};
    </select>

    <!--    public List<Dept> queryAll();   -->
    <select id="queryAll" resultType="Dept">
        SELECT * FROM dept;
    </select>

</mapper>

6)创建 service 接口:DeptSerivce

public interface DeptService {
    public boolean addDept(Dept dept);
    public Dept queryById(Long deptno);
    public List<Dept> queryAll();
}

7)添加 DeptServiceImpl 实现类

@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<Dept> queryAll() {
        return deptDao8001.queryAll();
    }
}

8)创建 Controller 接口:用来提供RestFul服务

@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<Dept> queryAll() {
        return deptService.queryAll();
    }
}

9)添加启动类 DeptProvider_8001

@SpringBootApplication
public class DeptProvider_8001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptProvider_8001.class, args);
    }
}

六、服务消费者模块 - springcloud-consumer-dept

1)新建子模块(普通的maven模块)服务消费者:springcloud-consumer-dept-80

2)在该模块中导入需要的依赖

<?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>com.cyan</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-consumer-dept</artifactId>

    <dependencies>
        <!--实体类-->
        <dependency>
            <groupId>com.cyan</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--web-->
        <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>

</project>

3)创建 application.yaml 配置端口号

server:
  port: 80

4)写一个配置类来将 RestTemplate 放入 IOC 容器中

@Configuration
public class ConfigBean {   // 相当于spring中的 applicationContext.xml  来把对象放入IOC中
    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

5)写一个 Controller 接口 DeptConsumerController 来给前端提供访问

@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<Dept> list() {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
    }

}

6)编写主启动类 DeptConsumer_80

@SpringBootApplication
public class DeptConsumer_80 {
    public static void main(String[] args) {
        SpringApplication.run(DeptConsumer_80.class,args);
    }
}

这样,服务消费者通过 RestTemplate 就可以发起请求获取到远程的服务了

技术图片

技术图片

SpringCloud(2) - Rest学习环境搭建

标签:utc   odi   obj   简单的   sch   tar   lombok   格林威治   yaml   

原文地址:https://www.cnblogs.com/Dm920/p/13174613.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!