在<<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