diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/pom.xml new file mode 100644 index 00000000..70487968 --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/pom.xml @@ -0,0 +1,63 @@ + + + + + com.alibaba.cloud + nacos-discovery-example + 2.2.0.BUILD-SNAPSHOT + + 4.0.0 + + + nacos-reactivediscovery-consumer-example + jar + Example demonstrating how to use nacos discovery + + + + + org.springframework.boot + spring-boot-starter-webflux + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + org.springframework.cloud + spring-cloud-starter-netflix-ribbon + + + + + + org.springframework.boot + spring-boot-starter-actuator + + + + org.springframework.cloud + spring-cloud-starter-loadbalancer + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-deploy-plugin + ${maven-deploy-plugin.version} + + true + + + + + + diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/src/main/java/com/alibaba/cloud/examples/ConsumerReactiveApplication.java b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/src/main/java/com/alibaba/cloud/examples/ConsumerReactiveApplication.java new file mode 100644 index 00000000..7da82f78 --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/src/main/java/com/alibaba/cloud/examples/ConsumerReactiveApplication.java @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.cloud.examples; + +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient; +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.context.annotation.Bean; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.reactive.function.client.WebClient; + +/** + * @author Jim + */ +@SpringBootApplication +public class ConsumerReactiveApplication { + + @Bean + @LoadBalanced + public WebClient.Builder webClient() { + return WebClient.builder(); + } + + @RestController + class MyController { + @Autowired + private ReactiveDiscoveryClient reactiveDiscoveryClient; + + @Autowired + private WebClient.Builder webClientBuilder; + + @GetMapping("/all-services") + public Flux allServices() { + return reactiveDiscoveryClient.getInstances("service-provider") + .map(serviceInstance -> serviceInstance.getHost() + ":" + + serviceInstance.getPort()); + } + + @GetMapping("/service-call/{name}") + public Mono serviceCall(@PathVariable("name") String name) { + return webClientBuilder.build().get() + .uri("http://service-provider/echo/" + name).retrieve() + .bodyToMono(String.class); + } + } + + public static void main(String[] args) { + SpringApplication.run(ConsumerReactiveApplication.class, args); + } + +} diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/src/main/resources/application.properties new file mode 100644 index 00000000..a96e890a --- /dev/null +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-reactivediscovery-consumer-example/src/main/resources/application.properties @@ -0,0 +1,6 @@ +spring.application.name=service-consumer-reactive +server.port=18083 +management.endpoints.web.exposure.include=* +spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 + +spring.cloud.loadbalancer.ribbon.enabled=false \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml index 6518e4cd..a0be8ef0 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml @@ -19,6 +19,7 @@ nacos-discovery-consumer-example nacos-discovery-consumer-sclb-example + nacos-reactivediscovery-consumer-example nacos-discovery-provider-example nacos-discovery-spring-cloud-config-server-example nacos-discovery-spring-cloud-config-client-example