spring cloud 之 openFeign

Feign和openFeign

Feign

Fegin使java Http客户端更加方便简洁,

Feign集成了Ribbon、RestTemplate实现了负载均衡的执行Http调用,只不过对原有的方式(Ribbon+RestTemplate)进行了封装,开发者不必手动使用RestTemplate调服务,而是定义一个接口,在这个接口中标注一个注解即可完成服务调用,这样更加符合面向接口编程的宗旨,简化了开发

openFeign 

底层使用的是java的URL connection作为默认http客户端,不支持连接池,建议使用HttpClient,支持连接池

openFeign是springcloud在Feign的基础上支持了SpringMVC的注解,如@RequestMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,并交给spring容器管理,实现类中做负载均衡并调用其他服务,调用的时候非常简单便捷,只需要注入Bean即可,这里的动态代理指的是jdk动态代理,因为是基于接口的http代理调用

环境搭建

这里注册中心使用nacos作为注册中心

建立被调用服务并注册nacos

配置文件

spring:
  application:
    name: cloud-test-service
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

controller

package com.slbuildenv.cloud.controller;

import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
@RequestMapping("/find")
@RefreshScope
public class TestFeign {


    @GetMapping("/date")
    public String getDate(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }
}

建立调用服务并注册nacos

添加依赖

openFeign有条件注解判断是否引入Httpclient,如果引入此依赖,则使用Httpclient,默认无需配置,默认值就是开启

spring cloud 之 openFeign_第1张图片  


        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
         
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
        
        
            io.github.openfeign
            feign-httpclient
        
        
            org.springframework.boot
            spring-boot-starter-web
        
 

 配置文件

spring:
  application:
    name: cloud-feign-client
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
server:
  port: 9300
feign:
  httpclient:
    connection-timeout: 5000

client接口

package com.slbuildenv.cloud.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient("cloud-test-service")
public interface TestFeign {

    @GetMapping("/find/date")
    String findDate();
}

contoller调用

package com.slbuildenv.cloud.controller;

import com.slbuildenv.cloud.client.TestFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/feign")
public class TestFeignRemote {

    @Autowired
    private TestFeign testFeign;

    @GetMapping("/date")
    public String getRemoteDate(){
       return  testFeign.findDate();
    }
}

启动类加开启feign的客户端注解

package com.slbuildenv.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;

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

项目结构

spring cloud 之 openFeign_第2张图片

查看Nacos注册情况及测试

查看服务注册情况

spring cloud 之 openFeign_第3张图片

 发起请求测试feign调用 http://localhost:9300/feign/date

spring cloud 之 openFeign_第4张图片 

 feign还有整合Hystrix和sentinel做熔断升级,在Hystrix和sentinel中介绍

你可能感兴趣的:(springCloud,spring,cloud,spring,后端)