SpringCloud feign调用问题

微服A

启动类
@SpringBootApplication
@EnableEurekaClient             //启动EnableEureka客户端
@EnableCircuitBreaker           //断路器、熔断
@EnableFeignClients({"com.order.feign"})  //当前feign业务接口接口所在的包
@EnableAceCache                 //开启缓存注解,第三方封装
@EnableTransactionManagement    //开启注解事务
@MapperScan("com.order.mapper")    //Mapper 扫描包
@EnableDiscoveryClient
public class OrderBootstrap {

    public static void main(String[] args) {
        new SpringApplicationBuilder(OrderBootstrap.class).web(true).run(args);
    }

}

控制器

import com.github.wxiaoqi.security.common.result.sub.Result;
import com.yueyongyueyou.order.feign.TestFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Title: HshShoppingCarController
 * @Description: 购物车控制器
 * @author wdz
 * @date 2019/3/11 13:05
 */
@RestController
@RequestMapping(value="/shopCar/")
@Slf4j
public class HshShoppingCarController {

    @Autowired
    TestFeignService testFeignService;

    @GetMapping(value = "getShopCar")
    public Result getShopCar(String msg){
        log.info("获取购物车");
        Result result = new Result();
        result = testFeignService.getTest(msg);
        return result;
    }
}

feign业务
import com.github.wxiaoqi.security.common.result.sub.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
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.ResponseBody;

@FeignClient(name = "hsh-item")
@Component(value = "testFeignService")
@RequestMapping(value = "/api/item/")
public interface TestFeignService {

    @RequestMapping(value = "getTest",method = RequestMethod.GET)
    @ResponseBody
    Result getTest(@RequestParam("msg") String msg);
}

微服B
启动类
import com.ace.cache.EnableAceCache;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.transaction.annotation.EnableTransactionManagement;


@EnableEurekaClient             //启动EnableEureka客户端
@EnableCircuitBreaker           //断路器、熔断
@EnableFeignClients({"com.item.feign"})  //服务调用
//@EnableScheduling               //开启计划任务支持
//@EnableAceAuthClient          //开启服务鉴权,
@EnableAceCache                 //开启缓存注解,第三方封装
@EnableTransactionManagement    //开启注解事务
@MapperScan("com.item.mapper")    //Mapper 扫描包
@SpringBootApplication   //标记为SpringBoot 启动类
@EnableDiscoveryClient
public class ItemBootstrap {

    public static void main(String[] args) {
        System.out.println("服务启动............");
        new SpringApplicationBuilder(ItemBootstrap.class).web(true).run(args);
    }
}
控制器

import com.github.wxiaoqi.security.common.result.sub.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/api/item")
@Slf4j
public class OrderItemController {

    @RequestMapping("/getTest")
    @ResponseBody
    public Result getTest(String msg){
        Result result = new Result();
        log.info("item服务中接收到的参数是:{}",msg);
        return result.setS("我是item返回的哦");
    }
}

 

启动类的feign注解配置及feign服务配置即可

你可能感兴趣的:(java)