FeignService.java 1.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
package com.kwan.springcloud.service;

import com.kwan.springcloud.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Service
public class FeignService {

    /**
     * http直接访问
     */
    private static final String GOODS_URL = "http://localhost:9100/kwanGoodsInfo/";


    /**
     * eureka访问
     */
    private static final String GOODS_URL_2 = "http://goods-service/kwanGoodsInfo/";


    @Autowired
    RestTemplate restTemplate;

    //    @HystrixCommand(fallbackMethod = "addServiceFallback")
    public Result addService(String id) {

        //调用远程的controller
        return restTemplate.getForEntity(GOODS_URL_2 + id, Result.class).getBody();
    }

    public Result addServiceFallback(String id) {
        log.info("失败id={}", id);
        return Result.error("ribbon方式请求 服务不可用 请求失败");
    }

}