java技术--SpringCloud:Feign代码实现(10)

1.Feign概念回顾:简介

(1)Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单
(2)使用Feign,只需要创建一个接口并注解
(3)Feign具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解
(4)Feign支持可插拔的编码器和解码器
(5)Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果
(6)概括:
    <1>Feign 采用的是基于接口的注解
    <2>Feign 整合了ribbon,具有负载均衡的能力
    <3>整合了Hystrix,具有熔断的能力

2.准备工作

(1)继续用前面创建好的工程
(2)启动EurekaServer,端口为8000;
(3)启动EurekaClient两次,端口分别为8001 、8002

3.创建一个Feign的服务

1)新建一个spring-boot工程,取名为FeignServer
(2)创建pom.xml配置文件添加依赖:
   <project xmlns="http://maven.apache.org/POM/4.0.0"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
            http://maven.apache.org/xsd/maven-4.0.0.xsd">   
	<modelVersion>4.0.0</modelVersion>
	<artifactId>FeignServer</artifactId>
	<packaging>jar</packaging>
	<parent>
		<groupId>com.bisien</groupId>
		<artifactId>SpringCloud-Parent</artifactId>
		<version>0.0.1-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>
</project>3)创建application.properties配置文件
    #服务端口
    server.port=8004
    eureka.instance.hostname=localhost
    #指定服务的注册中心EurekaServer地址
    eureka.client.service- url.defaultZone=http://${eureka.instance.hostname}:8000/eureka/
    #指定服务名
    spring.application.name:FeignServer
(4)编写程序的启动类FeignServiceApplication,代码示例如下:
    @SpringBootApplication
    @EnableEurekaClient
    // 通过@EnableDiscoveryClient指向服务中心注册
    @EnableDiscoveryClient
    // @EnableFeignClients注解开启Feign的功能
    @EnableFeignClients
    public class FeignServiceApplication {
	   public static void main(String[] args) {
		SpringApplication.run(FeignServiceApplication.class, args);}}5)编写Server层接口类,代码示例如下:
    /**
     * 通过@FeignClient(“服务名”),来指定调用哪个服务
     * 这里的服务名:就是EurekaClient配置文件application.properties中指定的服务名
     * 例如:spring.application.name:EurkaClient
     */
    @FeignClient(value = "EurkaClient")
    public interface IHelloFeignService {
	  @RequestMapping(value = "/hi",method = RequestMethod.GET)
	    String sayHiFromClientOne(@RequestParam(value = "name") String name);}6)编写Contorller层,代码示例如下:
   /**
    * 对外暴露一个”/hi”的API接口 
    * 通过上面定义的Feign客户端IHelloFeignService来消费服务
    */
     @RestController
     public class HelloFeignContorller {
        @Resource
	    private IHelloFeignService helloFeignService;
	    @GetMapping(value = "/hi")
	    public String sayHi(@RequestParam String name) {
		   return helloFeignService.sayHiFromClientOne(name);}}7)启动程序,多次访问http://localhost:8004/hi?name=张三,浏览器交替显示		        

4.Ribbon与Feign区别总结

(1)Ribbon和Feign都可以实现负载均衡功能
(2)Ribbon需要配合使用RestTemplate
   <1>在Service层,注入RestTemplate
   <2>在RestTemplate的getForObject()方法中指定已经注册到注册中心的服务实例
   <3>在Cintorller层调用即可
(3)Feign封装了Ribbon
   <1>使用Feign不需要注入RestTemplate
   <2>定义一个Service接口类
     2.1.在接口类上使用@FeignClient注解
     2.2.在@FeignClient注解中指明服务实例名称
     2.3.在方法上使用 @RequestMapping注解指明请求方式,请求接口
       2.3.1.接口中@RequestMapping注解的使用和Contorller中的使用一样
       2.3.2.接口方法中@RequestMapping必须存在
       2.3.3.如果没有该注解那么注册中心无法显示并且运行和请求报错
   <3>在Contorller层调用即可
     3.1.Contorller层调用注入接口类即可
     3.2.方法上使用@RequestMapping注解和接口类使用方式一样
     3.3.或者使用@GetMapping注解(适用于GET请求)      
发布了191 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq591009234/article/details/105709833