SpringCloud框架搭建+实际例子+讲解+系列五

(4)服务消费者,面向前端或者用户的服务

本模块涉及到很多知识点:比如Swagger的应用,SpringCloud断路器的使用,服务API的检查、token的校验,feign消费者的使用。大致代码框架如下:

 

 

 

先看下简单的配置文件application.properties

spring.application.name=mallservice-app
server.port=4444
eureka.client.serviceUrl.defaultZone=http://server1:1111/eureka/,http://server2:1112/eureka/,http://server3:1113/eureka/
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds:5000
urifilter.properties

#urllist
url.filterList[0]=/acc/signup
url.filterList[1]=/acc/login
面向用户的Controller类:

package com.mallapp.api;

import com.common.constant.RestApiResult;
import com.common.constant.ReturnCode;
import com.google.gson.Gson;
import com.mallapp.Security.JWTUtils;
import com.mallapp.client.IAccountFeignClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@Api(value="用户服务",tags = "用户服务接口")
@RestController
@RequestMapping("/acc")
public class IAccountController {
@Autowired
IAccountFeignClient accountFeignClient;


@ApiOperation(value="用户注册")
@RequestMapping(value="signup",method = RequestMethod.POST)
public RestApiResult signUp(@RequestParam String phone, @RequestParam String password){
RestApiResult restApiResult = new Gson().fromJson(accountFeignClient.signUp(phone,password),RestApiResult.class);
System.out.println(restApiResult);
return restApiResult;
}
@ApiOperation(value="用户登录")
@RequestMapping(value="login",method = RequestMethod.POST)
public RestApiResult login(@RequestParam String phone ,@RequestParam String password){
RestApiResult restApiResult = new Gson().fromJson(accountFeignClient.login(phone,password),RestApiResult.class);
try{
System.out.println(restApiResult);
if (restApiResult.isSuccess()){
String accessToken = JWTUtils.createJWT(UUID.randomUUID().toString(),(String)restApiResult.getAddmessage(),2*60*60*1000);
restApiResult.setAddmessage(accessToken);
}
}catch (Exception ex){
ex.printStackTrace();
}
return restApiResult;
}
}
@Autowired
IAccountFeignClient accountFeignClient;
 这个是服务发现用的Feign的客户端,看一下它的实现:

package com.mallapp.client;

import com.mallapp.client.hystrix.AccountFeignClientHystrix;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name="ACCOUNT-SERVICE", fallback = AccountFeignClientHystrix.class)
public interface IAccountFeignClient {
@RequestMapping(value = "/acc/signup",method = RequestMethod.GET)
public String signUp(@RequestParam(value = "phone") String phone, @RequestParam(value = "password") String password);
@RequestMapping(value = "/acc/login",method = RequestMethod.POST)
public String login(@RequestParam(value = "phone") String phone, @RequestParam(value = "password") String password);
}
这个接口必须和服务提供端的controller类的接口完全一致,而且参数注解一定完全一致。

 

看下SpringCloud所说的断路器类的实现:(意义就是服务消费者端调用服务提供端的时候,调用超时或者服务器异常等,会直接通过此接口返回响应)

package com.mallapp.client.hystrix;

import com.common.constant.RestApiResult;
import com.common.constant.ReturnCode;
import com.google.gson.Gson;
import com.mallapp.client.IAccountFeignClient;
import org.springframework.stereotype.Component;

@Component
public class AccountFeignClientHystrix implements IAccountFeignClient {
@Override
public String signUp(String phone, String password) {
return new Gson().toJson(new RestApiResult(false, ReturnCode.SYSTEM_ERROR,"The server is busy now......"));
}

@Override
public String login(String phone, String password) {
return new Gson().toJson(new RestApiResult(false, ReturnCode.SYSTEM_ERROR,"The server is busy now......"));
}
}


看下所说的AOP中的前置通知、后置通知、环绕通知等实现类:

package com.mallapp.aop;

import com.common.constant.RestApiResult;
import com.common.constant.ReturnCode;
import com.mallapp.Security.JWTUtils;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.SignatureException;
import io.jsonwebtoken.UnsupportedJwtException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.Map;

