飞滴出行网约车2022-使用 feign 远程调用验证码生成接口以及可变参数控制验证码位数

master
yh 3 years ago
parent 02b7b10477
commit 0feb760919

@ -27,6 +27,24 @@
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.mashibing</groupId>
<artifactId>internal-common</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- feign 组件 基于 loadbalancer 依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -3,9 +3,11 @@ package com.mashibing.apipassenger;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication @SpringBootApplication
@EnableDiscoveryClient @EnableDiscoveryClient
@EnableFeignClients
public class ApiPassengerApplication { public class ApiPassengerApplication {
public static void main(String[] args) { public static void main(String[] args) {

@ -1,5 +1,6 @@
package com.mashibing.apipassenger.controller; package com.mashibing.apipassenger.controller;
import com.mashibing.apipassenger.remote.ServiceVerificationcodeClient;
import com.mashibing.apipassenger.request.VerificationCodeDTO; import com.mashibing.apipassenger.request.VerificationCodeDTO;
import com.mashibing.apipassenger.service.VerificationCodeService; import com.mashibing.apipassenger.service.VerificationCodeService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;

@ -0,0 +1,18 @@
package com.mashibing.apipassenger.remote;
import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.internalcommon.response.NumberCodeResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient("service-verificationcode")
@Service
public interface ServiceVerificationcodeClient {
@RequestMapping(method = RequestMethod.GET,value = "/numberCode/{size}")
ResponseResult<NumberCodeResponse> getNumberCode(@PathVariable("size") Integer size);
}

@ -1,16 +1,27 @@
package com.mashibing.apipassenger.service; package com.mashibing.apipassenger.service;
import com.mashibing.apipassenger.remote.ServiceVerificationcodeClient;
import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.internalcommon.response.NumberCodeResponse;
import net.sf.json.JSONObject; import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
public class VerificationCodeService { public class VerificationCodeService {
@Autowired
private ServiceVerificationcodeClient verificationcodeClient;
public String generatorCode(String passengerPhone){ public String generatorCode(String passengerPhone){
// 调用验证码服务,获取验证码 // 调用验证码服务,获取验证码
System.out.println("调用验证码服务,获取验证码"); System.out.println("调用验证码服务,获取验证码");
String code = "111111";
// 使用 frign 远程调用生成验证码服务接口
ResponseResult<NumberCodeResponse> numberCodeResponse = verificationcodeClient.getNumberCode(6);
String numberCode = numberCodeResponse.getData().getNumberCode();
System.out.println("remote number code: "+numberCode);
// 存入redis // 存入redis
System.out.println("存入redis"); System.out.println("存入redis");

@ -15,9 +15,9 @@ public class NumberCodeController {
public ResponseResult getNumbercode(@PathVariable Integer size){ public ResponseResult getNumbercode(@PathVariable Integer size){
// 生成指定长度验证码 // 生成指定长度验证码
double codetmp = ((Math.random() + 1)*9) * (Math.pow(10,size-2));// *9 保证首位不为0+1保证最低被乘数大于1长度使用 10的size-2平方数 double codetmp = (Math.random() *9 + 1) * (Math.pow(10,size-1));// *9 保证首位不为0+1保证最低被乘数大于1长度使用 10的size-1平方数
Integer numberCode = (int)codetmp; Integer numberCode = (int)codetmp;
System.out.println("已生成 "+size+" 长度验证码"); System.out.println("generator code"+size+" 长度验证码为:"+numberCode);
// 封装返回结果 // 封装返回结果
NumberCodeResponse data = new NumberCodeResponse(); NumberCodeResponse data = new NumberCodeResponse();

Loading…
Cancel
Save