openfeign远程调用实现

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] patterns=new String[]{"/login","/order/getMiaoShaResult","/verifyCode/get"};
        registry.addInterceptor(sysInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(patterns);
    }
package com.java1234.miaosha;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableFeignClients(basePackages = "com.java1234.miaosha.feign")
@SpringBootApplication
@MapperScan("com.java1234.miaosha.mapper")
@EnableDiscoveryClient
public class MiaoshaMiaoshaApplication {

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

}

package com.java1234.miaosha.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient("miaosha-order")
public interface OrderFiegnService {

    @RequestMapping("/order/getMiaoShaResult")
    public String getMiaoShaResult(@RequestParam("userId") Integer userId, @RequestParam("miaoShaGoodsId") Integer miaoShaGoodsId);
}

    <dependencies>
        <dependency>
            <groupId>com.java1234</groupId>
            <artifactId>miaosha-common</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>


    /**
     * 查询秒杀结果
     * @param userId
     * @param miaoShaGoodsId
     * @return
     */
    @RequestMapping("/getMiaoShaResult")
    public String getMiaoShaResult(@RequestParam("userId") Integer userId,@RequestParam("miaoShaGoodsId") Integer miaoShaGoodsId){
        System.out.println("order");
        return orderService.getMiaoShaResult(userId,miaoShaGoodsId);
    }
@Override
    public String getMiaoShaResult(Integer userId, Integer miaoShaGoodsId) {
        HashMap map=new HashMap();
        map.put("user_id",userId);
        map.put("miaosha_goods_id",miaoShaGoodsId);
        Order order=orderMapper.findByUserIdAndMiaoShaGoodsId(map);
        if(order!=null){
            return order.getId();
        }else{
            //  TODO 查询商品是否秒杀完 从redis中判断是否商品秒杀完
            boolean isOver = (boolean) redisUtil.get(Constant.REDIS_GOODS_MIAOSHA_OVER_PREFIX, miaoShaGoodsId + "");

            boolean over=true;
            if(over){
                return "-1";
            }else{
                return "0";
            }
        }
    }
package com.java1234.miaosha.service;




import com.baomidou.mybatisplus.extension.service.IService;
import com.java1234.miaosha.entity.Order;

import java.util.Map;

/**
 * 用户Service接口
 */
public interface IOrderService extends IService<Order> {

    /**
     * 根据id查询订单
     * @param id
     * @return
     */
    public Order findById(String id);

    /**
     * 根据用户id和秒杀商品id查询订单
     * @param map
     * @return
     */
    public Order findByUserIdAndMiaoShaGoodsId(Map map);

    /**
     * 获取秒杀结果
     * @param id
     * @param miaoShaGoodsId
     * @return
     */
    public String getMiaoShaResult(Integer id, Integer miaoShaGoodsId);
}

package com.java1234.miaosha.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java1234.miaosha.constant.Constant;
import com.java1234.miaosha.entity.Order;
import com.java1234.miaosha.mapper.OrderMapper;
import com.java1234.miaosha.service.IOrderService;
import com.java1234.miaosha.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户Service实现类
 */
@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Order findById(String id) {
        return orderMapper.findById(id);
    }

    @Override
    public Order findByUserIdAndMiaoShaGoodsId(Map map) {
        return orderMapper.findByUserIdAndMiaoShaGoodsId(map);
    }

    @Override
    public String getMiaoShaResult(Integer userId, Integer miaoShaGoodsId) {
        HashMap map=new HashMap();
        map.put("user_id",userId);
        map.put("miaosha_goods_id",miaoShaGoodsId);
        Order order=orderMapper.findByUserIdAndMiaoShaGoodsId(map);
        if(order!=null){
            return order.getId();
        }else{
            //  TODO 查询商品是否秒杀完 从redis中判断是否商品秒杀完
            boolean isOver = (boolean) redisUtil.get(Constant.REDIS_GOODS_MIAOSHA_OVER_PREFIX, miaoShaGoodsId + "");

            boolean over=true;
            if(over){
                return "-1";
            }else{
                return "0";
            }
        }
    }
}

你可能感兴趣的:(java,spring,boot,开发语言)