栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringCloud: 使用WebClient调用Eureka中的微服务

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringCloud: 使用WebClient调用Eureka中的微服务

1.加入webflux依赖,完整pom.xml如下:



    4.0.0

    cn.edu.tju
    springcloudloadbalancer
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.7.RELEASE
         
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.SR12
                pom
                import
            
        
    

    
        
            org.springframework.boot
            spring-boot-starter-web
            
                
                    org.slf4j
                    slf4j-log4j12
                
            
        





        
            com.google.code.gson
            gson
            2.8.6
        

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        

        
            org.springframework.cloud
            spring-cloud-starter-loadbalancer
        

        
            mysql
            mysql-connector-java
            5.1.30
        





        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
            2.1.0.RELEASE


        

        
        
            org.springframework.boot
            spring-boot-starter-webflux
            2.3.7.RELEASE
        





    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



2.创建配置类:

package cn.edu.tju.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;

@Configuration
public class WebClientConfig {
    @Bean
    @LoadBalanced
    public WebClient.Builder loadBalancedWebClientBuilder() {
        return WebClient.builder();
    }
}

3.在controller中注入配置类,并调用其builder()方法产生WebClient对象,然后调用微服务并返回结果

package cn.edu.tju.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@RestController
public class ReactiveController {
    @Autowired
    private WebClient.Builder webClientBuilder;
    @RequestMapping("/react")
    public Mono react(){
        System.out.println("in react controller!");
        WebClient webClient=webClientBuilder.build();
        Mono stringMono = webClient.get().uri("http://demoservice/test").retrieve().bodyToMono(String.class);
        return stringMono;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/724314.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号