IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

SpringCloud教程(三)声明式访问Feign、负载均衡Ribbon

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

0-前言

  eureka实际上已经集成了负载均衡调度框架Ribbon;

  我们有了各个微服务了,那怎么来调用他们呢,一种方法是可以使用 RestTemplate(如:String str= restTemplate.getForObject(“http://localhost:8763/user/hello”,String.class);

使用RestTemplate的话要使用负载均衡调度(ribbon)需要增加@LoadBalanced注解,如:

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() 
{
       return new RestTemplate();
   }

但是,我们一般不会这样用,我们更希望像调用本地方法一样来调用其他微服务;这时候,Feign就粉末登场了

  Feign:声明式服务调用,它使得我们调用其他微服务,就像调用本地方法一样;它默认使用Ribbon实现负载均衡;

1-Feign的使用

1、1、添加依赖

        <!-- 声明式调用 feign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

1、2、添加接口

  其中name实需要调用的已经注册到eureka的微服务的名称;方法跟要调用的微服务方法一致,只包含声明,不包含实现

package com.anson.service.feign;

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

@FeignClient(name = "user")
public interface FUserService
{
    @RequestMapping("/user/hello")
    public  String hello();
}

1、3、调用

 @Autowired
    private FUserService fUserService;

   @RequestMapping("/order2")
    public  String order2()
    {
        return fUserService.hello();

    }

是不是就像调用本地方法一样简单了;

1.4、运行测试

我们把eureka server项目、user项目(服务提供者)、order项目(服务消费者)依次启动;其中user启动了两个实例,

111_1.png

然后运行 http://localhost:8766/order/order2 ,多刷新几次

111_2.png

111_3.png

可以看到,Feign已成功执行,并且通过默认使用的Ribbon实现了负载均衡;

简单使用就这么简答,更深入的后续再详聊,GIT源码后续再放出

文章永久链接:https://tech.souyunku.com/?p=31819


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(86) 打赏



未经允许不得转载:搜云库技术团队 » SpringCloud教程(三)声明式访问Feign、负载均衡Ribbon

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367