初探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,就像调用本地方法一样,开发者完全感受不到这是在调用远程方法。

初探Spring Cloud Fegin (简单使用)_第1张图片

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

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