zoukankan      html  css  js  c++  java
  • springCloud4---feign

    JAX-WS : soap是遵循这个标准。

    JAX-RS : 标准,jersey框架遵循这个标准。

    Feign是一个声明式的web service客户端。Feign使用的负载均衡也是ribbonFeign整合了RibbonEureka

    package com.itmuch.cloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.cloud.netflix.feign.EnableFeignClients;
    
    @SpringBootApplication
    @EnableEurekaClient
    @EnableFeignClients  //Feign的客户端
    public class ConsumerMovieFeignApplication {
      public static void main(String[] args) {
        SpringApplication.run(ConsumerMovieFeignApplication.class, args);
      }
    }
    package com.itmuch.cloud.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.itmuch.cloud.entity.User;
    import com.itmuch.cloud.feign.UserFeignClient;
    
    @RestController
    public class MovieController {
    
      @Autowired
      private UserFeignClient userFeignClient;
    
    /*  原来是通过restTemplate来调用
      @GetMapping("/movie/{id}")
      public User findById(@PathVariable Long id) {
            return this.restTemplate.getForObject(this.userServicePath + id, User.class);
      }*/
      
      @GetMapping("/movie/{id}")
      public User findById(@PathVariable Long id) {
        return this.userFeignClient.findById(id);
      }
    
      @GetMapping("/test")
      public User testPost(User user) {
        return this.userFeignClient.postUser(user);
      }
    
      @GetMapping("/test-get")
      public User testGet(User user) {
        return this.userFeignClient.getUser(user);
      }
    }
    package com.itmuch.cloud.feign;
    
    import org.springframework.cloud.netflix.feign.FeignClient;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    import com.itmuch.cloud.entity.User;
    
    @FeignClient("microservice-provider-user")  //请求microservice-provider-user服务
    public interface UserFeignClient {
      @RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)//microservice-provider-user服务的/simple/{id}
      public User findById(@PathVariable("id") Long id); // 两个坑:1. @GetMapping不支持   2. @PathVariable得设置value
    
      @RequestMapping(value = "/user", method = RequestMethod.POST) //post对象
      public User postUser(@RequestBody User user);
    
      // 该请求不会成功,只要参数是复杂对象,即使指定了是GET方法,feign依然会以POST方法进行发送请求。可能是我没找到相应的注解或使用方法错误。
      // 如勘误,请@lilizhou2008  eacdy0000@126.com
      @RequestMapping(value = "/get-user", method = RequestMethod.GET)
      public User getUser(User user);
    }
    spring:
      application:
        name: microservice-consumer-movie  #本服务的名字
    server:
      port: 7905
    eureka:
      client:
        healthcheck:
          enabled: true
        serviceUrl:
          defaultZone: http://user:password123@localhost:8761/eureka  #注册到的eureka的地址
      instance:
        prefer-ip-address: true
    <?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>
    
        <artifactId>microservice-consumer-movie-feign</artifactId>
        <packaging>jar</packaging>
    
        <parent>
            <groupId>com.itmuch.cloud</groupId>
            <artifactId>microservice-spring-cloud</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
            <!-- 引入feign依赖 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-feign</artifactId>
            </dependency>
        </dependencies>
    </project>

    复写feign的默认配置,

    package com.itmuch.cloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.cloud.netflix.feign.EnableFeignClients;
    
    @SpringBootApplication
    @EnableEurekaClient
    @EnableFeignClients
    //默认只扫描这个类所在的包和子包
    public class ConsumerMovieFeignApplication {
      public static void main(String[] args) {
        SpringApplication.run(ConsumerMovieFeignApplication.class, args);
      }
    }
    package com.itmuch.cloud.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.itmuch.cloud.entity.User;
    import com.itmuch.cloud.feign.FeignClient2;
    import com.itmuch.cloud.feign.UserFeignClient;
    
    @RestController
    public class MovieController {
    
      @Autowired
      private UserFeignClient userFeignClient;
    
      @Autowired
      private FeignClient2 feignClient2;
    
      @GetMapping("/movie/{id}")
      public User findById(@PathVariable Long id) {
        return this.userFeignClient.findById(id);
      }
    
      @GetMapping("/{serviceName}")
      public String findServiceInfoFromEurekaByServiceName(@PathVariable String serviceName) {
        return this.feignClient2.findServiceInfoFromEurekaByServiceName(serviceName);
      }
    }
    package com.itmuch.cloud.feign;
    
    import org.springframework.cloud.netflix.feign.FeignClient;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import com.itmuch.config.Configuration2;
    
    //Configuration2作为他的配置文件
    @FeignClient(name = "xxxx", url = "http://localhost:8761/", configuration = Configuration2.class)
    public interface FeignClient2 {
      @RequestMapping(value = "/eureka/apps/{serviceName}")
      public String findServiceInfoFromEurekaByServiceName(@PathVariable("serviceName") String serviceName);
    }
    package com.itmuch.cloud.feign;
    
    import org.springframework.cloud.netflix.feign.FeignClient;
    
    import com.itmuch.cloud.entity.User;
    import com.itmuch.config.Configuration1;
    
    import feign.Param;
    import feign.RequestLine;
    
    //Configuration1作为他的配置文件
    @FeignClient(name = "microservice-provider-user", configuration = Configuration1.class)
    public interface UserFeignClient {
      @RequestLine("GET /simple/{id}")
      public User findById(@Param("id") Long id);
    }
    package com.itmuch.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import feign.Contract;
    import feign.Logger;
    
    @Configuration
    public class Configuration1 {
      @Bean
      public Contract feignContract() {
        return new feign.Contract.Default();
      }
    
      @Bean
      Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
      }
    }
    package com.itmuch.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import feign.auth.BasicAuthRequestInterceptor;
    
    @Configuration
    public class Configuration2 {
      @Bean
      public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
        return new BasicAuthRequestInterceptor("user", "password123");
      }
    }
    spring:
      application:
        name: microservice-consumer-movie
    server:
      port: 7901
    eureka:
      client:
        healthcheck:
          enabled: true
        serviceUrl:
          defaultZone: http://user:password123@localhost:8761/eureka
      instance:
        prefer-ip-address: true
    logging:
      level:
        com.itmuch.cloud.feign.UserFeignClient: DEBUG   #日志是debug级别
    
    # 解决第一次请求报超时异常的方案:
    # hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
    # 或者:
    # hystrix.command.default.execution.timeout.enabled: false
    # 或者:
    feign.hystrix.enabled: false ## 索性禁用feign的hystrix
    
    # 超时的issue:https://github.com/spring-cloud/spring-cloud-netflix/issues/768
    # 超时的解决方案: http://stackoverflow.com/questions/27375557/hystrix-command-fails-with-timed-out-and-no-fallback-available
    # hystrix配置: https://github.com/Netflix/Hystrix/wiki/Configuration#execution.isolation.thread.timeoutInMilliseconds
    <?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>
    
        <artifactId>microservice-consumer-movie-feign-customizing</artifactId>
        <packaging>jar</packaging>
    
        <parent>
            <groupId>com.itmuch.cloud</groupId>
            <artifactId>microservice-spring-cloud</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-feign</artifactId>
            </dependency>
        </dependencies>
    </project>
  • 相关阅读:
    【openCV学习笔记】【2】读取并播放一段视频
    Net WebApi中使用Swagger
    .pfx格式和.Cer格式的区别
    数字证书原理
    ASP.NET发送电子邮件
    WindowsAzure上把WebApp和WebService同时部署在一个WebRole中
    JS原型与原型链终极详解
    三、ASP.NET MVC Controller 控制器(二:IController控制器的创建过程)
    二、ASP.NET MVC Controller 控制器(一:深入解析控制器运行原理)
    一、ASP.NET Routing路由(深入解析路由系统架构原理)
  • 原文地址:https://www.cnblogs.com/yaowen/p/9129068.html
Copyright © 2011-2022 走看看