@Aspect
@Component
public class ApiExecuteNoticeService {
private final static Logger LOG = LoggerFactory.getLogger(ApiExecuteNoticeService.class);
private final static String access_token = "accessToken";


/**
* 方法之前执行
* @param joinPoint
* @throws Exception
*/
@Before("execution(public * com.mallapp.api.*.*(..))")
public void doBeforeInService(JoinPoint joinPoint)throws Exception{
System.out.println("Before to check the API......");
}

/**
* 方法之后执行
* @param joinPoint
* @throws Exception
*/
@After("execution(public * com.mallapp.api.*.*(..))")
public void AfterInService(JoinPoint joinPoint)throws Exception{
System.out.println("After to check the API......");
}

/**
* 环绕通知
* @param joinPoint
* @return
* @throws Exception
*/
@Around("execution(public * com.mallapp.api.*.*(..))")
public RestApiResult doAroundInService(ProceedingJoinPoint joinPoint)throws Exception{
System.out.println("Around to check the API......");
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)requestAttributes;
HttpServletRequest request = servletRequestAttributes.getRequest();
String requestPath = request.getRequestURI();
System.out.println("uri: " + requestPath);
/*需要过滤不进行检查的url地址*/
// if (requestPath.contains("acc")){
// try {
// return (RestApiResult)joinPoint.proceed();
// } catch (Throwable throwable) {
// throwable.printStackTrace();
// }
// System.out.println("url /acc does not to check.");
// return null;
// }
Map<String,String[]> inputMap = request.getParameterMap();
Iterator<String> keyIter = inputMap.keySet().iterator();
boolean result = false;
while(keyIter.hasNext()){
String currKey = keyIter.next();
String value = ((String[])inputMap.get(currKey))[0].toString();
if (!access_token.equals(currKey)){
continue;
}
try{
JWTUtils.parseJWT(value);
System.out.println("cuurKey="+currKey+",value="+value);
result = true;
}catch(ExpiredJwtException ex){
ex.printStackTrace();
}catch (UnsupportedJwtException ex){
ex.printStackTrace();
}catch (MalformedJwtException ex){
ex.printStackTrace();
}catch (SignatureException ex){
ex.printStackTrace();
}catch (IllegalArgumentException ex){
ex.printStackTrace();
}
}
if (!result){
return new RestApiResult(false,ReturnCode.INVALID_VALUE,"token校验失败.");
}
try {
return (RestApiResult) joinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return new RestApiResult(false,ReturnCode.SYSTEM_ERROR,"unkonwn exception");
}
}
 token校验所涉及到类:

package com.mallapp.Security;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import io.jsonwebtoken.*;
import org.apache.tomcat.util.codec.binary.Base64;

import java.util.Date;
import java.util.UUID;

public class JWTUtils {
private final static String SECRETKEY = "OVlpXYjNwaFJYUllVbXhXTkZaR1pEQlNiVkYzWTBac1YxWkZXbE";
/**
* 由字符串生成加密key
*/
public static SecretKey generateKsy(String keyStr){
byte[] encodeKey = Base64.decodeBase64(keyStr);
SecretKey secretKey = new SecretKeySpec(encodeKey,0,encodeKey.length,"AES");
return secretKey;
}
/**
* 创建JWT,加密过程
*/
public static String createJWT(String id,String subject,long ttlMillis)throws Exception{
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
long nowMillis = System.currentTimeMillis();
Date now = new Date(nowMillis);
SecretKey key = generateKsy(SECRETKEY);
JwtBuilder jwtBuilder = Jwts.builder().setIssuer("").setId(id).setIssuedAt(now).setSubject(subject)
.signWith(signatureAlgorithm,key);
if (ttlMillis >= 0){
long expireMillis = nowMillis + ttlMillis;
Date expireDate = new Date(expireMillis);
jwtBuilder.setExpiration(expireDate);
}
return jwtBuilder.compact();
}
/**
* 解析JWT,解密过程
*/
public static Claims parseJWT(String jwt) throws ExpiredJwtException,UnsupportedJwtException,MalformedJwtException,
SignatureException,IllegalArgumentException{
SecretKey key = generateKsy(SECRETKEY);
Claims claims = Jwts.parser().setSigningKey(key).parseClaimsJws(jwt).getBody();
return claims;
}

// public static void main(String[] args){
// try{
// String token = createJWT(UUID.randomUUID().toString(),"",20000);
// System.out.println(token);
// Claims claims = parseJWT(token);
// System.out.println(claims.getExpiration()+" : "+claims.getExpiration().getTime());
// }catch (Exception ex){
// ex.printStackTrace();
// }
// }
}
 
UriFilterConfig类是用来接受Spring配置的xml文件的:urlifilter.properties
   

