ruoyi-cloud 服务间的调用,OpenFeign的使用

1. 在公共包内添加实体类

ruoyi-cloud 服务间的调用,OpenFeign的使用_第1张图片

2.在 com.ruoyi.common.core.constant 添加如下代码

package com.ruoyi.common.core.constant;
public class ServiceNameConstants
{
    /**
     * 药材服务的serviceid (生产者 nacos内注册应用名)
     */
    public static final String DRUG_SERVICE = "ruoyi-drug";
}

ruoyi-api\ruoyi-api-system\src\main\java\com\ruoyi\system\api创建 RemoteDrugService

package com.ruoyi.system.api;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.constant.ServiceNameConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.factory.RemoteDrugFallbackFactory;
import com.ruoyi.system.api.factory.RemoteUserFallbackFactory;
import com.ruoyi.system.api.model.BDrug;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

/**
 * 药材服务
 * 
 * @author ruoyi
 */
@FeignClient(contextId = "remoteDrugService", value = ServiceNameConstants.DRUG_SERVICE, fallbackFactory = RemoteDrugFallbackFactory.class)
public interface RemoteDrugService
{
    /**
     * 通过药材名查询药材信息
     *
     * @param id 药材id
     * @param source 请求来源
     * @return 结果
     */
     // SecurityConstants.INNER  我使用了内部调用接口 所以不需要网关路由那一段url
    @GetMapping("/drug/{id}")
    public AjaxResult getDrugInfo(@PathVariable("id") Long id, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
}

ruoyi-api\ruoyi-api-system\src\main\java\com\ruoyi\system\api\factory创建 RemoteDrugFallbackFactory

package com.ruoyi.system.api.factory;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.system.api.RemoteDrugService;
import com.ruoyi.system.api.domain.SysUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

/**
 * 药材服务降级处理
 * 
 * @author ruoyi
 */
@Component
public class RemoteDrugFallbackFactory implements FallbackFactory<RemoteDrugService>
{
    private static final Logger log = LoggerFactory.getLogger(RemoteDrugFallbackFactory.class);

    @Override
    public RemoteDrugService create(Throwable throwable)
    {
        log.error("药材服务调用失败:{}", throwable.getMessage());
        return new RemoteDrugService()
        {
            @Override
            public AjaxResult getDrugInfo(Long id, String source)
            {
                return AjaxResult.error("获取药材失败:" + throwable.getMessage());
            }
        };
    }
}

3. 在消费者启动类 添加扫描包路径

不然和报如下图所示错误nested exception is java.lang.IllegalStateException: No fallbackFactory instance of type class xxxxxxFallbackFactory found for feign client xxxService

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sciGenuineController': Unsatisfied dependency expressed through field 'sciGenuineService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sciGenuineServiceImpl': Unsatisfied dependency expressed through field 'remoteDrugService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.ruoyi.system.api.RemoteDrugService': Unexpected exception during bean creation; nested exception is java.lang.IllegalStateException: No fallbackFactory instance of type class com.ruoyi.system.api.factory.RemoteDrugFallbackFactory found for feign client remoteDrugService
@SpringBootApplication(scanBasePackages = {"com.ruoyi.genuine","com.ruoyi.system"})
//com.ruoyi.genuine 是我自己的包名字(消费者)

service层

package com.ruoyi.genuine.service.impl;

@Service
public class SciGenuineServiceImpl implements ISciGenuineService 
{
....
	 @Autowired
     private RemoteDrugService remoteDrugService;
...
    @Override
    public BDrug selectBdrugById(Long id)
    {
        AjaxResult drugInfoResult = remoteDrugService.getDrugInfo(id, SecurityConstants.INNER);
     
        if (StringUtils.isNull(drugInfoResult) || StringUtils.isNull(drugInfoResult.get("data")))
        {
            throw new ServiceException("药材:" + id + " 不存在");
        }
        if ("200" == drugInfoResult.get("code"))
        {
            throw new ServiceException((String) drugInfoResult.get("msg"));
        }
        Object o = drugInfoResult.get(AjaxResult.DATA_TAG);
        BDrug dburg = JSON.parseObject(JSON.toJSONString(o), new TypeReference<BDrug>() { });

        return dburg;
    }
}

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