Spring Cloud服务的提供者与消费者(Feign)

一、Feign简介

Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。

在工程的配置文件application.yml文件,指定程序名为service-feign,端口号为8765,服务注册地址为http://localhost:8761/eureka/ ,代码如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8765
spring:
  application:
    name: service-feign

开启Feign,在启动类上加入注解@EnableFeignClients

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

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

比如在代码中调用了service-hi服务的“/hi”接口,代码如下:

@FeignClient(value = "service-hi")
public interface SchedualServiceHi {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

在Web层的controller层,对外暴露一个”/hi”的API接口,通过上面定义的Feign客户端SchedualServiceHi 来消费服务。代码如下:

@RestController
public class HiController {

    @Autowired
    SchedualServiceHi schedualServiceHi;
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    public String sayHi(@RequestParam String name){
        return schedualServiceHi.sayHiFromClientOne(name);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_756589808/article/details/81983106