一、博客前提
spring cloud alibaba中的feign服务调用和spring cloud中的feign服务调用一样,下面不在多做说明,直接粘贴代码
二、修改nacosclient服务
修改nacosclient服务,增加一个对外暴露的接口以供外部访问,新增DataController类,直接在类中返回数据
package tp;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
/**
* @Package: com.tp
* @ClassName: DataController
* @Author: tanp
* @Description: ${description}
* @Date: 2020/7/20 15:24
*/
@RestController
public class DataController {
@Value("${server.port}")
String port;
@RequestMapping("/datas")
public List<String> products() {
String[] arr = {"A"+port,"B"+port,"C"+port};
return Arrays.asList(arr);
}
}
三、新建nacosclient2子模块
新建完成后,结构如下
四、修改pom文件
使用feign还需在pom中引入feign依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-alibab</artifactId>
<groupId>com</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacosclient2</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
五、新建启动类
新建启动类NacosClient2Application,启动类中需加上@EnableFeignClients
package tp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @Package: tp
* @ClassName: NacosClientApplication
* @Author: tanp
* @Description: ${description}
* @Date: 2020/8/3 17:43
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosClient2Application {
public static void main(String[] args) {
SpringApplication.run(NacosClient2Application.class,args);
}
}
六、新建yml文件
直接coppynacosclient服务的,修改端口号和服务名
server:
port: 8988
spring:
application:
name: nacos-client2
cloud:
nacos:
discovery:
#指定nacos server的地址
server-addr: localhost:8848
七、新建访问接口
新建访问接口ShowDataController,在controller中注入feign客户端,调用nacosclient中的获取数据的方法
package tp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @Package: com.tp
* @ClassName: ShowDataController
* @Author: tanp
* @Description: ${description}
* @Date: 2020/7/20 16:57
*/
@RestController
public class ShowDataController {
@Autowired
FeignClientServer feignClientServer;
@RequestMapping("/getdatas")
public List<String> listData() {
List<String> list = feignClientServer.getDatas();
return list;
}
}
八、新建feign客户端
在客户端中使用注解@FeignClient 定义feign客户端 ,注解的value值为你需要调用的数据服务的服务名,然后用@GetMapping注解表明你调用数据服务中的那个方法
package tp;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
/**
* @Package: com.tp
* @ClassName: FeignClientServer
* @Author: tanp
* @Description: ${description}
* @Date: 2020/7/20 16:59
*/
@Service
@FeignClient(value = "nacos-client")
public interface FeignClientServer {
@GetMapping("/datas")
public List<String> getDatas();
}
九、启动并访问
首先可以看到在nacos服务中心页面中多了nacos-client2服务
然后访问http://localhost:8988/getdatas,可以访问到nacos-client中的数据