Nacos服务调用(基于Openfeign)
在<<Nacos服务注册>>这篇文章里,我搭建了一个nacos服务中心,并且注册了一个服务,下面我们来看在上一篇文章的基础上,怎样用Openfeign来调用这个服务。
0.同上篇,启动nacos
1.搭建alibaba spring cloud脚手架
访问https://start.aliyun.com/bootstrap.html,GroupID: com.alibaba.cloud,Artifact:nocos-discovery-consumer-sample。,选择依赖:Nacos Service Discovery,Spring Web,Cloud Bootstrap.
2.在pom文件中引入spring-cloud-starter-loadbalancer,注意一定要引入loadbalancer,否则用openfeign调用会报错。
完整的pom文件如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.11</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.alibaba.cloud</groupId> <artifactId>nocos-discovery-consumer-sample</artifactId> <version>0.0.1-SNAPSHOT</version> <name>nocos-discovery-consumer-sample</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud-alibaba.version>2021.0.4.0</spring-cloud-alibaba.version> <spring-cloud.version>2021.0.4</spring-cloud.version> </properties> <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> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> <version>3.0.1</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3.配置application.properties
spring.application.name=nocos-discovery-consumer-sample
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
spring.cloud.nacos.discovery.server-addr=localhost:8848
spring.cloud.nacos.discovery.namespace=public
spring.main.allow-circular-references=true
server.port=9091
注意这一行:spring.cloud.nacos.discovery.server-addr=localhost:8848 ,表明这是nacos服务中心的地址。
4.新建一个Openfeign调用类:
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.*; import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient; @FeignClient("nocos-discovery-provider-sample") @LoadBalancerClient("nocos-discovery-provider-sample") public interface EchoService { @GetMapping("/echo/{message}") public String callEcho(@PathVariable String message); }
这里要注意的是标注@LoadBalancerClient("nocos-discovery-provider-sample"),nocos-discovery-provider-sample就是上一篇中服务提供方注册到nacos的名称.
5.新建一个Controller:
@RestController public class RestTemplateController { @Autowired private EchoService echoService; @GetMapping("/call/echo/{message}") public String callEcho(@PathVariable String message) { //openFeign test return "From openFeign :"+echoService.callEcho(message); } }
6.SpringBoot启动类:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class NocosDiscoveryConsumerSampleApplication { public static void main(String[] args) { SpringApplication.run(NocosDiscoveryConsumerSampleApplication.class, args); } }
记得要加标注@EnableFeignClients
7.访问http://localhost:9091/call/echo/hello