【教程】SpringCloud+Nacos+Feign+Gateway ( 三 ) Nacos Feign 服务消费者 使用者(推荐)

 

【教程】IDEA创建SpringCloud ( 三.二 ) Nacos Feign 服务消费者 使用者(推荐)

 

可参考:【教程】IDEA创建SpringCloud ( 三.二 ) Eureka Feign (Eureka推荐) 服务消费者 使用者 https://blog.csdn.net/G971005287W/article/details/113871425

代码:https://gitee.com/guanweiCode/SpringCloudGw

 

1 创建项目

 

2 配置POM

重点

    <!-- 引入SpringCloud的Nacos server依赖 -->
    <!--服务发现pom-->
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>

    <!-- feign -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>

 

完整 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.gw</groupId>
  <artifactId>gwcloud-nacos-consumer-demo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>gwcloud-nacos-consumer-demo</name>

  <!-- 引入父项目 -->
  <parent>
    <groupId>com.gw</groupId>
    <artifactId>gwcloud-nacos-consumer</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <dependencies>
    <!-- 引入SpringCloud的euekea server依赖 -->
    <!--服务发现pom-->
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>

    <!-- web服务 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

  </dependencies>

</project>

 

3 配置 application.yml

server:
  port: 8821  #服务消费者 使用者 调用者
spring:
  application:
    name: demo-consumer  #指定应用名称
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

4. 创建启动类

重点

/**
 * 代表自己是一个服务消费者
 */
@EnableDiscoveryClient
/**
 * 当前使用Nacos的Server
 * basePackages 配置feign客户端目录
 */
@EnableFeignClients(basePackages = "com.gw.nacos.consumer.demo.client")

完整

package com.gw.nacos.consumer.demo;

/**
 * @Title: Nacos服务消费者 调用者
 * @Description: 描述
 * @Version: v1.0
 * @Author: Mr.Guan
 * @Mail GuanWeiMail@163.com
 * @DateTime: 2021-02-26
 * @Project SpringCloudDemo
 * @Package com.gw
 */

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * 代表自己是一个服务消费者
 */
@EnableDiscoveryClient
/**
 * 当前使用Nacos的Server
 * basePackages 配置feign客户端目录
 */
@EnableFeignClients(basePackages = "com.gw.nacos.consumer.demo.client")
@SpringBootApplication
public class NacosDemoConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosDemoConsumerApplication.class, args);
    }

}

5  FeignClient

package com.gw.nacos.consumer.demo.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @Title: Feign客户端 消费者
 * @Description: 用户服务
 * @Version: v1.0
 * @Author: Mr.Guan
 * @Mail GuanWeiMail@163.com
 * @DateTime: 2021-02-23
 * @Project springcloud-gw-parent
 * @Package com.gw.eureka.serviceconsume.feign.client
 */
@FeignClient(name = "user-provide")   //name 对应的服务名spring.application.name
public interface UserClient {

    /**
     * 通过用户id获取用户
     * @param userId
     * @return
     */
    @GetMapping("/user/getUser/{userId}")
    String getUser(@PathVariable("userId") String userId);

}

 

6. Controller

package com.gw.nacos.consumer.demo.controller;

import com.gw.nacos.consumer.demo.client.UserClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Title: 标题
 * @Description: 描述
 * @Version: v1.0
 * @Author: Mr.Guan
 * @Mail GuanWeiMail@163.com
 * @DateTime: 2021-02-26 16:42:01
 * @Project springcloud-gw-parent
 * @Package com.gw.nacos.consumer.demo.controller
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("user")
public class DemoConsumerController {

    @Value("${server.port}")
    private String port;

    @Resource
    private UserClient userClient;

    /**
     * 通过用户ID获取用户
     * @param userId
     * @return
     */
    @ApiOperation(value = "根据用户ID查询用户")
    @GetMapping("/getUserInfo/{userId}")
    public String getUserInfo(@PathVariable("userId") String userId){

        String user = userClient.getUser(userId);
        return String.format("【%s-Demo消费者】:调用Feign接口返回值 %s", port, user);
    }


}

 

 

 

 

 

 

 

 

 

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 点我我会动 设计师:白松林 返回首页