nacos服务注册与发现整合feign:webflux

客户端:feign

pom 配置

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

        
            org.springframework.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    

   
       
           
               org.springframework.cloud
               spring-cloud-dependencies
               Greenwich.RELEASE
               pom
               import
           
           
               org.springframework.cloud
               spring-cloud-alibaba-dependencies
               0.2.1.RELEASE
               pom
               import
           
       
   
application.yml 配置文件
spring:
  application:
    name: nacos-discovery-consumer-feign
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        metadata:
           name: lengleng

server:
  port: 8052
程序
-- application
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class NacosDiscoveryConsumerFeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosDiscoveryConsumerFeignApplication.class, args);
    }
}
-- 声明远程服务接口
@FeignClient("nacos-discovery-provider")
public interface DemoFeignService {
    @GetMapping("/demo")
    String demo(@RequestParam("name") String name);
}
-- controller 远程服务调用
@RestController
public class DemoController {
    @Autowired
    private DemoFeignService demoFeignService;

    @GetMapping("/test")
    public String test(String name) {
        return demoFeignService.demo(name);
    }
}

客户端:webflux

pom 配置
//不能使用boot-web-start

        
            org.springframework.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
        
            org.springframework.boot
            spring-boot-starter-webflux
        
    
    

   
       
           
               org.springframework.cloud
               spring-cloud-dependencies
               Greenwich.RELEASE
               pom
               import
           
           
               org.springframework.cloud
               spring-cloud-alibaba-dependencies
               0.2.1.RELEASE
               pom
               import
           
       
   
application.yml 配置文件
spring:
  application:
    name: nacos-discovery-consumer-webflux
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        metadata:
           name: lengleng
server:
  port: 8053
程序
-- application
@EnableDiscoveryClient
@SpringBootApplication
public class NacosDiscoveryConsumerWebFluxApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosDiscoveryConsumerWebFluxApplication.class, args);
    }
    @Bean
    @LoadBalanced//支持负载均衡
    public WebClient.Builder webClientBuilder() {
        return WebClient.builder();
    }
}
-- controller
@RestController
public class DemoController {
    @Autowired
    private WebClient.Builder webclientBuilder;
    @GetMapping("/test")
    public Mono test(String name) {
        return webclientBuilder.build()
                .get()
                .uri("http://nacos-discovery-provider/demo?name=" + name)
                .retrieve()
                .bodyToMono(String.class);
    }
}

服务端

pom配置同上
application.yml配置
server:
  port: 8051

spring:
  application:
    name: nacos-discovery-provider
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        metadata:
          name: provider
程序
@EnableDiscoveryClient
@SpringBootApplication
public class NacosDiscoveryProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosDiscoveryProviderApplication.class, args);
    }
}
@RestController
public class DemoController {
    @Value("${server.port}")
    private Integer port;
    @GetMapping("/demo")
    public String demo(String name) {
        return "hello " + name + port;
    }
}

测试

curl http://nacos-discovery-provider/demo?name=xxx

你可能感兴趣的:(nacos服务注册与发现整合feign:webflux)