spring cloud alibaba中的feign服务调用和spring cloud中的feign服务调用一样,下面不在多做说明,直接粘贴代码
修改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 products() {
String[] arr = {"A"+port,"B"+port,"C"+port};
return Arrays.asList(arr);
}
}
新建完成后,结构如下
使用feign还需在pom中引入feign依赖
spring-cloud-alibab
com
0.0.1-SNAPSHOT
4.0.0
nacosclient2
org.springframework.boot
spring-boot-starter-web
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
2.0.0.RELEASE
org.springframework.cloud
spring-cloud-commons
2.0.0.RELEASE
org.springframework.cloud
spring-cloud-starter-openfeign
新建启动类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);
}
}
直接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 listData() {
List list = feignClientServer.getDatas();
return list;
}
}
在客户端中使用注解@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 getDatas();
}
首先可以看到在nacos服务中心页面中多了nacos-client2服务
然后访问http://localhost:8988/getdatas,可以访问到nacos-client中的数据