初学者对spring cloud的声明式服务的调用理解

之前我们了解了spring Cloud 客户端负载均衡(Ribbon)和spring Cloud Hystrix(服务容错保护),接下来我们来看看更厉害的东东那就是spring cloud Feign,那么什么是Feign呢?

一、什么是Feign

首先Feign 是一个声明web服务客户端,整合了spring Cloud Ribbon 和spring Cloud Hystrix,除此之外,还具有可插拔的注解,这使得我们的客户端编写更加方便,结合了Eureka默认实现了负载均衡,接下来我们来看看快速入门。

二、Feign的入门使用

首先我们借助于之前的服务Consumer中添加Feign的依赖,代码如下:

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

接下来我们在启动类上贴上@EnableFeignClients,该标签表示开启Feigin,代码如下所示:

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class FeignConsumerApplication {

public static void main(String[] args) {
    SpringApplication.run(FeignConsumerApplication.class, args);
}

完成了一半了,接下来我们需要创建Fegin接口,我这里的接口是helloSerevice代码如下:

   mport com.sgcc.feignconsumer.domain.User;
   import org.springframework.cloud.openfeign.FeignClient;
   import org.springframework.web.bind.annotation.*;
   @FeignClient(value = "feign-provider")
    public interface HelloService {
  @RequestMapping("/hello")
   public String hello();

  @RequestMapping(value = "/hello1",method = RequestMethod.GET)
  public  String hello(@RequestParam("name") String name);
  @RequestMapping(value = "/hello2",method = RequestMethod.GET)
  public User hello(@RequestHeader("name") String name,@RequestHeader("age")int age);
  @RequestMapping(value = "/hello3",method = RequestMethod.POST)
  public String hello(@RequestBody User user);

}

这里是多个参数的接口,我们可以看到是接口上的@FeignClient(value = "feign-provider")是负载到了feign-provider服务上,我们来看看该服务的接口,代码如下:

 @RestController
 public class ProviderController {

@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String  hello(){
    return "hello provider";
}

@RequestMapping(value = "/hello1",method = RequestMethod.GET)
public String hello(@RequestParam String name){
    return "hello" +name;
}
@RequestMapping(value = "/hello2",method = RequestMethod.GET)
public User hello(@RequestHeader String name,@RequestHeader int age){
    return new User(name,age);
}
@RequestMapping(value = "/hello3",method = RequestMethod.POST)
public String hello(@RequestBody User user){
    return "hello" +user.getName()+","+user.getAge();
  }
}

在该服务中定义一个实体Bean User对象代码如下:

''''''
  import lombok.*;
  @Getter
  @Setter
  @AllArgsConstructor
  @NoArgsConstructor
  @ToString
  public class User {
  private String name;
  private int age;
  }

这是一个简单的Bean对象,接着看我们的Consumer服务的相关配置文件:

server.port=8000
spring.application.name=feign-consumer
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/

我们在来看看我们consumer的接口代码:

@RestController
public class ConsumerController {
@Autowired
private HelloService helloService;
@RequestMapping(value = "/feign" ,method = RequestMethod.GET)
public String helloConsumer(){

    return helloService.hello();
}
@RequestMapping(value = "/feign1",method = RequestMethod.GET)
public String helloConsumer2(){

    StringBuilder sb = new StringBuilder();
    sb.append(helloService.hello()).append("\n");
    sb.append(helloService.hello("sb")).append("\n");
    sb.append(helloService.hello("小黑",20)).append("\n");
    sb.append(helloService.hello(new User("小明",18))).append("\n");
    return sb.toString();
  }

}

分别启动我们的服务Eureka-server、feign-provider、feign-consumer如图所示:

微信截图_20190225181112.png

如图我们可以看到,我们的服务都注册了进去,接下来看结果,

微信截图_20190225181822.png

微信截图_20190225181933.png

有点乱码不好意思,基本的功能都实现了

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容