spring cloud fegin入门

fegin整合了hystrix和ribbon,并且使用了springMVC的相关注解,提供了使用服务提供者服务的更加便捷的方式。以下是fegin的入门配置

pom.xml

        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-hystrixartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-feignartifactId>
        dependency>

引入hystrix依赖与fegin依赖


在主类添加注解

@EnableDiscoveryClient
@EnableFeignClients

创建一个service接口进行服务的调用

package com.cfh.eurekaconsumer.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient("hello-service")//指定调用的服务
@Service
public interface HelloService {

    @RequestMapping("/hello")//调用的服务的接口
    public String hello();
}

在controller中使用service服务(实际上使用的是远程服务)

package com.cfh.eurekaconsumer.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient("hello-service")
@Service
public interface HelloService {

    @RequestMapping("/hello")
    public String hello();
}

你可能感兴趣的:(spring,cloud)