SpringCloudAlibaba(6)Nacos客户端

2020-06-19

创建一个项目名为alibaba-nacos-provider的文件夹

引入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">
    <!--POM文件的模型版本是4.0.0-->
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <!--组名一般是域名反写-->
        <groupId>top.misterzhang</groupId>
        <!--继承父级依赖-->
        <artifactId>alibaba-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../alibaba-dependencies/pom.xml</relativePath>
    </parent>

    <!--和下边的name一样,最好是项目名-->
    <artifactId>alibaba-nacos-provider</artifactId>
    <!--打包方式是jar包-->
    <packaging>jar</packaging>

    <name>alibaba-nacos-provider</name>
    <url>http://misterzhang.top</url>
    <inceptionYear>202020-Now</inceptionYear>

    <dependencies>
        <!-- SpringBoot的Web场景启动器 -->
        <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-config</artifactId>
        </dependency>
        <!--SpringBoot健康监控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--SpringBoot测试场景启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--SpringCloud ailibaba nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--自己包名的路径-->
                    <mainClass>top.misterzhang.alibaba.nacos.provider.NacosProviderApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

创建启动类

package top.misterzhang.alibaba.nacos.provider;

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

/**
 * @author zhangfan
 * @date 2020/6/19 10:31 上午
 * 创建nacos客户端,本服务为服务提供者
 */
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosProviderApplication.class,args);
    }
}

编写bootstrap.yml配置文件

server:
  port: 8081

spring:
  application:
    name: misterzhang-nacos
  cloud:
    nacos:
      discovery:
        server-addr: 你的IP:8848 #配置Nacos地址

management:
  endpoints:
    web:
      exposure:
        include: '*'

#  加载顺序:bootstrap.properties, bootstrap.yml, application.properties, application.yml

编写controller

package top.misterzhang.alibaba.nacos.provider.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhangfan
 * @date 2020/6/19 11:27 上午
 */
@RestController
public class PortController {

    @GetMapping("/get/port{consumer}")
    public String getPort(@PathVariable String consumer){
        return consumer + "服务,调用了" + "8081";
    }
}

请求成功,配置完成


标题:SpringCloudAlibaba(6)Nacos客户端
作者:张范
地址:http://misterzhang.top/articles/2020/06/19/1592535845796.html