当前位置 : 主页 > 编程语言 > java >

【Spring Cloud 远程调用】管理员服务系统

来源:互联网 收集:自由互联 发布时间:2022-07-22
1. 什么是远程调用 客户端程序通过接口调用服务端程序,并获得该服务返回的数据的过程,称为远程调用。 “商品管理系统”调用“用户管理系统”的过程,就是“远程调用”。此时
1. 什么是远程调用

客户端程序通过接口调用服务端程序,并获得该服务返回的数据的过程,称为远程调用。

“商品管理系统”调用“用户管理系统”的过程,就是“远程调用”。此时“商品管理系统”相当于模拟“浏览器”。


【Spring Cloud 远程调用】管理员服务系统_spring

1.1 调用方式

常见的远程调用方式有2种:

RPC:Remote Produce Call远程过程调用,==自定义数据==格式的远程调用方式。基于原生TCP通信,速度快,效率高。

Http:采用http协议远程调用方式,==规定==了数据传输的格式,缺点是消息封装臃肿。现在热门的Rest风格,就可以通过http协议来实现。

常见框架 :

RPC常见框架: dubbo、WebService

Http常见框架:HttpClient、RestTemplat

区别 HTTP RPC
速度 较慢 快
难度 简单 复杂
灵活性 灵活,跨平台,跨语言

2. 搭建环境
2.1 创建父项目:daykaohe6_22
2.1.1配置pom文件
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-build</artifactId>
<version>2.3.5.RELEASE</version>
</parent>

<properties>
<spring.cloud.version>Hoxton.SR12</spring.cloud.version>
<spring.cloud.alibaba.version>2.2.7-SNAPSHOT</spring.cloud.alibaba.version>
<mybatis.plus.starter.version>3.4.0</mybatis.plus.starter.version>
<durid.starter.version>1.1.10</durid.starter.version>
<swagger.version>2.7.0</swagger.version>
<jwt.jjwt.version>0.9.0</jwt.jjwt.version>
<jwt.joda.version>2.9.7</jwt.joda.version>
</properties>



<dependencyManagement>
<dependencies>
<!-- Spring Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.7.RELEASE</version>
<type>pom</type>
</dependency>

<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis.plus.starter.version}</version>
</dependency>

<!-- Druid连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${durid.starter.version}</version>
</dependency>

