栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringCloud Alibaba微服务实战二 服务调用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringCloud Alibaba微服务实战二 服务调用

目录

服务调用

Openfeign远程调用

统一返回类型

远程调用

测试负载均衡是否生效


服务调用

在SpringCloud体系中,所有微服务间的通信都是通过Feign进行调用,Feign是一个http请求调用的轻量级框架,可以以Java接口注解的方式调用Http请求,而不用像使用HttpClient、OKHttp3等组件通过封装HTTP请求报文的方式调用。Feign通过处理注解,将请求模板化,当实际调用的时候,传入参数,根据参数再应用到请求上,进而转化成真正的请求,这种请求相对而言比较直观。而且Feign默认集成了负载均衡器Ribbon,不需要自己实现负载均衡逻辑。

Openfeign远程调用

Feign使用Ribbon作为负载均衡

Openfeign使用Loadbalance作为负载均衡

编辑配置 勾上允许允许多个实例

先启动一个8081

更改yaml的端口号后再启动

便可启动2个不同端口的同一实例

 

 

可以看到除了端口不一致外其它都一致

统一返回类型

Java接口统一返回数据类型_你蠢哭了我的博客-CSDN博客

account-service controller

package com.kuang.account.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kuang.account.service.AccountService;
import com.kuang.common.entity.Account;
import com.kuang.common.exception.BusinessException;
import com.kuang.common.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    AccountService accountService

    @GetMapping("/getAccountList")
    public Result getAccountList(){
        List list = accountService.list();
        return Result.ok().data("list",list);
    }

}

远程调用

user-service

pom.xml 配置

    
        
            org.example
            common-service
            1.0-SNAPSHOT
        

        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            2021.1
        

        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

        
        
            org.springframework.cloud
            spring-cloud-starter-loadbalancer
        

    

用户需要调用账户的服务

那么账户需要提供Openfeign调用的接口

UserApplication 添加  @EnableFeignClients 注解

package com.kuang.user;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

//开启feign的远程调用服务
@EnableFeignClients
@SpringBootApplication
@EnableDiscoveryClient
public class UserApplication {

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

account-service controller新增一个接口

package com.kuang.account.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kuang.account.service.AccountService;
import com.kuang.common.entity.Account;
import com.kuang.common.exception.BusinessException;
import com.kuang.common.response.Result;
import com.kuang.common.response.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    AccountService accountService;

    @Value("${server.port}")
    private String port;


    @GetMapping("/getAccountList")
    public Result getAccountList(){
        List list = accountService.list();
        return Result.ok().data("list",list);
    }

    @GetMapping("/getAccountByUserId/{id}")
    public Result getAccountByUserId(@PathVariable("id")Integer id){
        LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Account::getId,id);
        Account account = accountService.getOne(wrapper);
        if(account==null)
        {
            throw new BusinessException(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode(),
                    ResultCode.USER_ACCOUNT_NOT_EXIST.getMessage());
        }
        return Result.ok().data("account",account).data("port",port);
    }
}

user-service feign下创建 账单远程服务类

package com.kuang.user.feign;

import com.kuang.common.response.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;


@FeignClient("account-service")
public interface AccountFeign {

    @GetMapping("/account/getAccountByUserId/{id}")
    Result getAccountByUserId(@PathVariable("id")Integer id);

}

用户调用账户 被调用者 (账户) 要提供多个服务

测试负载均衡是否生效

仅开启一个用户服务与两个账单服务进行测试

 

 反复刷新观察端口号变化

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/980853.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号