微服务和VUE入门教程(23): 微服务之间的调用

微服务和VUE入门教程(23): 微服务之间的调用

微服务和VUE入门教程(0): 着手搭建项目
微服务和VUE入门教程(1): 搭建前端登录界面
微服务和VUE入门教程(2): 注册中心
微服务和VUE入门教程(3): user微服务的搭建
微服务和VUE入门教程(4):网关zuul的搭建
微服务和VUE入门教程(5): 前后端交互
微服务和VUE入门教程(6):连接数据库-mybatis
微服务和VUE入门教程(7):配置中心-config
微服务和VUE入门教程(8):前端主页的编写
微服务和VUE入门教程(9): token验证-token后端生成以及前端获取
微服务和VUE入门教程(10): token验证-前端登录拦截以及token过期提醒
微服务和VUE入门教程(11): mybatis 动态查询
微服务和VUE入门教程(12):前端提示搜索框的实现
微服务和VUE入门教程(13): token验证-zuul拦截与验证
微服务和VUE入门教程(14): 热部署
微服务和VUE入门教程(15): 课堂小知识
微服务和VUE入门教程(16): zuul 熔断
微服务和VUE入门教程(17): VUE 响应拦截器
微服务和VUE入门教程(18): 前端接口模块化
微服务和VUE入门教程(19): VUE组件化–子组件向父组件通信
微服务和VUE入门教程(20): VUE组件化–父组件向子组件通信
微服务和VUE入门教程(21): springboot中定时器-Schedule
微服务和VUE入门教程(22): 页面长时间未操作自动退出登录
微服务和VUE入门教程(23): 微服务之间的调用
微服务和VUE入门教程(24): 微服务之断路器
微服务和VUE入门教程(25): 微服务之Hystrix-dashboard
微服务和VUE入门教程(26): 微服务之turbine
微服务和VUE入门教程(27):VUE前端工程打包## 1. 前言:

开发微服务,免不了会有微服务之间的调用。在这里,我们使用的是openfeign 。因为微服务间的调用不需要通过zuul,因此就可以跳过token验证这一步,但是也没有了zuul的服务转发这个功能。

为了模拟微服务间的调用,我们在my-user微服务中新建一个接口,让my-student微服务来调用这个接口。

2. UserController.java 修改

新建一个hello的接口,很简单,只有一个打印语句。

@RequestMapping(value = "/hello", method = RequestMethod.GET)
public void hello(){
    System.out.println("hello");
}

3. 加入依赖

<!--openfein-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>2.1.3.RELEASE</version>
</dependency>

4. StuApplication.java修改

加入注解

@EnableFeignClients

5. 新建UserFeign接口

在my-student微服务中service包中新建接口UserFeign

package com.student.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

// 微服务的名字
@FeignClient(name = "my-user")
public interface UserFeign {
    // 接口地址
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public void hello();
}

6. StuController.java 修改

6.1 引入UserFeign

@Autowired
UserFeign userFeign;

6.2 使用UserFeign

我们在学生查询的接口中调用my-user的hello接口,来模拟调用,修改如下:

//学生查询
@RequestMapping(value = "/get", method = RequestMethod.POST)
public JSONObject getStudent(@RequestBody JSONObject jsonObject){
    //获取学生信息
    List<Student> studentList = stuService.getStu(jsonObject);
    //获取学生数量
    int stuCount = stuService.getStuCount(jsonObject);

    //微服务之前通信
    userFeign.hello();

    JSONObject result = new JSONObject();
    result.put("studentList",studentList);
    result.put("stuCount",stuCount);
    return result;
}

因为hello这个接口没有返回值,直接使用 userFeign.hello() 即可。

7. 验证:

当我们在前端调用学生查询这个接口的时候,观察一下my-user的控制台输出。
在这里插入图片描述

可以打印出“hello”,说明已成功调用。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值