<!--swagger2-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
2.2 创建子项目:company-data
  • 环境
  • <!--web起步依赖-->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!--swagger2-->
    <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    </dependency>
    <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    </dependency>

    <!--支持lombok-->
    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    </dependency>

    <dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.0</version>
    </dependency>
    <!--MySQL数据库驱动-->
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!--测试-->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
    <groupId>com.czxy.</groupId>
    <artifactId>test-mybatis-plus</artifactId>
    <version>1.0-SNAPSHOT</version>
    <scope>test</scope>
    </dependency>

    </dependencies>

    2.application.yml配置文件

    server:
    port: 8081
    spring:
    datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/20220622exam?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: 1234
    mybatis-plus:
    configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #输出日志
    map-underscore-to-camel-case: true
    global-config:
    db-config:
    table-prefix: t_

    3.创建javaBean

    package com.czxy.domain;

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

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @TableName("companys")
    public class Companys {
    @TableId
    private String keyno;

    private String cname;

    private String oper;

    private String industry;
    }

    4.创建CompanysContoller

    @RestController
    @RequestMapping("/companys")
    public class CompanysContoller {
    @Resource
    private CompanysMapper companysMapper;

    @GetMapping
    public BaseResult selectAll(){
    List<Companys> list = companysMapper.selectList(null);
    if(list ==null){
    return BaseResult.error("查询失败");
    }
    return BaseResult.ok("查询成功",list);
    }

    @GetMapping("/{keyno}")
    public BaseResult selectById(@PathVariable("keyno"){
    Companys companys = companysMapper.selectById(keyno);
    if(companys ==null){
    return BaseResult.error("查询失败");
    }
    return BaseResult.ok("查询成功",companys);
    }
    @PostMapping
    public BaseResult save(@RequestBody Companys companys){
    int insert = companysMapper.insert(companys);
    if(insert !=1){
    return BaseResult.error("添加失败");
    }
    return BaseResult.ok("添加成功");
    }
    @DeleteMapping("/{keyno}")
    public BaseResult deleteById(@PathVariable("keyno"){
    int i = companysMapper.deleteById(keyno);
    if(i !=1){
    return BaseResult.error("删除失败");
    }
    return BaseResult.ok("删除成功");
    }

    @PutMapping
    public BaseResult updata(@RequestBody Companys companys){
    int i = companysMapper.updateById(companys);
    if(i !=1){
    return BaseResult.error("修改失败");
    }
    return BaseResult.ok("修改成功");
    }


    }

    5.创建Mapper

    @Mapper
    public interface CompanysMapper extends BaseMapper<Companys> {
    }

    6.创建启动类

    @SpringBootApplication
    public class TestCompanyApplication {

    public static void main(String[] args){
    SpringApplication.run(TestCompanyApplication.class,args);
    }
    }
    2.2 创建子项目:company-http

    1.创建启动类

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

    2.创建javaBean

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Companys {
    private String keyno;

    private String cname;

    private String oper;

    private String industry;
    }

    3.创建contoller

    @RestController
    @RequestMapping("/companys")
    public class CompanysContoller {
    @Resource
    private CompanysService companysService;

    @GetMapping
    public BaseResult selectAll(){
    List<Companys> list=companysService.list();
    return BaseResult.ok("查询成功",list);
    }


    @GetMapping("/{keyno}")
    public BaseResult selectById(@PathVariable("keyno"){
    BaseResult<Companys> user = companysService.getById(keyno);
    return BaseResult.ok("查询成功",user);
    }
    @PostMapping
    public BaseResult save(@RequestBody Companys companys){
    try {
    companysService.insert(companys);
    return BaseResult.ok("添加成功");
    } catch (Exception e) {
    return BaseResult.error(e.getMessage());
    }
    }
    @DeleteMapping("/{keyno}")
    public BaseResult deleteById(@PathVariable("keyno"){
    Boolean i = companysService.deleteById(keyno);
    if(i){
    return BaseResult.ok("删除成功");
    }
    return BaseResult.error("删除失败");
    }

    @PutMapping
    public BaseResult updata(@RequestBody Companys companys){
    try {
    companysService.updateById(companys);
    return BaseResult.ok("修改成功");
    } catch (Exception e) {
    return BaseResult.error(e.getMessage());
    }
    }


    }

    4.创建service

    package com.czxy.service.impl;
    import com.czxy.admin.AdminTest;
    import com.czxy.domain.Companys;
    import com.czxy.service.CompanysService;
    import com.czxy.vo.BaseResult;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import javax.annotation.Resource;
    import java.util.List;

    @Service
    @Transactional
    public class CompanysServiceimpl implements CompanysService {

    @Resource
    private AdminTest adminTest;
    @Override
    public List<Companys> list(){
    List<Companys> list= adminTest.list();
    return list;
    }

    @Override
    public BaseResult<Companys> getById(String keyno){
    BaseResult<Companys> userBaseResult = adminTest.selectById(keyno);
    return userBaseResult;
    }

    @Override
    public void insert(Companys companys){
    adminTest.insert(companys);

    }

    @Override
    public Boolean deleteById(String keyno){
    Boolean result=adminTest.delete(keyno);
    return result;
    }

    @Override
    public void updateById(Companys companys){
    adminTest.update(companys);
    }
    }

    5.创建admin进行远程调用

    package com.czxy.admin;

    import com.czxy.domain.Companys;
    import com.czxy.vo.BaseResult;
    import org.checkerframework.checker.units.qual.C;
    import org.springframework.core.ParameterizedTypeReference;
    import org.springframework.http.HttpMethod;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Component;
    import org.springframework.web.client.RestTemplate;

    import javax.annotation.Resource;
    import java.util.List;

    @Component
    public class AdminTest {

    @Resource
    private RestTemplate restTemplate;


    public List<Companys> list(){
    String url="http://localhost:8081/companys";
    ResponseEntity<BaseResult> entity = restTemplate.getForEntity(url, BaseResult.class);
    List<Companys> list = (List<Companys>) entity.getBody().getData();
    return list;
    }


    public BaseResult<Companys> selectById(String keyno){
    String url="http://localhost:8081/companys/"+keyno;
    ResponseEntity<BaseResult<Companys>> resultResponseEntity = restTemplate.exchange(url, HttpMethod.GET, null, new ParameterizedTypeReference<BaseResult<Companys>>() {
    });
    return resultResponseEntity.getBody();
    }

    public void insert(Companys companys){
    String url="http://localhost:8081/companys";
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<BaseResult> entity = restTemplate.postForEntity(url, companys, BaseResult.class);

    }

    public Boolean delete(String keyno){
    String url="http://localhost:8081/companys/"+keyno;
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.delete(url);
    return true;
    }

    public void update(Companys companys){
    String url="http://localhost:8081/companys";
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.put(url,companys,BaseResult.class);
    }
    }

    最后进行测试

    http://localhost:8082/swagger-ui.html#/

    最后只要能调用到就说明你已经学会了哦~

    网友评论