Spring cloud 3 - Eurake+Feign服务使用

前面已经搭建了Eurake的服务,以及提供了一个dept的三层架构查询。
现在只需要完成该系统从Eurake调用deptService。

jar包(有可以不需要加)

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.12.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-feign</artifactId>
        <version>1.3.5.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
        <version>1.3.5.RELEASE</version>
    </dependency>

</dependencies>

application.properties文件

server.port=8081
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.registry-fetch-interval-seconds: 30
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:8761/eureka/
spring.application.name=eurake-user-controller

需要写一个接口来调用给我们提供服务的类

//Eurake里面的应用名称
@FeignClient("USER-SERVICE")
public interface EurakeService {

    //必须并且只能填写一种请求方法
    //value:写请求那个项目的controller层路径
    @RequestMapping(method = RequestMethod.GET,value="/dept/find")
    public List<Dept> findAll();
}

本系统的调用

@Controller
@RequestMapping("/dept")
public class DeptController {

    //把接口注入进来即可
    @Autowired
    private EurakeService eurakeService;

    @RequestMapping("find")
    @ResponseBody
    public List<Dept> find(){
        List<Dept> list = eurakeService.findAll();
        System.out.println(list.size());
        return list;
    }
}

启动

//Springboot启动
@SpringBootApplication
//Eureka客户端
@EnableEurekaClient
//Feign客户端
@EnableFeignClients
public class StartProject {
    public static void main(String[] args) {
        SpringApplication.run(StartProject.class, args);
    }
}

链接:https://pan.baidu.com/s/1IJLUhyUjPBHHb9_XzQsElA 密码:gsya

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值