SpringCloud系列——构建多模块应用,远程调用服务day1-3

SpringCloud系列——构建多模块应用,远程调用服务

  • 构建多模块应用
    • 1.构建父模块
    • 2. 清理父模块
    • 3.父模块pom管理依赖
      • 注意点
      • 完整依赖
    • 4.构建子模块(commons)
    • 5.pom依赖导入
      • 引用父模块
      • 完整子pom
  • 远程调用服务
    • 1. 配置声明,注册RestTemplate到spring容器中
    • 2.编写controller进行调用
      • 声明服务地址
      • 客户端调用
      • 完整代码
    • 项目重构示例
      • 目录解释
      • 1.编写common-api模块
        • 目录
        • pom依赖导入
        • 将实体类,工具类抽取出来
          • user实体类
          • 统一结果处理ResultJSONUtil
        • 使用maven清理项目
      • 2.编写commons服务模块
        • 目录
        • pom
        • yaml配置
        • 直接编写业务逻辑
          • UserMapper
          • UserService
          • UserServiceImpl
          • UserController
      • 3.构建客户端client
        • 目录
        • pom
        • yaml
        • ApplicationContextConfig
        • ClientController

构建多模块应用

在上一篇文章中我们进行了quickstart的演示,这篇文章中我们进一步,将所有项目使用spring initializr进行构建

1.构建父模块

SpringCloud系列——构建多模块应用,远程调用服务day1-3_第1张图片

2. 清理父模块

只要留下.idea和pom.xml即可!

3.父模块pom管理依赖

注意点

需要加上

//设置打包为pom
<packaging>pom</packaging>
//用于管理下面的子模块
<modules>
        <module>client</module>
        <module>commons</module>
</modules>
//使用properties对版本进行统一管理
<properties>
        <java.version>1.8</java.version>
        <mysql.version>8.0.17</mysql.version>
        <spring.boot.version>2.6.7</spring.boot.version>
        <mybatis.plus.version>3.5.1</mybatis.plus.version>
        <junit.version>5.8.2</junit.version>
        <druid.version>1.2.9</druid.version>
        <cloud.version>2021.0.1</cloud.version>
        <cloud.alibaba.version>2021.0.1.0</cloud.alibaba.version>
        <lombok.version>1.18.22</lombok.version>
    </properties>

完整依赖

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springcloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springcloud</name>
    <description>Demo project for Spring Boot</description>
    <packaging>pom</packaging>
    <modules>
        <module>client</module>
        <module>commons</module>
    </modules>
    <properties>
        <java.version>1.8</java.version>
        <mysql.version>8.0.17</mysql.version>
        <spring.boot.version>2.6.7</spring.boot.version>
        <mybatis.plus.version>3.5.1</mybatis.plus.version>
