首页 > 编程语言 > 详细

SpringCloud(3)-Rest环境搭建

时间:2020-04-21 23:56:02      阅读:120      评论:0      收藏:0      [点我收藏+]

SpringCloud(3)- Rest环境搭建

总体介绍

  1. 数据库:sc01 , 表dept:

技术分享图片

插入一些数据:

技术分享图片

  1. 父Maven工程:springcloud

  2. 父Maven工程下的三个模块:

    springcloud-api【封装整体的entity/公共配置等】

    springcloud-provider-dept-8001【服务提供者】

    springcloud-consumer-dept-80【服务消费者】

版本选择

  1. 打开spring官网,找到Spring Cloud ,查看最新版本的Doc

技术分享图片

  1. 发现最新版本的要spring Boot 2.2.5.RELEASE 即以上

技术分享图片

父Maven工程:springcloud

  1. 新建一个Maven工程,修改其pom文件,修改打包方式为pom,增加依赖管理,版本管理。
<packaging>pom</packaging>
 <properties>
        <mybatis-version>2.1.2</mybatis-version>
        <druid-version>1.1.20</druid-version>
        <lombok-version>1.16.16</lombok-version>
 </properties>
<dependencyManagement>
    <dependencies>
        <!-- SpringCloud依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!-- SpringBoot依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.2.5.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!-- MyBatis依赖 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-version}</version>
        </dependency>

        <!-- Druid依赖 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid-version}</version>
        </dependency>

        <!-- Lombok依赖 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok-version}</version>
        </dependency>

    </dependencies>
</dependencyManagement>

springcloud-api【封装整体的entity/公共配置等】

  1. 在父Maven项目下新建一个Module,创建子Maven项目springcloud-api。
  2. 在pom文件中添加依赖。
    <dependencies>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

    </dependencies>
  1. 新建entity包,编写实体类Dept。
@Data
@NoArgsConstructor
public class Dept implements Serializable {
    private long deptno;
    private String deptname;
    private String dbSource;
    public Dept(String deptname){
        this.deptname=deptname;
    }
}

springcloud-provider-dept-8001【服务提供者】

  1. 在父Maven项目下新建一个Module,创建子Maven项目springcloud-provider-dept-8001。

  2. 在pom文件中添加依赖。

 <dependencies>
        <dependency>
            <groupId>com.yinrz</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
    </dependencies>
  1. 修改application.yml文件
server:
  port: 8001

mybatis:
  type-aliases-package: com.yinrz.springcloud.entity
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml


spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/sc01?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
  1. 在resources下新建mybatis文件夹,在此文件夹下新建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"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>
  1. 在com.yinrz.springcloud包下新建mapper包,编写DeptMapper
@Mapper
@Repository
public interface DeptMapper {
    List<Dept> selAll();
    Dept selById(long id);
    boolean insDept(Dept dept);
}

  1. 在com.yinrz.springcloud包下新建service包与impl包,编写DeptService与DeptServiceImpl
public interface DeptService {
    List<Dept> queryAll();
    Dept queryById(long id);
    boolean addDept(Dept dept);
}
@Service
public class DeptServiceImpl implements DeptService {
    @Resource
    private DeptMapper deptMapper;

    public List<Dept> queryAll(){
        return deptMapper.selAll();
    }

    public Dept queryById(long id) {
        return deptMapper.selById(id);
    }

    public boolean addDept(Dept dept) {
        return deptMapper.insDept(dept);
    }
}
  1. 在resource下的mybaits包下新建文件夹mapper,编写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.yinrz.springcloud.mapper.DeptMapper">
    <select id="selAll" resultType="com.yinrz.springcloud.entity.Dept" >
        select * from sc01.dept
    </select>
    <select id="selById" resultType="com.yinrz.springcloud.entity.Dept" parameterType="long" >
        select * from sc01.dept where deptno = #{deptno}
    </select>
    <insert id="insDept"  parameterType="com.yinrz.springcloud.entity.Dept" >
        insert into sc01.dept(deptname, db_source) values (#{deptname},DATABASE())
    </insert>
</mapper>
  1. 在com.yinrz.springcloud包下新建controller包,编写DeptProviderController
@RestController
public class DeptProviderController {

    @Resource
    DeptService deptServiceImpl;

    @GetMapping("/dept/all")
    public List<Dept> queryAll(){
        return deptServiceImpl.queryAll();
    }
    @PostMapping("/dept/add")
    public boolean addDept(Dept dept){
        return deptServiceImpl.addDept(dept);
    }
    @GetMapping("/dept/get/{id}")
    public Dept queryById(@PathVariable("id") long id){
        return deptServiceImpl.queryById(id);
    }
}
  1. 在com.yinrz.springcloud包下编写主启动类DeptProvider_8001
@SpringBootApplication
public class DeptProvider_8001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptProvider_8001.class,args);
    }
}

springcloud-consumer-dept-80【服务消费者】

  1. 在父Maven项目下新建一个Module,创建子Maven项目springcloud-consumer-dept-80。

  2. 在pom文件中添加依赖。

 <dependencies>

        <dependency>
            <groupId>com.yinrz</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>
  1. 修改application.yml文件
server:
  port: 80
  1. 在com.yinrz.springcloud包下新建config包,编写BeanConfig,配置RestTemplate
@Configuration
public class BeanConfig {
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}
  1. 在com.yinrz.springcloud包下新建controller包,编写DeptConsumerController
@RestController
public class DeptConsumerController {

    @Resource
    RestTemplate restTemplate;
    private static final String REST_PATH_PREFIX="http://localhost:8001";

    @RequestMapping("/consumer/dept/all")
    public List<Dept> all(){
        return restTemplate.getForObject(REST_PATH_PREFIX+"/dept/all",List.class);
    }
    @RequestMapping("/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id") long id){
        return restTemplate.getForObject(REST_PATH_PREFIX+"/dept/get/"+id,Dept.class);
    }
    @RequestMapping("/consumer/dept/add")
    public boolean add(Dept dept){
        return restTemplate.postForObject(REST_PATH_PREFIX+"/dept/add",dept,Boolean.class);
    }

}
  1. 在com.yinrz.springcloud包下编写主启动类DeptConsumer_80
@SpringBootApplication
public class DeptConsumer_80 {
    public static void main(String[] args) {
        SpringApplication.run(DeptConsumer_80.class,args);
    }
}

测试

  1. 分别启动DeptProvider_8001,与DeptConsumer_80。
  2. 浏览器访问http://localhost/consumer/dept/get/1

技术分享图片

浏览器访问http://localhost/consumer/dept/add?deptname=123

技术分享图片

浏览器访问http://localhost/consumer/dept/all

技术分享图片

SpringCloud(3)-Rest环境搭建

原文:https://www.cnblogs.com/yinrz/p/12748972.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!