SpringCloudFeign

编程

Feign集成了Ribbon,利用Ribbon维护了MicroServiceCloud-Dept的服务列表信息,并且通过轮询实现了客户端的负载均衡。而与Ribbon不同的是,通过feign只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现了服务调用。

 集成feign

1 pom文件添加feign支持

   <dependency>

       <groupId>org.springframework.cloud</groupId>

       <artifactId>spring-cloud-starter-feign</artifactId>

   </dependency>

2 注解接口服务 @FeignClient声明

package com.atguigu.springcloud.service;

import java.util.List;

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;

import com.atguigu.springcloud.entities.Dept;

@FeignClient(value = "MICROSERVICECLOUD")

public interface DeptClientService

{

  @RequestMapping(value = "/dept/get/{id}",method = RequestMethod.GET)

  public Dept get(@PathVariable("id") long id);

  @RequestMapping(value = "/dept/list",method = RequestMethod.GET)

  public List<Dept> list();

  @RequestMapping(value = "/dept/add",method = RequestMethod.POST)

  public boolean add(Dept dept);

}

 

3 消费服务者添加feign支持,@EnableFeignClients(basePackages= {"com.atguigu.springcloud"})

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(basePackages= {"com.atguigu.springcloud"})

@ComponentScan("com.atguigu.springcloud")

public class DeptConsumer80_Feign_App

{

  public static void main(String[] args)

  {

   SpringApplication.run(DeptConsumer80_Feign_App.class, args);

  }

}

 

以上是 SpringCloudFeign 的全部内容, 来源链接: utcz.com/z/513332.html

回到顶部