【教程】SpringCloud+Nacos+Feign+Gateway ( 二 ) Nacos Client 服务提供者 客户端项目(推荐)

 

【教程】IDEA创建SpringCloud ( 二.二 ) Nacos Client 服务提供者 客户端项目(推荐)

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

1. 启动Nacos服务

2 创建服务提供者参考  springcloud-eureka-servicesupport 

 

 

3.服务提供者具体代码配置 POM.xml

<?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-provide-user</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>gwcloud-nacos-provide-user</name>

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

  <dependencies>
    <!-- 引入SpringCloud的 nacos 依赖 -->
    <!--服务发现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>

重点

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

 

4. yml配置

server:
  port: 8811  #服务提供方
spring:
  application:
    name: user-provide  #指定应用名称
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

5 启动类  重点 

@EnableDiscoveryClient
package com.gw.nacos.provide.user;

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

/**
 * @Title: Nacos服务提供方
 * @Description: 描述
 * @Version: v1.0
 * @Author: Mr.Guan
 * @Mail GuanWeiMail@163.com
 * @DateTime: 2021-02-26
 * @Project SpringCloudDemo
 * @Package com.gw
 */
@EnableDiscoveryClient
@SpringBootApplication
public class NacosUserProvideApplication {

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

 

6 Controller  跟普通Controller一样

package com.gw.nacos.provide.user.controller;

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;

/**
 * @Title: 用户服务
 * @Description: 描述
 * @Version: v1.0
 * @Author: Mr.Guan
 * @Mail GuanWeiMail@163.com
 * @DateTime: 2021-02-26 16:43:36
 * @Project springcloud-gw-parent
 * @Package com.gw.nacos.provide.user.controller
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("user")
public class UserProvideController {


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

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

        return String.format("【%s-服务提供者】:%s", port, userId);
    }

}

 

 

 

 

 

 

 

 

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