最基础springcloud微服务教学(二)--- Consumer消费者(Client)通过REST调用Provider提供者(Service)提供的服务

一、API工程
这一篇文章基于上一篇文章的工程;既然已经创建了父工程;那么真正的角色也应该让它到位了;首先我根据maven的依赖原理;我将编写一个属于Dept的api工程;因为无论 Consumer消费者(Client)还是Provider提供者(Service);都是需要Dept这么一个实体类;所以我为了方便以后消费者提供者无需自己去写那么一个Dept实体类;我将单独创建API工程;而消费者提供者只需要依赖引用一下就可以了:在springcloud_parent上右击;new -> Moudle -> Maven 这个工程名我就叫做microservicecloud-api;我上篇文章说过;我的一概的思维是先配置后写代码;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">
   <!--继承父工程-->
    <parent>
        <artifactId>springcloud_parent</artifactId>
        <groupId>com.lp.springcloud_parent</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>microservicecloud-api</artifactId> <!--当前的工程的名称-->



    <dependencies>
        <!--当前Module需要用到的jar包;按自己的需求添加-->
        <!--我们只要是引用父工程的jar;无再需写版本;这就是dependencyManagement的作用-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

</project>

当工程一旦创建;我们返回去查看父工程的pom文件;会多了一项 :

		<modules>
        	<module>microservicecloud-api</module>
        </modules>

这就是模块的象征。那么接下来我们就得创建Dept这么一个实体类:
在这里插入图片描述代码如下:

package com.chihiro.microservice.entity;


import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;


 // @AllArgsConstructor全参数的构造方法
@NoArgsConstructor  //无参的构造方法
@Data    //自动给参数生成get和set方法
@Accessors(chain = true)  //开启连式
public class Dept implements Serializable {

    private Long deptno;//主键

    private String dname;//部门名称

   private String db_source;//数据库名称;

     public Dept(String dname) {
         this.dname = dname;
     }
 }

二、Provider提供者(Service)
为什么叫做服务提供者;是不是就是所谓的CURD啊;这个工程它是不是就是给拿来调用接口的啊;给大家提供服务嘛;按照上面步骤在父工程下创建一个名称microservicecloud-privider-dept-8001 这里的8001指的就是端口名称;这里我为了方便区分所以加上端口号命名;老规矩;修改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">
    <!--继承父工程-->
    <parent>
        <artifactId>springcloud_parent</artifactId>
        <groupId>com.lp.springcloud_parent</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>microservicecloud-privider-dept-8001</artifactId>

    <dependencies>
        <!--引入自定义的api通用包,可以使用Dept部门entity-->
        <dependency>
            <groupId>com.lp.springcloud_parent</groupId>
            <artifactId>microservicecloud-api</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>

        <!--mysql驱动包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </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-jetty</artifactId>
        </dependency>

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

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

        <!--修改保存之后立即生效  热部署-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
            <version>1.2.4.RELEASE</version>
        </dependency>

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

    </dependencies>


</project>

接下来修改yml文件:


mybatis:
  config-location: classpath:mybatis-config/mybatisconfig.xml       # mybatis的配置文件路径
  type-aliases-package: com.chihiro.microservice.entity     # 实体别名
  mapper-locations:
  - classpath:mybatis-config/mapper/**/*.xml       # mapper的配置文件路径



spring:
   application:
     name: microservicecloud-dept          #这个名称对于注册服务的时候起很大作用
   datasource:                              #数据源信息
     type: com.alibaba.druid.pool.DruidDataSource
     driver-class-name: com.mysql.jdbc.Driver
     url: jdbc:mysql://localhost:3306/microservice
     username: root
     password: 123456
     dbcp2:
       min-idle: 5                         # 数据库连接池的最小维持连接数
       initial-size: 5                     # 初始化连接数
       max-total: 5                        # 最大连接数
       max-wait-millis: 200                # 等待连接获取的最大超时时间
server:
  port: 8001     #端口号

既然是提供者;咋们是不是就得写一套接口啊;必须连接数据库啊;按往常出牌呗;首先咋们先将配置文件写好:
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"/>  <!-- 全局映射器启用缓存 -->
        <setting name="useGeneratedKeys" value="true"/>  <!-- 允许 JDBC 支持自动生成主键 -->
        <setting name="defaultExecutorType" value="REUSE"/> <!-- 配置默认的执行器 -->
        <setting name="logImpl" value="SLF4J"/> <!-- 指定 MyBatis 所用日志的具体实现 -->
        <!-- <setting name="mapUnderscoreToCamelCase" value="true"/>  驼峰式命名 -->
    </settings>

</configuration>

配置好就是一套约定模板啊;一言不合用图说话:
在这里插入图片描述
是不是咱们项目经常规范的操作啊;我相信对于大家这个建立还是比较容易的;这里的细节代码我就不写上去了;对于这个并不是很难;我就把代码放到git上;提供给大家作为参考。**但是必须首先自我测试一下;要能够自我可以运行。**我这里为了给大家展示自我调用和消费者调用的区别和成功;这里我自己测试了:
在这里插入图片描述

三、Consumer消费者(Client)
所谓的消费者;它就相当于只是去调用接口;对于接口那里来的以及做具体哪些操作;他并不要管这些;就像我是作者;是不是要写文章啊;提供给你们看啊;而你们并不要去写文章吧;只需要认真阅读吧;那么它的配置或者写的代码是不是就很少了啊;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">
    <parent>
        <artifactId>springcloud_parent</artifactId>
        <groupId>com.lp.springcloud_parent</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>microservicecloud-consumer-dept-80</artifactId>
    <description>部门服务的消费者</description>


    <dependencies>
            <!--使用自定义的api-->
        <dependency>
            <groupId>com.lp.springcloud_parent</groupId>
            <artifactId>microservicecloud-api</artifactId>
            <version>${project.version}</version>
        </dependency>

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

        <!--修改保存之后立即生效  热部署-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
            <version>1.2.4.RELEASE</version>
        </dependency>

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

    </dependencies>


</project>

因为一般调用接口是不是有返回数据给页面啊;所以消费者工程偏向与WEB的;所以我们需要导入web的jar包。而他的yml配置就更加简单了:

server:
  port: 80

这就是它的yml;那我这个单独的消费者工程怎么去调用到提供者工程的接口呢;这时候我们需要一个REST了;咋们需要利用它去调接口;那REST又是什么呢?说白了就是一个重新封装httpClient请求的一个类;使用它咋们必须需要配置下:

package com.chihiro.microservice.cfgbeans;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration   // 相当于-->spring  的 applicationContext.xml
public class ConfigBean {


    @Bean
    public RestTemplate getUserSeriver(){

        return new RestTemplate();
    }

}

咋们还得写一个Controller去调用:

package com.chihiro.microservice.controller;

import com.chihiro.microservice.entity.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

@Controller
public class DeptController_consumer {
	//访问接口前缀地址
    private static final String REST_URL_PREFIX = "http://localhost:8001";   

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/consumer/dept/list")
    @ResponseBody
     public List<Dept> list(){
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);  //参数说明("访问的URL","需传的数据","该接口返回数据类型")
    }
}

消费者的代码就完成了;现在就是去调用了;就必须开启两个应用了;首先开启提供者的服务;它是8001端口号;然后开启消费者服务;80端口号;我为了区别我把这个请求 @RequestMapping(value = “/consumer/dept/list”)前面加了consumer;为了给大家展示:
在这里插入图片描述
源码下载:https://git.oschina.net/Chirhiro/springcloud

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小面包CC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值