Spring Cloud Alibaba系列(一)nacos作為服務注冊中心


Spring Cloud Alibaba各組件版本關系

Spring Cloud Alibaba Version Sentinel Version Nacos Version RocketMQ Version Dubbo Version Seata Version
2.2.1.RELEASE 1.7.1 1.2.1 4.4.0 2.7.6 1.1.0
2.2.0.RELEASE 1.7.1 1.1.4 4.4.0 2.7.4.1 1.0.0
2.1.2.RELEASE or 2.0.2.RELEASE 1.7.1 1.2.1 4.4.0 2.7.6 1.1.0
2.1.1.RELEASE or 2.0.1.RELEASE or 1.5.1.RELEASE 1.7.0 1.1.4 4.4.0 2.7.3 0.9.0
2.1.0.RELEASE or 2.0.0.RELEASE or 1.5.0.RELEASE 1.6.3 1.1.1 4.4.0 2.7.3 0.7.1

畢業版本依賴關系

Spring Cloud Version Spring Cloud Alibaba Version Spring Boot Version
Spring Cloud Hoxton.SR3 2.2.1.RELEASE 2.2.5.RELEASE
Spring Cloud Hoxton.RELEASE 2.2.0.RELEASE 2.2.X.RELEASE
Spring Cloud Greenwich 2.1.2.RELEASE 2.1.X.RELEASE
Spring Cloud Finchley 2.0.2.RELEASE 2.0.X.RELEASE
Spring Cloud Edgware 1.5.1.RELEASE 1.5.X.RELEASE

這次項目中我們用的alibaba版本是2.2.1.REALEASE,因此各組件的版本與之對應,在實際應用中請務必使用與Spring Cloud Alibaba版本相對應的Spring Cloud版本和Spring Boot版本。

什么是nacos

在spring cloud版本中我們使用eureka、consul等做為服務注冊中心,使用spring cloud config做為配置中心。而在spring cloud alibaba中,使用nacos組件即可完成服務注冊發現與服務配置兩大功能。

安裝nacos

下載地址:https://github.com/alibaba/nacos/releases

nacos支持的三種模式:

  • 單機模式 - 用於測試和單機試用。
  • 集群模式 - 用於生產環境,確保高可用。
  • 多集群模式 - 用於多數據中心場景。

下載完成后解壓,我們發現有兩個啟動文件,stratup.cmd 和 startup.sh。打開這兩個文件我們發現startup.cmd默認支持的是單機模式,startup.sh默認支持的是集群模式。

我們雙擊運行startup.cmd。

  • nacos默認端口是:8848
  • 默認用戶名:nacos
  • 默認密碼:nacos

訪問http://127.0.0.1:8848/nacos/index.html,如果出現以下界面則安裝正常。

注冊一個服務到nacos

  1. 新建一個服務,在pom中加入必要的依賴
  <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.2.5.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <dependencyManagement>
    <dependencies>
      <!--Spring cloud Hoxton.SR3-->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Hoxton.SR3</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!--Spring cloud alibaba 2.1.0.RELEASE-->
      <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>2.2.1.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
  </dependencies>
  1. 在application.yml中配置服務名稱和nacos地址
server:
  port: 9001
spring:
  application:
    name: nacos-discovery-server
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
  1. 提供一個接口
@SpringBootApplication
public class NacosDiscoveryServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosDiscoveryServerApplication.class, args);
    }
    @RestController
    static class TestController {
        @GetMapping("/hello")
        public String hello(@RequestParam String name) {
            return "hello,nacos discovery! " + name;
        }
    }
}
  1. 啟動服務,控制台打印以下內容,就說明注冊成功了。
2020-04-28 14:49:42.749  INFO 9864 --- [           main] c.a.c.n.registry.NacosServiceRegistry    : nacos registry, DEFAULT_GROUP nacos-discovery-server 192.168.9.114:9001 register finished
  1. 打開nacos控制台,我們可以在服務列表中發現我們的服務了。

注意事項

  1. 為什么我們的啟動類上沒有加@EnableDiscoveryClient注解,但是已經把服務注冊到nacos上了?

    在springcloud E版本的時候,對服務注冊進行了優化,在依賴了spring-cloud-starter-alibaba-nacos-discovery之后,默認會將服務注冊到注冊中心。

  2. 在已經依賴spring-cloud-starter-alibaba-nacos-discovery的情況下,如果我們不想讓我們的服務注冊到nacos應該怎么做?

    在配置文件中添加如下依賴即可:

spring:
  cloud:
    nacos:
      discovery:
        enabled: false

代碼示例


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM