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

客户端:feign

pom 配置
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
 </parent>
 
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
<!--依赖版本定义-->
   <dependencyManagement>
       <dependencies>
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-dependencies</artifactId>
               <version>Greenwich.RELEASE</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-alibaba-dependencies</artifactId>
               <version>0.2.1.RELEASE</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>
       </dependencies>
   </dependencyManagement>
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
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
    </dependencies>
    
<!--依赖版本定义-->
   <dependencyManagement>
       <dependencies>
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-dependencies</artifactId>
               <version>Greenwich.RELEASE</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-alibaba-dependencies</artifactId>
               <version>0.2.1.RELEASE</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>
       </dependencies>
   </dependencyManagement>
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<String> 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
发布了12 篇原创文章 · 获赞 0 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览