赞
踩
创建【nacos-feign】子服务,用于测试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>
- <artifactId>nacos</artifactId>
- <groupId>com.kongcheng</groupId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
-
- <groupId>com.kongcheng</groupId>
- <artifactId>nacos-feign</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>nacos-feign</name>
- <description>Demo project for Spring Boot</description>
-
- <properties>
- <java.version>1.8</java.version>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
- </dependency>
- <!-- 在微服务中引入openfeign的依赖-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-openfeign</artifactId>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <version>2.3.0.RELEASE</version>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- </project>
主入口添加【@EnableDiscoveryClient】用作开启服务注册,【@EnableFeignClients】作为开启OpenFeign:
- 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//开启OpenFeign
- public class NacosFeignApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(NacosFeignApplication.class, args);
- }
-
- }
配置文件如下:
- server:
- port: 18202
- spring:
- application:
- name: nacos-feign
- cloud:
- nacos:
- discovery:
- server-addr: 127.0.0.1:8848 # Nacos作为服务注册中心地址
创建名为【api】的Package,如下所示:
创建RemoteClient接口,来定义OpenFeign要调用的远程服务接口:
调用的是前面创建的【nacos-provide】子服务中的方法,需要注意方法名需要与远程服务名相同
- import org.springframework.cloud.openfeign.FeignClient;
- import org.springframework.web.bind.annotation.GetMapping;
-
- @FeignClient(name = "nacos-provide",fallback = RemoteHystrix.class)
- public interface RemoteClient {
-
- @GetMapping("/helloNacos")
- String helloNacos();
- }
创建RemoteHystrix接口,来进行熔断降级处理
- import org.springframework.stereotype.Component;
-
- /**
- * 服务降级处理
- */
- @Component
- public class RemoteHystrix implements RemoteClient{
-
- public String helloNacos() {
- return "请求超时了!";
- }
- }
创建【TestController】控制层:
- import com.kongcheng.nacosfeign.api.RemoteClient;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- public class TestController {
-
- @Autowired
- private RemoteClient remoteClient;
-
- @GetMapping("/feign")
- public String test() {
- return remoteClient.helloNacos();
- }
- }
完成后启动服务。
需要启动【Nacos-server】,【nacos-provide】,【nacos-feign】,启动成功后浏览器访问:localhost:18202/feign,可以通过OpenFeign远程调用nacos-provide的接口
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。