微服务项目:尚融宝(33)(服务调用(1))

认清现实,放弃幻想,准备斗争

一、运行Nacos注册中心

1、Nacos下载和安装

 

2、Windows启动Nacos

解压:将下载的压缩包解压

启动:startup.cmd -m standalone

3、访问

http://localhost:8848/nacos 

用户名密码:nacos/nacos

二、服务发现

1、引入依赖

service-base模块中配置Nacos客户端依赖

<!--服务发现-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

2、添加服务配置信息

在需要注册到注册中心的微服务放入配置文件中添加配置

#spring:
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 # nacos服务地址

 每个服务模块都要配置

 

3、启动微服务

启动已注册的微服务,查看 “服务管理 => 服务列表”,可就以看到已注册的微服务

 

需求

发送短信时校验手机号是否注册

一、校验手机号是否注册

1、UserInfoController

service-core中添加controller方法:将网络图片保存到OSS服务器中

@ApiOperation("校验手机号是否注册")
@GetMapping("/checkMobile/{mobile}")
public boolean checkMobile(@PathVariable String mobile){
    return userInfoService.checkMobile(mobile);
}

2、UserInfoService

接口:

boolean checkMobile(String mobile);

实现: 

@Override
public boolean checkMobile(String mobile) {
    QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("mobile", mobile);
    Integer count = baseMapper.selectCount(queryWrapper);
    return count > 0;
}

三、接口的远程调用

service-sms中添加远程调用

1、CoreUserInfoClient

接口:


@FeignClient(value = "service-core")
public interface CoreUserInfoClient {

    @GetMapping("/api/core/userInfo/checkMobile/{mobile}")
    boolean checkMobile(@PathVariable String mobile);
}

2、ApiSmsController

引入client

@Resource
private CoreUserInfoClient coreUserInfoClient;

 在获取验证码方法中调用远程方法校验手机号是否存在

//手机号是否注册
boolean result = coreUserInfoClient.checkMobile(mobile);
System.out.println("result = " + result);
Assert.isTrue(result == false, ResponseEnum.MOBILE_EXIST_ERROR);

//生成验证码
.....

四、超时控制

openfeign默认的连接超时时间为1秒,测试时很可能会出现远程调用超时错误。

可以在配置文件中添加如下配置:

feign:
  client:
    config:
      default:
        connectTimeout: 10000 #连接超时配置
        readTimeout: 600000 #执行超时配置

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一个风轻云淡

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

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

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

打赏作者

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

抵扣说明:

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

余额充值