欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Nacos--在Spring cloud中使用Spring Cloud Alibaba Nacos Discovery(服务注册+配置管理示例)

程序员文章站 2022-07-15 10:17:11
...

开发套路放在前:
a 加依赖

b 加注解

c 写配置


  • 父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.zeng</groupId>
    <artifactId>nacosTest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>quickstart‐provider</module>
    </modules>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!--spring-cloud-alibaba-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--spring-cloud-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--spring-boot-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.10</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.2.5.RELEASE</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
  • 子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">
    <parent>
        <artifactId>nacosTest</artifactId>
        <groupId>com.zeng</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>quickstart-provider</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--需要引入该jar才能使bootstrap配置文件生效-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.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>
        </dependency>
    </dependencies>
</project>
  • nacos配置

Nacos--在Spring cloud中使用Spring Cloud Alibaba Nacos Discovery(服务注册+配置管理示例)

  • 测试controller

@RestController
@Slf4j
@RefreshScope
public class ServiceTest {

    @Value("${common.config1:local}")
    private String testValue;

    @GetMapping(value = "/getConfig")
    public String getConfig() {
        log.debug("debug:common.config1="+testValue);
        log.info("info:common.config1="+testValue);
        return testValue;//可动态更新
    }
}
  • bootstrap.yml

spring:
  application:
    name: quickstart-provider
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.31.119:8848
        namespace: 405e2609-c439-4b1e-b177-82f5a5ce2a0b
      config:
        server-addr: 192.168.31.119:8848
        file-extension: yaml
        namespace: 405e2609-c439-4b1e-b177-82f5a5ce2a0b
        group: TEST_GROUP
  • 启动类

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ProviderApp {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApp.class,args);
    }
}

@EnableDiscoveryClient 在spring cloud项目中表明此项目是一个注册发现客户端,这里注册服务发现使用的是Nacos

@EnableFeignClients 开启FeignClient

注:如果只是注册服务提供者到nacos,@EnableDiscoveryClient和@EnableFeignClients可以省略,但大部分服务不仅是提供者,也是消费者,所以习惯把这两个注解加上。

  • 测试

打开http://127.0.0.1:8848/nacos/的服务列表:

Nacos--在Spring cloud中使用Spring Cloud Alibaba Nacos Discovery(服务注册+配置管理示例)

测试配置管理成功:

Nacos--在Spring cloud中使用Spring Cloud Alibaba Nacos Discovery(服务注册+配置管理示例)

并能在线更新配置(日志级别)成功:

Nacos--在Spring cloud中使用Spring Cloud Alibaba Nacos Discovery(服务注册+配置管理示例)


遇到如下错误:

java.lang.IllegalArgumentException: no server available
    at com.alibaba.nacos.client.naming.net.NamingProxy.reqAPI(NamingProxy.java:438) ~[nacos-client-1.1.1.jar:na]
    at com.alibaba.nacos.client.naming.net.NamingProxy.reqAPI(NamingProxy.java:386) ~[nacos-client-1.1.1.jar:na]
    at com.alibaba.nacos.client.naming.net.NamingProxy.sendBeat(NamingProxy.java:309) ~[nacos-client-1.1.1.jar:na]
    at com.alibaba.nacos.client.naming.beat.BeatReactor$BeatTask.run(BeatReactor.java:92) [nacos-client-1.1.1.jar:na]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_201]
    at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) [na:1.8.0_201]
    at java.util.concurrent.FutureTask.run(FutureTask.java) [na:1.8.0_201]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_201]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.8.0_201]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_201]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_201]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_201]

可能是因为你加了nacos服务发现的依赖,但是没有在bootstrap.yml中配置

Nacos--在Spring cloud中使用Spring Cloud Alibaba Nacos Discovery(服务注册+配置管理示例)

Nacos--在Spring cloud中使用Spring Cloud Alibaba Nacos Discovery(服务注册+配置管理示例)


建议:

1 因为是配置管理和服务发现都使用nacos,所以最好先测试好配置管理后,再配置服务发现

2 可能有的小伙伴和我一样,想把服务发现的配置放到nacos的配置管理中,其实完全没必要,因为配置管理中的服务发现配置修改后,服务实例必需重启才会生效。

相关标签: alibaba nacos