springcloud学习笔记(五)SpringCloud集成Feign(1)

声明性REST客户端:Feign

对于feign,springCloud官方如是说:

Feign是一个声明式的Web服务客户端。这使得Web服务客户端的写入更加方便 要使用Feign创建一个界面并对其进行注释。它具有可插入注释支持,包括Feign注释和JAX-RS注释。Feign还支持可插拔编码器和解码器。Spring Cloud增加了对Spring MVC注释的支持,并使用Spring Web中默认使用的HttpMessageConvertersSpring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端。

下面一个示例更好的理解feign:

使用三个测试项目:

先启动服务器和提供接口的客户端:


在eureka可以看到发现了一个微服务,在这个客户端有一个接口:


好,准备工作做完,开始写feign:

首先加入依赖

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
完整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">
    <parent>
        <artifactId>SpringCloudTest</artifactId>
        <groupId>com.xc.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>EurekaService_feign</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

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

    </dependencies>

</project>
启动类加入注解 @EnableFeignClients
完整如下:

package com.xc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class App_feign {
    public static void main(String args[]){
        SpringApplication.run(App_feign.class);
    }
}

配置文件 改动端口以及微服务名称:

完整如下:

spring:
  application:
    name: test_feign
server:
  port: 7777
eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://xc:123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
下面写一个feign接口:

package com.xc.feign;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient("cloud-test")
public interface TestFeign {
    @RequestMapping(value = "/say/{words}",method = RequestMethod.GET)
    public String sayHello(@PathVariable("words") String words);
}
其中 @FeignClient("cloud-test") 中的参数为eureka中的名称 也可修改为@FeignClient(url="localhost:7070")

此处接口应与cloud-test当中 要访问的接口一致。

然后写一个rest接口:

package com.xc.controller;

import com.xc.feign.TestFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

         @Autowired
        private TestFeign testFeign;

        @RequestMapping(value="/say/{word}",method = RequestMethod.GET)
        public String say(@PathVariable String word){
            return testFeign.sayHello(word);
        }

}

ok,开始测试:


访问http://192.9.8.3:7777/say/haha


成功获取到数据。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值