0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例
Posted wuxun1997
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例相关的知识,希望对你有一定的参考价值。
这里的feign依然是原来的feign,只不过将注册中心由eureka换成了nacos。服务提供方参见0.9.0.RELEASE版本的spring cloud alibaba nacos实例,消费方跟提供方一样,只需加入feign的相关内容即可。抡出三板斧:
1、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"> <modelVersion>4.0.0</modelVersion> <groupId>com.wlf</groupId> <artifactId>lxytrans-application-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR2</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>0.9.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2、application同服务提供方,只需注册到nacos即可:
#端口 server.port=8383 #应用名 spring.application.name=lxytrans-consumer #注册中心 spring.cloud.nacos.discovery.server-addr=localhost:8848
3、启动类中加入注解@EnableFeignClients,引入服务提供方接口,并通过注解指向服务提供方服务实例@FeignClient("lxytrans-provider"):
import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class TransConsumerApplication public static void main(String[] args) SpringApplication.run(TransConsumerApplication.class, args); @Slf4j @RestController static class TestController @Autowired private ApplicationApi applicationApi; @GetMapping("/sayhello") public String sayhello() return "say: " + applicationApi.hello(); @FeignClient("lxytrans-provider") interface ApplicationApi @GetMapping("/hello") String hello();
消费者跑起来后可在nacos中看到:
通过消费方调用提供方:
以上是关于0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例的主要内容,如果未能解决你的问题,请参考以下文章
spring boot 集成axis1.4 java.lang.NoClassDefFoundError: Could not initialize class org.apache.axis.cl
为 cl.exe (Visual Studio Code) 指定命令行 C++ 版本
Dalston.RELEASE版本 Eureka 升级为Nacos