mac_girl

spring cloud整合nacos

最近看到公司的项目服务的注册、发现以及配置都是用nacos来管理,实在是好奇其中的缘由,花了两天的时间翻阅了一些文档,在本地搭建了一个spring cloud的demo并将其注册到nacos上,进行一些配置。

nacos是什么?

在alibaba给出的官方文档里给出了这个问题的解释:nacos是动态服务注册、配置管理、服务管理的平台。

具体的一些定义以及使用可参考官方文档:Nacos文档

如何使用Nacos?

下载nacos客户端并启动

nacos客户端的下载方式

1.从Nacos给出的文档链接直接下载nacos-server-$version.zip Nacos server下载 (从官网下载的缺点是特别特别慢!大家可以从我的百度网盘下载)

2.百度网盘下载:https://pan.baidu.com/s/1DZWWjd3ML6rdZgQtMUxQQQ 提取码:gtja

启动nacos

1.修改nacos/conf下 application.properties的mysql配置

 

 

2.创建nacos_config数据库,执行nacos/conf下的nacos-mysql.sql文件

 

 

3.修改nacos/bin 下的startup.cmd启动方式为单机模式(set MODE="standalone")

 4.startup.cmd启动nacos

 5.访问http://localhost:8848/nacos/ 验证启动成功

账户密码:nacos/nacos

构建spring cloud服务

spring cloud服务使用nacos实现服务注册、发现,配置管理需要引入两个依赖

<!--注册中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--Nacos配置中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

构建父工程nacos-demo

这里需要注意一点的是spring boot的版本要和spring cloud 、spring cloud alibaba的版本匹配,否则启动会报错

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>org.example</groupId>
    <artifactId>nacos-demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>nacos-provider</module>
        <module>nacos-consumer</module>
    </modules>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!--spring-boot-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- spring-cloud -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- 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>
        </dependencies>
    </dependencyManagement>

</project>

构建子工程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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example</groupId>
        <artifactId>nacos-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>nacos-provider</artifactId>

    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--nacos注册中心-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--Nacos配置中心-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

构建子工程nacos-consumer

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example</groupId>
        <artifactId>nacos-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>nacos-consumer</artifactId>

    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.0</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--注册中心-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--Nacos配置中心-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

使用nacos实现服务注册

nacos-provider服务

1.在resource下创建一个bootstrap.properties文件加入nacos配置

文件命名一定要是bootstrap.properties不然在nacos上的配置也不生效

spring.application.name=nacos-provider
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.file-extension=properties

server.port=1128

2.启动类加入@EnableDiscoveryClient注解

package com.example.nacosprovider;

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

@EnableDiscoveryClient
@SpringBootApplication
public class NacosProviderApplication {

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

}

3.启动服务,出现以下情况说明服务注册成功

nacos-consumer服务

1.在resource下创建一个bootstrap.properties文件加入nacos配置

spring.application.name=nacos-comsumer
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
#可以加载同一个namespace下面的不同group的多个配置文件
spring.cloud.nacos.config.ext-config[0].data-id=database.properties
spring.cloud.nacos.config.ext-config[0].group=GROUP_A
spring.cloud.nacos.config.ext-config[0].refresh=true
spring.cloud.nacos.config.ext-config[1].data-id=log.properties
spring.cloud.nacos.config.ext-config[1].group=GROUP_A
spring.cloud.nacos.config.ext-config[1].refresh=true
#可以加载当前group下面的多个配置文件
#spring.cloud.nacos.config.file-extension=properties
#spring.cloud.nacos.config.shared-dataids=database.properties
#spring.cloud.nacos.config.refreshable-dataids=database.properties
server.port=1121

2.启动类加入@EnableDiscoveryClient注解测试服务发现

package com.example.nacosconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient
@SpringBootApplication
public class NacosConsumerApplication {
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

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

3.启动服务,效果同上

测试服务与服务之间的调用

nacos-provier

1.创建EchoController 

package com.example.nacosprovider.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@RestController
public class EchoController {

    @GetMapping("/echo/{string}")
    public String echo(@PathVariable String string) {
        return "Hello Nacos Discovery " + string;
    }
}

nacos-consumer

1.创建TestController

在TestController 中调用nacos-provider服务

package com.example.nacosconsumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RefreshScope
public class TestController {
    @GetMapping("/echo/{str}")
    public String echo(@PathVariable String str) {
        return restTemplate.getForObject("http://nacos-provider/echo/" + str, String.class);
    }

}

调用结果:

 使用nacos实现配置管理

以在nacos-provider配置为例

1.在nacos平台上的配置管理->配置列表+号新建配置所需的参数,形式如下

 

 

 2.在bootstrap.properties中加入config配置

spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.file-extension=properties

3.获取nacos中配置的值

package com.example.nacosprovider.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class ConfigController {

    @Value("${config.address}")
    private String address;

    @GetMapping("/print/address")
    public String printAddress(){
        return address;
    }
}

4.运行结果

 以上内容参考了https://www.zhihu.com/column/c_1197898155730358272这个博主写的关于nacos系列,内容简直让我拍手叫绝,妙!妙!妙!,大家如果有需要可以详细看一下里面的内容,除了写了nacos是如何实现服务注册、配置管理和服务管理,同时还写了nacos的命名空间,扩展配置以及共享配置,实现了配置的隔离,在启动项目可以通过项目启动参数指定运行的环境,根据不同的运行环境使用不同的配置。

posted on 2022-09-29 15:11  宇宙美少女  阅读(510)  评论(0编辑  收藏  举报

导航