02--创建一个工程并将其注册至nacos,并使用nacos配置

nacos 环境搭建请参考
  1. 新建一maven项目spring-cloud-alibaba
  2. 删除 spring-cloud-alibaba 项目中的 src文件
  3. pom.xml中引入springboot及springcloudalibab依赖
<?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>spring-cloud-alibaba</groupId>
    <artifactId>spring-cloud-alibaba</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>spring-cloud-alibaba</name>
    <url>http://www.xxx.xxx</url>
    <description>spring-cloud-alibaba</description>

    <properties>
        <java.version>1.8</java.version>
        <spring.cloud.version>Greenwich.SR6</spring.cloud.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
    </parent>


    <!-- 依赖声明 -->
    <dependencyManagement>
        <dependencies>

            <!-- SpringCloud 微服务 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- SpringCloud 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>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.2</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
  1. 在spring-cloud-alibaba项目中新建producer-example模块
  2. producer-example 模块中引入nacos开发包
<?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>spring-cloud-alibaba</artifactId>
        <groupId>spring-cloud-alibaba</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>producer-example</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>
                <configuration>
                    <mainClass>com.cloud.alibaba.ProducerApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
  1. producer-example模块中 java目录下新建开发目录 com.cloud.alibaba 并新建ProducerApplication启动类
package com.cloud.alibaba;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author 刘志强
 * @date 2020/11/17 09:29
 * @EnableDiscoveryClient 启动客户端
 */
@EnableDiscoveryClient
@SpringBootApplication
@Slf4j
public class ProducerApplication implements CommandLineRunner {


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

    @Override
    public void run(String... args) {
        log.info("producer已启动");
    }
}

  1. resources 目录下新建 bootstrap.yml 文件。
  2. bootstrap.yml和application.yml的区别。Bootstrap属性的优先级高,因此默认情况下不能被本地配置覆盖。
  3. bootstrap.yml
spring:
  application:
    name: producer-example
  profiles:
    active: dev

  main:
    allow-bean-definition-overriding: true
  cloud:
    nacos:
      discovery:
        # 服务注册地址
        server-addr: 101.37.152.195:8848
      config:
        server-addr: 101.37.152.195:8848
        # 配置文件格式
        file-extension: yml
        # 共享配置
        shared-dataids: application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
  1. 服务启动 producer-example 服务会注册进nacos, 并读取nacos配置中的 producer-example-dev.yml,producer-example.yml 以及 application-dev.yml配置信息
  2. nacos配置如下
Data ID: producer-example-dev.yml
Group:DEFAULT_GROUP
配置格式:YAML
配置内容:
author:
    userName: 刘志强
    mailbox: 2425358736@qq.com
    

Data ID: producer-example.yml
Group:DEFAULT_GROUP
配置格式:YAML
配置内容:
server:
  port: 10000
  

Data ID: application-dev.yml
Group:DEFAULT_GROUP
配置格式:YAML
配置内容:
author:
    weChat: Liu19940528
  1. 修改启动类 ProducerApplication,打印配置信息
package com.cloud.alibaba;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.core.env.Environment;

/**
 * @author 刘志强
 * @date 2020/11/17 09:29
 * @EnableDiscoveryClient 启动客户端
 */
@EnableDiscoveryClient
@SpringBootApplication
@Slf4j
public class ProducerApplication implements CommandLineRunner {

    @Autowired
    private Environment env;

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

    @Override
    public void run(String... args) {
        log.info("producer已启动");
        log.info(env.getProperty("author.userName"));
        log.info(env.getProperty("author.mailbox"));
        log.info(env.getProperty("author.weChat"));
    }
}

  1. 启动项目 观察启动端口和打印信息是否来自nacos配置
  2. 观察nacos服务列表是否已将producer-example 注册
  3. springcloud 默认支持热更新spring.cloud.refresh.enabled=true
  4. @RefreshScope 注解 刷新作用域。 新增Author类
package com.cloud.alibaba.domain;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

/**
 * @author 刘志强
 * @date 2020/11/17 13:39
 */
@RefreshScope
@Component
public class Author {
    @Value("${author.userName}")
    private String userName;

    @Value("${author.mailbox}")
    private String mailbox;

    @Value("${author.weChat}")
    private String weChat;

    @Override
    public String toString() {
        return "Author{" +
                "userName='" + userName + '\'' +
                ", mailbox='" + mailbox + '\'' +
                ", weChat='" + weChat + '\'' +
                '}';
    }
}

  1. 修改启动类
package com.cloud.alibaba;

import com.cloud.alibaba.domain.Author;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 刘志强
 * @date 2020/11/17 09:29
 * @EnableDiscoveryClient 启动客户端
 */
@EnableDiscoveryClient
@SpringBootApplication
@Slf4j
@RestController
public class ProducerApplication implements CommandLineRunner {

    @Autowired
    private Author author;

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

    @Override
    public void run(String... args) {
        log.info("producer已启动");
        log.info(author.toString());
    }

    @GetMapping("/test")
    public String test() {
        return author.toString();
    }
}

  1. 启动后 修改配置 在访问test接口观察返回值
源码地址
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值