zoukankan      html  css  js  c++  java
  • spring cloud服务间调用feign

    参考文章:Spring Cloud Feign设计原理

    1.feign是spring cloud服务间相互调用的组件,声明式、模板化的HTTP客户端。类似的HttpURLConnection、Apache HttpComponnets、OkHttp3 、Netty都实现相同功能。

    目录结构

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.test</groupId>
        <artifactId>feigtest</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>feigtest</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
            <fastjson.version>1.2.32</fastjson.version>
    
        </properties>
    
        <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-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--feign -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
            <!--引入这个包才能使用fegin-->
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
            </dependency>
           
            <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
    
    
    </project>

    application.yml

    spring:
      application:
        name: service-feigntest
    server:
      port: 8702
    eureka:
      client:
        serviceUrl:
          #指向注册中心
          defaultZone: http://192.168.111.133:8888/eureka/
      instance:
        # 每间隔1s,向服务端发送一次心跳,证明自己依然”存活“
        lease-renewal-interval-in-seconds: 1
        # 告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,将我踢出掉。
        lease-expiration-duration-in-seconds: 2

    IHiService.java

    package com.feigtest;
    
    
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.PostMapping;
    
    @FeignClient("service-hi")
    public interface IHiService {
        @PostMapping("/hi")
        public String say();
    }
    FeigTestController.java
    package com.feigtest;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    
    @RestController
    public class FeigTestController {
        @Autowired
        IHiService hiService;
    
        @RequestMapping("/hifeign")
        public String getFeignService(){
            return hiService.say();
        }
    }
    FeigtestApplication
    package com.feigtest;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    @EnableEurekaClient
    @SpringBootApplication
    @EnableFeignClients
    public class FeigtestApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(FeigtestApplication.class, args);
        }
    
    }

     2.feign带token,服务调用时保持token不丢失。

    package com.Interceptor;
    
    import feign.RequestInterceptor;
    import feign.RequestTemplate;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    
    import javax.servlet.http.HttpServletRequest;
    
    /**
     * Feign配置
     * 使用FeignClient进行服务间调用,传递headers信息
     */
    @Configuration
    public class FeignConfig implements RequestInterceptor {
        @Override
        public void apply(RequestTemplate requestTemplate) {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            //添加token
            requestTemplate.header("token", request.getHeader("token"));
        }
    }


  • 相关阅读:
    央视好节目整理
    IT项目各阶段管理
    使用mint-ui Loadmore组件时出现报错
    简单了解map,filter,some,every,forEach,for in,for of,find,用法
    h5简单学习总结
    video标签详解(转载)
    浏览器对象
    获取后三天的时间
    Map和Set以及iterable类型集合的循环遍历
    placeholder的样式设置
  • 原文地址:https://www.cnblogs.com/pu20065226/p/11024700.html
Copyright © 2011-2022 走看看