玩转SpringCloud(F版本) 二.服务消费者(2)feign

上一篇博客讲解了服务消费者的ribbon+restTemplate 模式的搭建,此篇文章将要讲解服务消费者feign模式的搭建,这里是为了普及知识 平时的项目中两种消费模式选择其一便可html

 

本篇博客基于博客 玩转SpringCloud 一.服务的注册与发现(Eureka) 中的项目为基础  : https://www.cnblogs.com/lsy131479/p/9613755.html  web

 

2. feign

 

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

 

简而言之:浏览器

 

· Feign 采用的是基于接口的注解架构

 

· Feign 整合了ribbon,具备负载均衡的能力app

 

· 整合了Hystrix,具备熔断的能力负载均衡

 

老套路:函数

启动demo1,端口为8761; 启动demo2两次,端口分别为8762 、8773.spring-boot

 

 

新建一个spring-boot工程,post

 

取名为demo4

项目架构:

 

 

在它的pom文件引入Feign的起步依赖spring-cloud-starter-feign、Eureka的起步依赖spring-cloud-starter-netflix-eureka-client、Web的起步依赖spring-boot-starter-web,

<parent>
   <groupId>com.fsdm</groupId>
   <artifactId>SpringCloud_test1</artifactId>
   <version>1.0-SNAPSHOT</version>
</parent>
   <dependencies>
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-openfeign</artifactId>
       </dependency>
   </dependencies>

在工程的配置文件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接口

经过@ FeignClient(“服务名”),来指定调用哪一个服务。好比在代码中调用了service-hi服务的“/hi”接口

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

注解解析:

 

@FeignClient

 

1. feign模式来指定调用哪一个服务

 

 

 

@RequestMapping

 

1.类定义处: 提供初步的请求映射信息。相对于 WEB 应用的根目录;

 

2.方法处: 提供进一步的细分映射信息。 相对于类定义处的 URL。

 

3.若类定义处未标注 @RequestMapping,则方法处标记的 URL相对于 WEB 应用的根目录

 

4.返回ModelAndView时的url会根据你的 @RequestMapping实际状况组成。

 

5.若是类上没有映射,那么url直接就是方法的映射;不然url为类上+方法上映射路径组合。

 

 

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

@RestController  
public class HiController {
    //编译器报错,无视。 由于这个Bean是在程序启动的时候注入的,编译器感知不到,因此报错。
    @Autowired
    SchedualServiceHi schedualServiceHi;

    @GetMapping(value = "/hi")
    public String sayHi(@RequestParam String name) {
        return schedualServiceHi.sayHiFromClientOne( name );
    }

}

注解解析:

@Autowired


1.能够对成员变量、方法和构造函数进行标注,来完成自动装配的工做

1. 能够对成员变量、方法和构造函数进行标注,来完成自动装配的工做

 

@GetMapping

1. Spring4.3中引进

2. 来帮助简化经常使用的HTTP方法的映射,并更好地表达被注解方法的语义。

3.  @GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写。该注解将HTTP Get 映射到 特定的处理方法上。

 

Application启动类:

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class Demo4Application {

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

}

注解解析:

@EnableFeignClients

1. 指定为feign角色

 

 

启动程序,屡次访问http://localhost:8765/hi?name=fsdm,浏览器交替显示:

 

 

 

 

                        未完,待续。。。

相关文章
相关标签/搜索