网关gateway模块
①、配置文件,添加各个服务模块的路由路径
yml
gateway:
routes:
-id: server-cart #微服务名称
uri: lb://service-cart #负责均衡
predicates:
- Path=/api/order/cart/**
ThreadLocal
①、定义一个工具类
java
public class AuthContextUtil{
private static final ThreadLocal<SysUser> threadLocal = new ThreadLocal<>();
//存储数据
public static void set(SysUser sysUser){
threadLocal.set(sysUser);
}
//获取数据
public static SysUser get(){
return threadLocal.get();
}
//删除数据
public static void remove(){
threadLocal.remove();
}
}
②、业务方法中使用
java
@GetMapping(value="/getUserInfo")
public Result getUserInfo(){
return Result.build(AuthConextUtil.get(),ResultCodeEnum.SUCCESS); //从ThreadLocal中获取用户信息
}
远程调用
购物车服务模块------>商品服务模块中的数据
①、service-client模块将调用接口单独剥离出来作为调用接口端
添加依赖:spring-cloud-starter-openfeign spring-cloud-loadbalancer
创建远程调用接口
java
@FeignClient(value="service-product") //value的值和service-product模块中配置文件服务名称一致
public interface ProductFeignClient{
@GetMapping("/api/product/getBySkuId/{skuId}")
public ProductSku getBySkuId(@PathVariable("skuId") Long skuId);
}
②、service-cart模块中引入service-client模块坐标
启动类上添加注解
java
@EnableFeignClients(basePackages = {"com.michael.spzx"})
@EnableUserLoginAuthInterceptor //该注解是针对ThreadLocal存入的用户信息
java
@Autowired
private ProductFeignClient productFeignClient;//将接口进行注入
//方法中,调用即可
③、service-product模块服务提供
service/mapper省略
java
@GetMapping("/getbySkuId/{skuId}")
public ProductSku getBySkuId(@PathVariable Long skuId){
ProductSku productSku = productService.getBySkuId(skuId);
return productSku;
}
读取配置文件
①、配置文件
配置文件的相互导入
application-alipay.yml
yml
spzx:
applipay: #值省略
allipay_url:
app_id:
app_private_key:
return_payment_url:
notify_payment_url:
application-dev.yml中进行引入
yml
spring:
config:
import: application-alipay.yml
②、定义实体类读取配置文件
java
@Data
@ConfigurationProperties(prefix="apzx.alipay")
public class AlipayProperties{
private String alipayUrl; //属性名和配置文件中的保持一致
private String appPrivateKey;
public String alipayPublicKey;
private String appId;
public String returnPaymentUrl;
public String notifyPaymentUrl;
public final static String format="json";
public final static String charset="utf-8";
public final static String sign_type="RSA2"
}
③、启动类添加注解
java
@EnableConfigurationProperties(value={AlipayProperties.class})
统一异常
①、创建异常定义
java
@ControllerAdvice //给Controller增加一个统一操作
public class GlobalExceptionHandler{
//全局异常处理
@ExceptionHandler(Exception.class)
@ResponseBody
public Result error(){
return Result.build(null,ResultCodeEnum,SYSTEM_ERROR);
}
//引入自定义异常
@ExceptionHandler(GuiGuException.class)
@ResponseBody
public Result error(GuiguException e){
return Result.build(null,e.getResultCodeEnum());
}
}
java
/**
自定义异常
*/
@Data
public class GuiguException extends RuntimeException{
private Integer code;
private String message;
private ResultCodeEnum resultCodeEnum; //枚举类封装的错误状态码和错误信息
public GuiguExcetion(ResultCodeEnum resultCodeEnum){
this.resultCodeEnum = resultCodeEnum;
this.code = resultCodeEnum.getCode;
this.message = resultCodeEnum.getMessage();
}
public GuiguExcetion(Integer code,String message){
this.code = code;
this.message = message;
}
}
②、Service业务类使用
java
if(sysUser == null){
//throw new RuntimeException("用户名或密码错误");
//改成自定义异常
throw new GuiguException(ResultCodeEnum.LOGIN_ERROR);
}