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

SpringCloud: 使用WebClient调用Eureka中的微服务,第1张

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;
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5719864.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存