Springcloud Alibaba入门系列之客户端调用方式
在上节使用Nacos实现服务注册与发现中我们简单的利用了Nacos的其中最基础的特性完成了应用的注册和发现,也简单的通过接口来实现了consumer来调用producer的过程,这节我们来看看如何通过声明式的工具来调用我们的服务提供者,常见的是通过RestTemplate. feign以及webflux等,这节我们只来看看后面的两种.
使用feign的方式
关于什么是Feign?,这里就不在啰嗦了,实质上就是一个微服务之间的声明式调用的工具,详情可以去看看springcloud官网,首先在上节的项目中创建一个名为alibaba-nacos-client-feign的子模块项目.
- pom文件中需要添加对应的依赖,代码如下;
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- 其次我们需要定义feign客户端,代码如下;
package com.cacmp.alibaba.nacos.client.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @author cb
*/
@FeignClient(name = "alibaba-nacos-producer")
public interface FeignService {
@GetMapping("hello")
String hello(@RequestParam String name);
注解FeignClient其主要的目的是为了来指定要调用的那个服务名称,接口中我们只需要绑定要调用服务的rest接口,如上述代码中我们服务alibaba-nacos-producer的hello接口......
- 来看我们的配置文件,代码如下:
server:
port: 9001
spring:
application:
name: alibaba-nacos-client-feign
cloud:
nacos:
discovery:
server-addr: localhost:8848
- 最后来看我们的启动类,代码如下:
package com.cacmp.alibaba.nacos.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author cb
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class AlibabaNacosClientFeignApplication {
public static void main(String[] args) {
SpringApplication.run(AlibabaNacosClientFeignApplication.class, args);
}
同样,我们将该服务注册到了我们的Nacos注册中心中,当然我们作为feign客户端,就需要贴上@EnableFeignClients注解,我们启动我们的服务alibaba-nacos-producer来作为服务的提供者,而alibaba-nacos-client-feign来调用,访问http://localhost:9001/test后,会得到hello:Nacos!的结果,关于feign的讲解我们就到这里,接下来看webflux
webflux
关于webflux的讲解,这里不说,想了解的可以自己去看看,据说是用来替代springmvc的,两者可以对比去看,这里我们直接用就可以了,来看:
首先我们来创建一个alibaba-nacos-client-webflux项目,在pom文件中需要引入对应的依赖,代码如下:
<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.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
这里不再需要web的依赖了,接着我们来看配置文件,代码如下:
server:
port: 9002
spring:
application:
name: alibaba-nacos-client-webflux
cloud:
nacos:
discovery:
server-addr: localhost:8848
再来看启动类,代码入下:
package com.cacmp.alibaba.nacos.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.function.client.WebClient;
/**
* @author cb
*/
@SpringBootApplication
@EnableDiscoveryClient
public class AlibabaNacosClientWebfluxApplication {
public static void main(String[] args) {
SpringApplication.run(AlibabaNacosClientWebfluxApplication.class, args);
}
@Bean
@LoadBalanced
public WebClient.Builder webClientBuilder(){
return WebClient.builder();
}
来看我们的接口类,代码如下:
package com.cacmp.alibaba.nacos.client.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;
/**
* @author cb
*/
@RestController
public class WebFluxController {
@Autowired
private WebClient.Builder webBuilder;
@RequestMapping("test")
public Mono<String> testWebFlux(){
String uri = "http://alibaba-nacos-producer";
Mono<String> mono = webBuilder.build()
.get()
.uri(uri + "/hello?name=" + "Nacos!")
.retrieve()
.bodyToMono(String.class);
System.out.println(mono);
return mono;
}
启动我们的服务,访问http://127.0.0.1:9002/test,会得到hello:Nacos!的结果,以上就是webflux的简单使用.