spring cloud fegin入门

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

pom.xml

        <!-- hystrix -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </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();
}

猜你喜欢

转载自blog.csdn.net/m0_37556444/article/details/82561589