当前位置:   article > 正文

SpringCloud整合Nacos(一)----------- 使用Nacos作为注册中心_nacos-auth-pluging

nacos-auth-pluging

一、Nacos简介

   Nacos核心提供两个功能:服务注册与发现,动态配置管理。

  1、服务注册与发现

     Nacos提供基于DNS和基于RPC的服务发现,即能被用来支持https/http的服务注册与发现,也支持RPC如dubbo的服务注册与发现。

    与Dubbo使用的zookeeper相比而言,两者差异还是比较大的,zookeeper是一种分布式的协调服务,它天生是作为分布式数据一致性场景下的解决方案,所以zookeeper是CP的,它牺牲了可用性来保证一致性,在极端情况下(master选举期间)服务会对外停止,对于服务可用性要求比较高的系统是难以接受的。Nacos是一种去中心化的架构,属于CAP理论里的AP架构,支持最终一致性,在分布式服务发现与注册场景下具有很不错的性能。目前dubbo官方也支持使用Nacos代替zookeeper。

    2、动态配置服务

     动态修改配置并实时生效对于服务端的同学而已并不陌生,这种服务能够让我们的服务拥有更多的灵活性,不需要重启服务即可做到配置实时生效,非常适合于“配置优先”的服务开发。

二、Nacos安装(windows)

   1.下载安装

     下载地址:https://github.com/alibaba/nacos/releases/tag/1.1.0 

     选择:nacos-server-1.1.0.zip或者nacos-server-1.1.0.tar.gz 下载;下载完成解压。解压文件中的bin文件夹中有个startup.cmd文件,双击该文件就可以启动。启动成功后会弹出一个cmd窗口。启动失败则不会弹出。

   2.访问

   访问地址:http://localhost:8848/nacos/index.html    用户名密码:nacos/nacos

三、使用Nacos作为注册中心 

      1.我的环境

           windows7、JDK8、nacos-server-1.1.0、springboot-2.1.9.RELEASE、springcloud-Greenwich.SR3、spring-cloud-alibaba-dependencie-0.9.0.RELEASE

       2.创建服务提供者

           创建一个聚合工程SpringCloudNacos作为父工程,pom.xml如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <!-- <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.2.4.RELEASE</version>
  9. <relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;
  10. </parent>-->
  11. <groupId>com.learning.nacos</groupId>
  12. <artifactId>springcloudnacos</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>springcloudnacos</name>
  15. <description>Demo project for Spring Boot</description>
  16. <!--子工程 子工程创建好之后取消注释
  17. <modules>
  18. <module>nacos-provider</module>
  19. <module>nacos-consumers</module>
  20. </modules>-->
  21. <properties>
  22. <java.version>1.8</java.version>
  23. <spring-boot.version>2.1.9.RELEASE</spring-boot.version>
  24. <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
  25. <nacos.version>0.9.0.RELEASE</nacos.version>
  26. </properties>
  27. <dependencies>
  28. <dependency>
  29. <groupId>org.projectlombok</groupId>
  30. <artifactId>lombok</artifactId>
  31. <version>1.18.2</version>
  32. <scope>provided</scope>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-test</artifactId>
  37. <scope>test</scope>
  38. </dependency>
  39. <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
  40. <dependency>
  41. <groupId>org.junit.jupiter</groupId>
  42. <artifactId>junit-jupiter-api</artifactId>
  43. <version>5.5.2</version>
  44. <scope>test</scope>
  45. </dependency>
  46. </dependencies>
  47. <!-- 依赖版本号管理 重点在这里 -->
  48. <dependencyManagement>
  49. <dependencies>
  50. <dependency>
  51. <groupId>org.springframework.cloud</groupId>
  52. <artifactId>spring-cloud-dependencies</artifactId>
  53. <version>${spring-cloud.version}</version>
  54. <type>pom</type>
  55. <scope>import</scope>
  56. </dependency>
  57. <dependency>
  58. <groupId>org.springframework.boot</groupId>
  59. <artifactId>spring-boot-dependencies</artifactId>
  60. <version>${spring-boot.version}</version>
  61. <type>pom</type>
  62. <scope>import</scope>
  63. </dependency>
  64. <dependency>
  65. <groupId>org.springframework.cloud</groupId>
  66. <artifactId>spring-cloud-alibaba-dependencies</artifactId>
  67. <version>${nacos.version}</version>
  68. <type>pom</type>
  69. <scope>import</scope>
  70. </dependency>
  71. </dependencies>
  72. </dependencyManagement>
  73. </project>

在父工程springcloudnacos下创建子工程nacos-provider(服务提供者)。pom.xml如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>com.learning.nacos</groupId>
  7. <artifactId>springcloudnacos</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. </parent>
  10. <groupId>com.sprincloud.nacos</groupId>
  11. <artifactId>nacos-provider</artifactId>
  12. <version>0.0.1-SNAPSHOT</version>
  13. <name>nacos-provider</name>
  14. <description>Demo project for Spring Boot</description>
  15. <properties>
  16. <java.version>1.8</java.version>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-web</artifactId>
  22. </dependency>
  23. <!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-discovery -->
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  27. <version>0.9.0.RELEASE</version>
  28. </dependency>
  29. </dependencies>
  30. <build>
  31. <plugins>
  32. <plugin>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-maven-plugin</artifactId>
  35. </plugin>
  36. </plugins>
  37. </build>
  38. </project>

在启动类NacosProviderApplication中提供一个对外接口,并添加@EnableDiscoveryClient注解,开启注册发现功能。

  1. package com.sprincloud.nacos.nacosprovider;
  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.GetMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @RestController
  8. @EnableDiscoveryClient
  9. @SpringBootApplication
  10. public class NacosProviderApplication {
  11. public static void main(String[] args) {
  12. SpringApplication.run(NacosProviderApplication.class, args);
  13. }
  14. @GetMapping("/hello")
  15. public String helloNacos(){
  16. return "Hello Nacos !";
  17. }
  18. }

最好一步:application.yml配置文件:

  1. server:
  2. port: 8001
  3. spring:
  4. application:
  5. name: nacos-provide
  6. cloud:
  7. nacos:
  8. discovery:
  9. server-addr: 127.0.0.1:8848

3.创建服务消费者

在父工程springcloudnacos下创建子工程nacos-consumers(服务消费者)。pom.xml文件和nacos-provider一样。

服务消费者通过TestTemplate+Ribbon进行服务调用;NacosConsumersApplication启动类代码如下:

  1. package com.springcloud.nacos.nacosconsumers;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.web.bind.annotation.GetMapping;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import org.springframework.web.client.RestTemplate;
  11. @RestController
  12. @EnableDiscoveryClient
  13. @SpringBootApplication
  14. public class NacosConsumersApplication {
  15. public static void main(String[] args) {
  16. SpringApplication.run(NacosConsumersApplication.class, args);
  17. }
  18. @Autowired
  19. private RestTemplate template;
  20. @Bean
  21. @LoadBalanced
  22. public RestTemplate getRestTemplate(){
  23. return new RestTemplate();
  24. }
  25. @GetMapping("/consumer")
  26. public String test1() {
  27. return template.getForObject("http://nacos-provider/hello",String.class);
  28. }
  29. }

application.yml配置如下:

  1. server:
  2. port: 8002
  3. spring:
  4. application:
  5. name: nacos-consumers
  6. cloud:
  7. nacos:
  8. discovery:
  9. server-addr: 127.0.0.1:8848

4.启动测试

    先启动启动nacos,然后运行刚刚创建好的两个子工程。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/182362
推荐阅读
相关标签
  

闽ICP备14008679号