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

清疚 2022-12-22 01:00 187阅读 0赞
nacos 环境搭建请参考
  1. 新建一maven项目spring-cloud-alibaba
  2. 删除 spring-cloud-alibaba 项目中的 src文件
  3. pom.xml中引入springboot及springcloudalibab依赖

    <?xml version=”1.0” encoding=”UTF-8”?>

    1. <modelVersion>4.0.0</modelVersion>
    2. <groupId>spring-cloud-alibaba</groupId>
    3. <artifactId>spring-cloud-alibaba</artifactId>
    4. <version>1.0-SNAPSHOT</version>
    5. <name>spring-cloud-alibaba</name>
    6. <url>http://www.xxx.xxx</url>
    7. <description>spring-cloud-alibaba</description>
    8. <properties>
    9. <java.version>1.8</java.version>
    10. <spring.cloud.version>Greenwich.SR6</spring.cloud.version>
    11. </properties>
    12. <parent>
    13. <groupId>org.springframework.boot</groupId>
    14. <artifactId>spring-boot-starter-parent</artifactId>
    15. <version>2.1.7.RELEASE</version>
    16. </parent>
  1. <!-- 依赖声明 -->
  2. <dependencyManagement>
  3. <dependencies>
  4. <!-- SpringCloud 微服务 -->
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-dependencies</artifactId>
  8. <version>${spring.cloud.version}</version>
  9. <type>pom</type>
  10. <scope>import</scope>
  11. </dependency>
  12. <!-- SpringCloud Alibaba 微服务 -->
  13. <dependency>
  14. <groupId>com.alibaba.cloud</groupId>
  15. <artifactId>spring-cloud-alibaba-dependencies</artifactId>
  16. <version>2.1.0.RELEASE</version>
  17. <type>pom</type>
  18. <scope>import</scope>
  19. </dependency>
  20. </dependencies>
  21. </dependencyManagement>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.projectlombok</groupId>
  25. <artifactId>lombok</artifactId>
  26. <version>1.18.2</version>
  27. <scope>provided</scope>
  28. </dependency>
  29. </dependencies>
  30. <build>
  31. <resources>
  32. <resource>
  33. <directory>src/main/resources</directory>
  34. <filtering>true</filtering>
  35. </resource>
  36. </resources>
  37. <plugins>
  38. <plugin>
  39. <groupId>org.apache.maven.plugins</groupId>
  40. <artifactId>maven-compiler-plugin</artifactId>
  41. <configuration>
  42. <source>8</source>
  43. <target>8</target>
  44. </configuration>
  45. </plugin>
  46. </plugins>
  47. </build>
  48. </project>
  1. 在spring-cloud-alibaba项目中新建producer-example模块
  2. producer-example 模块中引入nacos开发包

    <?xml version=”1.0” encoding=”UTF-8”?>



    spring-cloud-alibaba
    spring-cloud-alibaba
    1.0-SNAPSHOT

    4.0.0

    producer-example



    org.springframework.boot
    spring-boot-starter-web



    com.alibaba.cloud
    spring-cloud-starter-alibaba-nacos-discovery



    com.alibaba.cloud
    spring-cloud-starter-alibaba-nacos-config






    org.springframework.boot
    spring-boot-maven-plugin

    com.cloud.alibaba.ProducerApplication





  3. 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 {

  1. public static void main(String[] args) {
  2. SpringApplication.run(ProducerApplication.class, args);
  3. }
  4. @Override
  5. public void run(String... args) {
  6. log.info("producer已启动");
  7. }
  8. }
  1. resources 目录下新建 bootstrap.yml 文件。
  2. bootstrap.yml和application.yml的区别。Bootstrap属性的优先级高,因此默认情况下不能被本地配置覆盖。
  3. bootstrap.yml

    spring:
    application:

    1. name: producer-example

    profiles:

    1. active: dev

    main:

    1. allow-bean-definition-overriding: true

    cloud:

    1. nacos:
    2. discovery:
    3. # 服务注册地址
    4. server-addr: 101.37.152.195:8848
    5. config:
    6. server-addr: 101.37.152.195:8848
    7. # 配置文件格式
    8. file-extension: yml
    9. # 共享配置
    10. shared-dataids: application-${ spring.profiles.active}.${ spring.cloud.nacos.config.file-extension}
  4. 服务启动 producer-example 服务会注册进nacos, 并读取nacos配置中的 producer-example-dev.yml,producer-example.yml 以及 application-dev.yml配置信息

  5. nacos配置如下

    Data ID: producer-example-dev.yml
    Group:DEFAULT_GROUP
    配置格式:YAML
    配置内容:
    author:

    1. userName: 刘志强
    2. mailbox: 2425358736@qq.com
  1. Data ID: producer-example.yml
  2. GroupDEFAULT_GROUP
  3. 配置格式:YAML
  4. 配置内容:
  5. server:
  6. port: 10000
  7. Data ID: application-dev.yml
  8. GroupDEFAULT_GROUP
  9. 配置格式:YAML
  10. 配置内容:
  11. author:
  12. 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 {

    1. @Autowired
    2. private Environment env;
    3. public static void main(String[] args) {
    4. SpringApplication.run(ProducerApplication.class, args);
    5. }
    6. @Override
    7. public void run(String... args) {
    8. log.info("producer已启动");
    9. log.info(env.getProperty("author.userName"));
    10. log.info(env.getProperty("author.mailbox"));
    11. log.info(env.getProperty("author.weChat"));
    12. }

    }

  2. 启动项目 观察启动端口和打印信息是否来自nacos配置

  3. 观察nacos服务列表是否已将producer-example 注册
  4. springcloud 默认支持热更新spring.cloud.refresh.enabled=true
  5. @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 {

    1. @Value("${author.userName}")
    2. private String userName;
    3. @Value("${author.mailbox}")
    4. private String mailbox;
    5. @Value("${author.weChat}")
    6. private String weChat;
    7. @Override
    8. public String toString() {
    9. return "Author{" +
    10. "userName='" + userName + '\'' +
    11. ", mailbox='" + mailbox + '\'' +
    12. ", weChat='" + weChat + '\'' +
    13. '}';
    14. }

    }

  6. 修改启动类

    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 {

    1. @Autowired
    2. private Author author;
    3. public static void main(String[] args) {
    4. SpringApplication.run(ProducerApplication.class, args);
    5. }
    6. @Override
    7. public void run(String... args) {
    8. log.info("producer已启动");
    9. log.info(author.toString());
    10. }
    11. @GetMapping("/test")
    12. public String test() {
    13. return author.toString();
    14. }

    }

  7. 启动后 修改配置 在访问test接口观察返回值

源码地址

发表评论

表情:
评论列表 (有 0 条评论,187人围观)

还没有评论,来说两句吧...

相关阅读