<!--        <mybatis.version>2.1.1</mybatis.version>-->
        <junit.version>5.8.2</junit.version>
        <druid.version>1.2.9</druid.version>
        <cloud.version>2021.0.1</cloud.version>
        <cloud.alibaba.version>2021.0.1.0</cloud.alibaba.version>
        <lombok.version>1.18.22</lombok.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-validation</artifactId>
                <version>${spring.boot.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>${spring.boot.version}</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis.plus.version}</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>${druid.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${cloud.alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>
<!--            <dependency>-->
<!--                <groupId>org.mybatis.spring.boot</groupId>-->
<!--                <artifactId>mybatis-spring-boot-starter</artifactId>-->
<!--                <version>${mybatis.version}</version>-->
<!--            </dependency>-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <version>${spring.boot.version}</version>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <version>${spring.boot.version}</version>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <version>${spring.boot.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>5.3.14</version>
            </dependency>

            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-test</artifactId>
                <version>${spring.boot.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

4.构建子模块(commons)

同父模块一样构建,但是这里只要删除mvn有关的文件即可,如.mvn,mvn.cmd,mvn,mvnw
SpringCloud系列——构建多模块应用,远程调用服务day1-3_第2张图片

5.pom依赖导入

引用父模块

<parent>
        <groupId>com.example</groupId>
        <artifactId>springcloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

大家都说父模块写了版本子模块就不用写了,这确实,但是不知道时IDEA的版本问题,我的IDEA2021不会帮我直接下载依赖到库里
当然我发现了解决方法
我们还是以带有标签的方式引入,这样就能下载下来,然后再把version标签去掉即可
如我们一开始(这里带有version标签)

			<dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-validation</artifactId>
                <version>${spring.boot.version}</version>
            </dependency>

等到依赖下载到库里之后
SpringCloud系列——构建多模块应用,远程调用服务day1-3_第3张图片
我们再把version标签去掉

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

完整子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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>springcloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>commons</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>commons</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
<!--            <version>2.6.7</version>-->
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
<!--            <version>3.5.1</version>-->
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
<!--            <version>1.2.9</version>-->
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
<!--            <version>2021.0.1</version>-->
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
<!--            <version>${cloud.alibaba.version}</version>-->
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
<!--            <version>${druid.version}</version>-->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
<!--            <version>2.6.7</version>-->
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
<!--            <version>${mysql.version}</version>-->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
<!--            <version>2.6.7</version>-->
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
<!--            <version>${lombok.version}</version>-->
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
<!--            <version>2.6.7</version>-->
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
<!--            <version>5.3.14</version>-->
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
<!--            <version>${junit.version}</version>-->
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
<!--            <version>2.6.7</version>-->
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

远程调用服务

RestTemplate提供了多种便捷访问远程Http服务的方法,
是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集

使用restTemplate访问restful接口非常的简单
(url, requestMap, ResponseBean.class)
这三个参数分别代表REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。

1. 配置声明,注册RestTemplate到spring容器中

package com.example.client.config;

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

@Configuration
public class ApplicationContextConfig {
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }

}

2.编写controller进行调用

声明服务地址

在这里插入图片描述

客户端调用

在这里插入图片描述

完整代码

package com.example.client.contoller;


import com.example.commonapi.util.ResultJSONUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
public class ClientController {

    private static final String COMMONS_URL = "http://localhost:8888";
    private static final String GETUSERINFO_URL = "/user/info";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/client/userinfo")
    public ResultJSONUtil useCommonsGetUserInfo(){
        return restTemplate.getForObject(COMMONS_URL+GETUSERINFO_URL,ResultJSONUtil.class);
    }
}

SpringCloud系列——构建多模块应用,远程调用服务day1-3_第4张图片

项目重构示例

接下来我们对项目进行重构,指明服务端,客户端,将模块的服务区分开来

目录解释

我们看到有一个父模块(springcloud)管理下面的子模块,声明pom中依赖的版本
其中client表示客户端(进行调用其他服务)
commons表示常见服务(提供服务接口供client进行调用)
common-api表示项目的常用类,我们把如常用实体类,工具类放置在其中供全局使用,所以我们要把common-api进行打包形成自己的依赖
SpringCloud系列——构建多模块应用,远程调用服务day1-3_第5张图片

1.编写common-api模块

目录

SpringCloud系列——构建多模块应用,远程调用服务day1-3_第6张图片

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>springcloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>common-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>common-api</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

将实体类,工具类抽取出来

user实体类
package com.example.commonapi.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("user")
public class User {
    private Long id;
    private String username;
    @TableField(value = "nickname")
    private String nickName;
}

统一结果处理ResultJSONUtil
package com.example.commonapi.util;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultJSONUtil {
    private Integer code;
    private String message;
    private Object data;

    public ResultJSONUtil(Integer code, String msg) {
        this(code, msg, null);
    }

    public static ResultJSONUtil success(Integer code,String msg,Object data){
        ResultJSONUtil resultJSONUtil = new ResultJSONUtil();
        resultJSONUtil.setCode(code);
        resultJSONUtil.setMessage(msg);
        resultJSONUtil.setData(data);
        return resultJSONUtil;
    }
    public static ResultJSONUtil success(String msg,Object data){
        return ResultJSONUtil.success(200,msg,data);
    }

    public static ResultJSONUtil fail(Integer code,String msg,Object data){
        ResultJSONUtil resultJSONUtil = new ResultJSONUtil();
        resultJSONUtil.setCode(code);
        resultJSONUtil.setMessage(msg);
        resultJSONUtil.setData(data);
        return resultJSONUtil;
    }

    public static ResultJSONUtil fail(Integer code,String msg){
        return ResultJSONUtil.success(code,msg,null);
    }
}

使用maven清理项目

先切换为跳过测试模式
SpringCloud系列——构建多模块应用,远程调用服务day1-3_第7张图片
然后在生命周期中进行clean

SpringCloud系列——构建多模块应用,远程调用服务day1-3_第8张图片
完成后进行install
SpringCloud系列——构建多模块应用,远程调用服务day1-3_第9张图片
这样我们就得到了自己的一个依赖

2.编写commons服务模块

目录

SpringCloud系列——构建多模块应用,远程调用服务day1-3_第10张图片

pom

其中要引入自己的common-api的依赖
SpringCloud系列——构建多模块应用,远程调用服务day1-3_第11张图片

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>springcloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>commons</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>commons</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>common-api</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
<!--            <version>3.5.1</version>-->
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
<!--            <version>1.2.9</version>-->
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2021.0.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${cloud.alibaba.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
<!--            <version>${druid.version}</version>-->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
<!--            <version>2.6.7</version>-->
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
<!--            <version>${mysql.version}</version>-->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
<!--            <version>2.6.7</version>-->
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
<!--            <version>${lombok.version}</version>-->
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
<!--            <version>2.6.7</version>-->
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
<!--            <version>5.3.14</version>-->
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
<!--            <version>${junit.version}</version>-->
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
<!--            <version>2.6.7</version>-->
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>common-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

yaml配置

配置端口号,名字,数据库连接即可

server:
  port: 8888

spring:
  application:
    name: commons

  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/routerboot?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL&allowPublicKeyRetrieval=true
    username: root
    password: syf20020816

直接编写业务逻辑

UserMapper
package com.example.commons.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.commonapi.entity.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper extends BaseMapper<User> {
}

UserService
package com.example.commons.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.commonapi.entity.User;
import com.example.commonapi.util.ResultJSONUtil;


public interface UserService extends IService<User> {
    ResultJSONUtil getUserInfo();
}

UserServiceImpl
package com.example.commons.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.commonapi.entity.User;
import com.example.commonapi.util.ResultJSONUtil;

import com.example.commons.mapper.UserMapper;
import com.example.commons.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public ResultJSONUtil getUserInfo() {
        User user = userMapper.selectById(1508100589987471362L);
        if(user!=null){
            return ResultJSONUtil.success("success",user);
        }else {
            return ResultJSONUtil.fail(501,"fail");
        }

    }
}

UserController
package com.example.commons.controller;

import com.example.commonapi.util.ResultJSONUtil;
import com.example.commons.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/info")
    public ResultJSONUtil getUserInfo(){
        return userService.getUserInfo();
    }
}

3.构建客户端client

目录

明确client只是充当客户端的角色去调用其他模块的服务,所以无需有service,mapper,entity,util等那些东西
SpringCloud系列——构建多模块应用,远程调用服务day1-3_第12张图片

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>springcloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>client</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>common-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
            <version>2.6.7</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>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

yaml

这里解释一下为什么要有数据库的配置,这也是我没有处理好的地方,因为我在common-api中用到了mybatis-plus的注解,所以必须要有数据库配置才能没有异常

server:
  port: 9000

spring:
  application:
    name: client

  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/routerboot?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL&allowPublicKeyRetrieval=true
    username: root
    password: syf20020816

ApplicationContextConfig

用来注册RestTemplate

package com.example.client.config;

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

@Configuration
public class ApplicationContextConfig {
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }

}

ClientController

package com.example.client.contoller;


import com.example.commonapi.util.ResultJSONUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
public class ClientController {

    private static final String COMMONS_URL = "http://localhost:8888";
    private static final String GETUSERINFO_URL = "/user/info";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/client/userinfo")
    public ResultJSONUtil useCommonsGetUserInfo(){
        return restTemplate.getForObject(COMMONS_URL+GETUSERINFO_URL,ResultJSONUtil.class);
    }
}

你可能感兴趣的:(Java学习,笔记,#,SpringCloud,spring,cloud,spring,boot,java)