Nacos集成 springcloud 注册中心、配置中心 示例

你的名字 2022-12-14 09:02 344阅读 0赞

服务提供者

引入依赖pom.xml:

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.0.3.RELEASE</version>
  5. <relativePath /> <!-- lookup parent from repository -->
  6. </parent>
  7. <dependencyManagement>
  8. <dependencies>
  9. <dependency>
  10. <groupId>org.springframework.cloud</groupId>
  11. <artifactId>spring-cloud-dependencies</artifactId>
  12. <version>Finchley.RELEASE</version>
  13. <type>pom</type>
  14. <scope>import</scope>
  15. </dependency>
  16. </dependencies>
  17. </dependencyManagement>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-web</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.cloud</groupId>
  25. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  26. <version>0.9.0.RELEASE</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  31. <version>0.9.0.RELEASE</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-configuration-processor</artifactId>
  36. <optional>true</optional>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.projectlombok</groupId>
  40. <artifactId>lombok</artifactId>
  41. </dependency>
  42. </dependencies>

bootstrap.properties

  1. #服务名称
  2. spring.application.name=NacosProviderTest
  3. #consul 地址
  4. spring.cloud.consul.host=localhost
  5. #consul 端口
  6. spring.cloud.consul.port=8500
  7. spring.cloud.consul.discovery.prefer-ip-address=true
  8. #nacos config server 地址
  9. spring.cloud.nacos.config.server-addr=127.0.0.1:8848
  10. #nacos 配置中心功能,默认true
  11. spring.cloud.nacos.config.enabled=true
  12. #nacos config server 动态配置文件格式
  13. spring.cloud.nacos.config.file-extension=yaml

applicatioin.yml

  1. jacky:
  2. info:
  3. age: 0

新建pojo

  1. import org.springframework.boot.context.properties.ConfigurationProperties;
  2. import org.springframework.cloud.context.config.annotation.RefreshScope;
  3. import org.springframework.stereotype.Component;
  4. import lombok.Data;
  5. @ConfigurationProperties("jacky.info")
  6. @RefreshScope
  7. @Component
  8. @Data
  9. public class MyProperties {
  10. int age;
  11. }

新建启动测试类:

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. @SpringBootApplication
  9. @EnableDiscoveryClient
  10. @RestController
  11. public class NacosProviderApplication {
  12. public static void main(String[] args) {
  13. SpringApplication.run(NacosProviderApplication.class, args);
  14. }
  15. @Autowired
  16. private MyProperties myProperties;
  17. @RequestMapping("test/{id}")
  18. public String test(@PathVariable Integer id) {
  19. return "服务提供者 test - id = " + id + " >>> " + myProperties.getAge();
  20. }
  21. }

nacos页面端新建配置信息:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3Zpc2lvbjU4MzkzNDA2MQ_size_16_color_FFFFFF_t_70

  1. server:
  2. port: 8888
  3. spring:
  4. application:
  5. name: NacosProviderTest
  6. cloud:
  7. nacos:
  8. discovery:
  9. server-addr: localhost:8848
  10. jacky:
  11. info:
  12. age: 20

服务消费者

配置中心同服务提供者

注册中心配置同服务提供者

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import org.springframework.web.client.RestTemplate;
  11. @SpringBootApplication
  12. @EnableDiscoveryClient
  13. @RestController
  14. public class NacosConsumerApplication {
  15. public static void main(String[] args) {
  16. SpringApplication.run(NacosConsumerApplication.class, args);
  17. }
  18. @Bean
  19. @LoadBalanced
  20. public RestTemplate restTemplate() {
  21. return new RestTemplate();
  22. }
  23. @Autowired
  24. private RestTemplate restTemplate;
  25. @RequestMapping("text/{id}")
  26. public String text(@PathVariable Integer id) {
  27. return "消费者获得数据:" + restTemplate.getForObject("http://NacosProviderTest/test/"+id, String.class);
  28. }
  29. }

发表评论

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

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

相关阅读

    相关 SpringCloud注册中心-Nacos

    Nacos简介 Nacos是阿里巴巴开源的一款支持服务注册与发现,配置管理以及微服务管理的组件。用来取代以前常用的注册中心(zookeeper , eureka等等),以