初探Spring Cloud Fegin (简单使用)

Fegin是一个声明式的web service 客户端。使用Fegin只需要在接口上加上注解就好了,方便快捷。Feign具有可插拔的注解特性,包括Feign 注解和JAX-RS注解,同时增加了对 Spring MVC的注解,Spring Web 默认使用了HttpMessageConverters, Spring Cloud 集成 Ribbon 和 Eureka 提供的负载均衡的HTTP客户端 Feign。在Spring Cloud中使用Fegin,就像调用本地方法一样,开发者完全感受不到这是在调用远程方法。

eureka-fegin、eureka-fegin-service和eureka-fegin-service-tmp 三个服务

eureka-fegin 通过fegin 调用eureka-fegin-service和eureka-fegin-service-tmp提供的服务

eureka-fegin-service和eureka-fegin-service-tmp的配置文件application.yml(服务端口记得不能一样)

server:
  port: 8070
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8080/eureka/
spring:
  application:
    name: eureka-fegin-service

提供服务的接口:

package com.cn.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FeginServiceController {

    @RequestMapping("/getName")
    public String getName(@RequestParam String name){
        return "hello1 : " + name;
    }
}

依赖:

dependencies {
    compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
}

启动类:

package com.cn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class FeginServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(FeginServiceApplication.class, args);
    }
}

eureka-fegin的配置文件application.yml

server:
  port: 8088
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8080/eureka/
spring:
  application:
    name: eureka-fegin

FeginService接口:

package com.cn.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "eureka-fegin-service")
public interface FeginService {

    @RequestMapping(value = "/getName", method = RequestMethod.GET)
    public String getName(@RequestParam("name") String name);
}

PS:记得添加FeginClient注解,value值为需要调用的服务(eureka-fegin-service和eureka-fegin-service-tmp的配置文件application.yml中配置的spring.application.name=eureka-fegin-service)的应用名称。

package com.cn.controller;

import com.cn.service.FeginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FeginController {
    @Autowired
    private FeginService feginService;
    @RequestMapping(value = "/getName")
    public String search(@RequestParam("name") String name){
        return feginService.getName(name);
    }
}

启动类:

package com.cn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeginApplication {
    public static void main(String[] args) {
        SpringApplication.run(FeginApplication.class, args);
    }
}
下面两个注解一定不要忘记添加
EnableFeignClients:开启Fegin的使用
EnableDiscoveryClient:开启服务发现

访问:http://localhost:8070/getName?name=bob

页面输出:hello1 : bob

访问:http://localhost:8060/getName?name=bob

页面输出:hello2 : bob

访问:http://localhost:8088/getName?name=bob

页面会循环输出:hello1 : bob和hello2 : bob

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值