zoukankan      html  css  js  c++  java
  • Spring cloud Feign 入门demo

    spring Feign (服务与服务调用工具)
        添加Feign依赖:
                <dependency>  
                    <groupId>org.springframework.cloud</groupId>  
                    <artifactId>spring-cloud-starter-feign</artifactId>  
                </dependency>
        
        application.properties中添加配置,超时时间设置和服务提供方的地址和端口号
        #Feign
        ribbon.ConnectTimeout=3000
        ribbon.ReadTimeout=60000
        admin.ribbon.listOfServers=192.16.150.130:8082
        
        启动类上要加@EnableFeignClients注解
        
        注意:如果使用的spring cloud版本比较新,在新版本中Feign对Hystrix的支持是默认关闭的,所以需要通过配置手动打开
              feign.hystrix.enabled=true,这样服务降级等功能才有效果。
        
        启动程序:
            @SpringBootApplication
            @EnableFeignClients
            public class MovCenterBootstrap {
                public static void main(String[] args) {
                    SpringApplication.run(MovCenterBootstrap.class, args);
                }
            }
        
        @EnableFeignClients注解的作用:通过@EnableFeignCleints注解开启FeignCleint(在启动项目的时候会检查启动类上有没有@EnableFeignClients注解,如果有该注解,则开启包扫描,扫描被@FeignClient注解的接口并将这些信息注入到ioc容器中)
        
        服务调用方Client代码:
            @Component
            @FeignClient(url = "http://localhost:8010", name = "user")
            public interface UserClient {
            
                @RequestMapping(method = RequestMethod.POST,value="/user/findOne",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
                Object findOne(@RequestParam("userId") Integer userId);
            }

            @FeignClient注解定义了该接口是一个Feign客户端,name指定了注册到Eureka上的服务名。
            @RequestMapping里指定了请求的相对url和http请求方式,与服务端一一对应。入参里的@RequestParam、@RequestBody、@RequestHeader注解比起服务端多了value属性,这里不能省略,需要显式的告知Feign客户端参数要如何对应
        
        服务调用方Controller代码:
            @RequestMapping("user")
            @RestController
            public class UserController {

                @Autowired
                private UserClient userClient;
        
                @PostMapping("findOne")
                @ApiOperation(value="查找单个用户", notes="根据id查询单个用户")
                @ApiImplicitParam(name="userId",value="userId",required=true,paramType="query")
                public Object findOne(@RequestParam("userId") Integer userId){
                    Object result = userClient.findOne(userId);
                    return result;
                }
            }
        
        spring cloud Feign 超时问题:默认的请求时间为1秒,超过这个时间便超时异常,在配置文件中添加如下配置可以解决超时问题,即把超时的时间设长
        application.yml服务调用方配置:hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
        
        
        服务提供方Controller代码:
            @RestController
            @RequestMapping("/user")
            public class UserController {

                @Autowired
                UserService userService;
                @PostMapping("/findOne")
                @ApiOperation(value="查询单个用户",notes="根据id查询单个用户信息")
                @ApiImplicitParam(name="userId",value="userId",required=true,paramType="query")
                public ObjectRestResponse<User> findOne(@RequestParam Integer userId){
                    ObjectRestResponse<User> result = new ObjectRestResponse<User>();
                    User user = userService.selectById(userId);
                    result.data(user);
                    return result;
                }

            }


    总结:Feign的源码实现的过程如下:

    • 首先通过@EnableFeignCleints注解开启FeignCleint
    • 根据Feign的规则实现接口,并加@FeignCleint注解
    • 程序启动后,会进行包扫描,扫描所有的@ FeignCleint的注解的类,并将这些信息注入到ioc容器中。
    • 当接口的方法被调用,通过jdk的代理,来生成具体的RequesTemplate
    • RequesTemplate在生成Request
    • Request交给Client去处理,其中Client可以是HttpUrlConnection、HttpClient也可以是Okhttp
    • 最后Client被封装到LoadBalanceClient类,这个类结合类Ribbon做到了负载均衡。

    原文地址:https://blog.csdn.net/weixin_39702323/article/details/80505908
  • 相关阅读:
    @media screen针对不同移动设备-响应式设计
    闭包的一些例子
    es6 新关键字const
    es6 新关键字let
    Unity 多屏(分屏)显示,Muti_Display
    小米手机常用操作
    Charles使用笔记
    AKKA学习笔记
    Gatling-Session
    Scala学习笔记-6-其他
  • 原文地址:https://www.cnblogs.com/jpfss/p/12107144.html
Copyright © 2011-2022 走看看