声明式服务调用fegin

一、简介

Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Spring Cloud Feign 基于Netflix Feign 实现的,整合了Spring Cloud Ribbon 与 Spring Cloud Hystrix,并且实现了声明式的Web服务客户端定义方式。

简而言之:

  • Feign 采用的是基于接口的注解

  • Feign 整合了ribbon

二、创建一个feign的服务

创建完pom文件修改如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
​
    <groupId>com.vesus</groupId>
    <artifactId>springcloud-fegin</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
​
    <name>springcloud-fegin</name>
    <description>Demo project for Spring Boot</description>
​
    <parent>
        <groupId>com.vesus</groupId>
        <artifactId>springcloud-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
​
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- eureka:微服务注册 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <!--fegin-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
​
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2、创建application.yml配置文件,增加注册中心地址

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/ # 注册中心地址
spring:
  application:
    name: spring-cloud-fegin
server:
  port: 8764

3、定义一个接口HelloService,通过@ FeignClient(“服务名”),来指定调用哪个服务。

package com.vesus.springcloudfegin.service;
​
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
​
@FeignClient(value = "spring-cloud-service")
//springcloud-service中定义的服务的名字
public interface HelloService {
​
    @RequestMapping(value = "/hello") //springcloud-service中定义的服务方法
    public String sayHello() ;
}

4、加入控制器HelloWordController

package com.vesus.springcloudfegin.controller;
​
​
import com.vesus.springcloudfegin.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
​
@RestController
public class HelloController {
​
    @Autowired
    HelloService helloService ;
​
    @RequestMapping(value = "/hello")
    public String sayHello(){
        return  helloService.sayHello() ;
    }
}
​

5、入口方法加入@EnableFeignClients,增加fegin支持。

package com.vesus.springcloudfegin;
​
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
​
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class SpringcloudFeginApplication {
​
    public static void main(String[] args) {
        SpringApplication.run(SpringcloudFeginApplication.class, args);
    }
}

启动工程,访问 http://localhost:8764/hello ,显示hello world 。

代码 :https://gitee.com/vesus198/springcloud-demo/tree/master/springcloud-fegin

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值