package com.mallapp.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
* Created by c00415904 on 2018/5/29.
*/
@Component
@ConfigurationProperties(prefix = "url")
@PropertySource(value = {"classpath:urifilter.properties"} ,ignoreResourceNotFound = true)
public class UriFilterConfig {
private List<String> filterList = new ArrayList<String>();
public List<String> getFilterList() {
return filterList;
}

public void setFilter(List<String> filterList) {
this.filterList = filterList;
}
}
Awagger2Config类用来生成在线API文档: http://127.0.0.1:4444/swagger-ui.html 4444为消费者提供的端口号
package com.mallapp.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Awagger2Config {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(getApiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.mallapp.api"))
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo getApiInfo(){
return new ApiInfoBuilder().title("Mall App Swagger Apis").description("For mall-service 's app use")
.version("V1.0").build();
}
}

服务启动类:

FeignApplication

package com.mallapp;

import com.common.constant.SystemConstant;
import com.common.util.JedisUtil;
import com.mallapp.config.UriFilterConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

import java.util.Date;

@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient
@EnableDiscoveryClient
public class FeignApplication implements CommandLineRunner{
@Autowired
private UriFilterConfig uriFilterConfig;
public static void main(String[] args){
SpringApplication.run(FeignApplication.class,args);
}
@Override
public void run(String... strings) throws Exception {
System.out.println("Begin to init data......"+new Date());
System.out.println(uriFilterConfig.getFilterList());
for(String url : uriFilterConfig.getFilterList()){
JedisUtil.SETS.sadd(SystemConstant.URL_NEED_CHECK_KEY,url);
}
}
}

我们分别启动服务消费者和服务提供者,然后进行postman测试或者前端测试:

 

 

 

转载于:https://www.cnblogs.com/huangwentian/p/10469196.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/449750.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

软件开发者最重要的四大技能

摘要&#xff1a;现如今&#xff0c;可供选择的技术、语言及平台可谓五花八门&#xff0c;因此要弄明白哪里是花时间训练的最佳投资点也就难上加难…… 现如今&#xff0c;可供选择的技术、语言及平台可谓五花八门&#xff0c;因此作为软件开发者&#xff0c;要弄明白哪里是花时…

数据缺失的补充与修改

1查看数据情况 df.shape df.info() 2.用指定值填充 df df.fillna(x) 3.判断是否缺失 df.isnull() 4.删除缺失数据 df df.dropna() 5.补充平均值 df df.fillna(df.mean()) 6.填充他前面一个元素值(ffill向前填充&#xff0c;bfill向后填充)&#xff08;limit:可以…

其他-私人♂收藏(比赛记录 Mar, 2019)

OwO 03.03 [USACO19JAN] A. Redistricting 题意&#xff1a;给 \(g\) &#xff0c;求 \(f(n)\) 。 \(f(i)f(j)[g(i)\ge g(j)],j \in (i-k,i]\) 。 离散化之后线段树优化 DP &#xff1b;或者发现额外贡献最多只有 \(1\) &#xff0c;单调队列。 B. Exercise Route 题意&#xf…

JSR 303 - Bean Validation 简介及使用方法

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 一、JSR-303简介 JSR-303 是 JAVA EE 6 中的一项子规范&#xff0c;叫做 Bean Validation&#xff0c;官方参考实现是Hibernate Valida…

POJ 3683 Priest John's Busiest Day(2-ST)

题目链接&#xff1a;http://poj.org/problem?id3683 题意&#xff1a;有n个婚礼要举行&#xff0c;但是只有一个牧师。第i个婚礼使用牧师的时间长为leni&#xff0c;可以在开始时或结束时使用。问能否使得n个婚礼均举行&#xff1f; 思路:对于婚礼i&#xff0c;i*2-1表示在开…

12个git实战建议和技巧

摘要&#xff1a;git无疑是现在最热门的版本控制工具&#xff0c;而且正在进一步侵占SVN以及CVS的市场。本文作者从国外技术问答社区Stack Overflow整理的12个很实用的git使用技巧和建议&#xff0c;希望对你有帮助。 1.使用“git diff”来折叠多行 用git diff经常会出现很多内…

python读写json和txt

读写json #数据保存如json文件 import json jsObj json.dumps(code_sec) fileObject open(jsonFile.json, w) fileObject.write(jsObj) fileObject.close() #读取json文件 # 将类文件对象中的JSON字符串直接转换成 Python 字典 with open(jsonFile.json, r, encoding…

Java 12 将于3月19日发布,8 个最终 JEP 一览

开发四年只会写业务代码&#xff0c;分布式高并发都不会还做程序员&#xff1f; JDK 12 已于2018年12月进入 Rampdown Phase One 阶段&#xff0c;这意味着该版本所有新的功能特性被冻结&#xff0c;不会再加入更多的 JEP 。该阶段将持续一个月&#xff0c;主要修复 P1-P3 级…

股票期货数据的resample处理

​ import pandas as pd stock_day pd.read_csv("stock_day.csv") stock_day stock_day.sort_index() # 对每日交易数据进行重采样 &#xff08;频率转换&#xff09; stock_day.index# 1、必须将时间索引类型转换成Pandas默认的类型 stock_day.index pd.to_datet…

ArcEngine调用FeatureToLine工具传参问题

FeatureToLine工具的in_features参数不能为内存图层&#xff0c;否则会报内存错误&#xff0c;正确的写法如下&#xff1a; FeatureToLine ftrToLine new FeatureToLine(); ftrToLine.in_features cpj.TempWs.PathName "\OriginDataset\" currentFc.Key; ftrToLi…

程序员如何做出“不难看”的设计

摘要&#xff1a;程序员在写代码的时候往往只注重功能的实现和性能的提升&#xff0c;忽视了外观和易用性&#xff0c;其实很多时候只要注意一些基本的规则&#xff0c;就可以大幅度提高产品的观感。 经常看到程序员展示自己做的东西&#xff0c;有一些是创业项目&#xff0c;有…

微服务实战(二):使用API Gateway

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 当你决定将应用作为一组微服务时&#xff0c;需要决定应用客户端如何与微服务交互。在单体式程序中&#xff0c;通常只有一组冗余的或者…

sql数据库挖坑

sql数据库存入数据时&#xff0c;因为列 名不允许有括号&#xff0c;无法识别&#xff0c;需要对括号进行剔除 df df.rename(columnslambda x: x.replace("(","").replace(),))

力扣——顶端迭代器

给定一个迭代器类的接口&#xff0c;接口包含两个方法&#xff1a; next() 和 hasNext()。设计并实现一个支持 peek() 操作的顶端迭代器 -- 其本质就是把原本应由 next() 方法返回的元素 peek() 出来。 示例: 假设迭代器被初始化为列表 [1,2,3]。调用 next() 返回 1&#xff0c…

五步让你成为专家级程序员

摘要&#xff1a;Mark Lassoff是一位高级技术培训师&#xff0c;从事培训工作已有10余年。他培训的客户包括美国国防部、Lockheed Martin等。在多年的培训生涯中&#xff0c;他总结了一些如何快速学习一门语言的技巧&#xff0c;这些技巧非常简单&#xff0c;但是却让人受益匪浅…

Ionic混合移动app框架学习

第一章 绪论创建移动app有三种安卓原生App&#xff0c;使用java语言&#xff0c;目前推荐kotlin语言&#xff0c;开发工具Android studioIOS原生App&#xff0c;使用Objective-C或者Swift语言&#xff0c;开发工具Xcode混合移动App&#xff0c;使用web通用语言&#xff08;HTML…

IPC 中 LPC、RPC 的区别和联系

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到教程。 进程间通信&#xff08;IPC&#xff0c;Inter-Process Communication&#xff09;&#xff0c;指至少两个进程或线程间传送数据或信号的…

Laravel 使用 Aliyun OSS 云存储

对象存储 ( Object Storage Service, 简称 OSS ) OSS 相信大家都听过, 它是阿里云对外提供的海量, 安全和高可靠的云存储服务. 大家可以把自己网站的资源存上面加快自己网站速度, aliyun 官网也有文档不过对于新手来说有点难, 那么这里我给大家推荐一个组件和组件的使用. johnl…

python多级索引修改

创建多级索引 cols pd.MultiIndex.from_tuples([("a","b"), ("a","c")]) pd.DataFrame([[1,2], [3,4]], columnscols) abc012134 df.columns df.columns.droplevel() df bc012134

在线学习新编程 技巧全攻略

摘要&#xff1a;有句俗语叫&#xff1a;“技多不压身”&#xff0c;如果你有时间和兴趣&#xff0c;不妨多了解和掌握编程技能&#xff0c;或许随时可能有用。本文为你收集了一些编程技巧&#xff0c;让你轻松学编程。 有句俗语叫&#xff1a;“技多不压身”&#xff0c;如果你…