天天看點

springcloud學習-19 Spring Cloud Alibaba、Nacos服務注冊和配置中心【周陽springcloud2020學習筆記】

Spring Cloud Alibaba了解

https://github.com/alibaba/spring-cloud-alibaba/blob/master/README-zh.md

Spring Cloud Alibaba 緻力于提供微服務開發的一站式解決方案。

此項目包含開發分布式應用微服務的必需元件,友善開發者通過 Spring Cloud 程式設計模型輕松使用這些元件來開發分布式應用服務。

主要功能:

  • 服務限流降級:預設支援 WebServlet、WebFlux, OpenFeign、RestTemplate、Spring Cloud Gateway, Zuul, Dubbo 和 RocketMQ 限流降級功能的接入,可以在運作時通過控制台實時修改限流降級規則,還支援檢視限流降級 Metrics 監控。
  • 服務注冊與發現:适配 Spring Cloud 服務注冊與發現标準,預設內建了 Ribbon 的支援。
  • 分布式配置管理:支援分布式系統中的外部化配置,配置更改時自動重新整理。
  • 消息驅動能力:基于 Spring Cloud Stream 為微服務應用建構消息驅動能力。
  • 分布式事務:使用 @GlobalTransactional 注解, 高效并且對業務零侵入地解決分布式事務問題。。
  • 阿裡雲對象存儲:阿裡雲提供的海量、安全、低成本、高可靠的雲存儲服務。支援在任何應用、任何時間、任何地點存儲和通路任意類型的資料。
  • 分布式任務排程:提供秒級、精準、高可靠、高可用的定時(基于 Cron 表達式)任務排程服務。同時提供分布式的任務執行模型,如網格任務。

    網格任務支援海量子任務均勻配置設定到所有 Worker(schedulerx-client)上執行。

  • 阿裡雲短信服務:覆寫全球的短信服務,友好、高效、智能的互聯化通訊能力,幫助企業迅速搭建客戶觸達通道。

元件:

  • Sentinel:把流量作為切入點,從流量控制、熔斷降級、系統負載保護等多個次元保護服務的穩定性。
  • Nacos:一個更易于建構雲原生應用的動态服務發現、配置管理和服務管理平台。
  • RocketMQ:一款開源的分布式消息系統,基于高可用分布式叢集技術,提供低延時的、高可靠的消息釋出與訂閱服務。
  • Dubbo:Apache Dubbo™ 是一款高性能 Java RPC 架構。
  • Seata:阿裡巴巴開源産品,一個易于使用的高性能微服務分布式事務解決方案。
  • Alibaba Cloud ACM:一款在分布式架構環境中對應用配置進行集中管理和推送的應用配置中心産品。
  • Alibaba Cloud OSS: 阿裡雲對象存儲服務(Object Storage Service,簡稱 OSS),是阿裡雲提供的海量、安全、低成本、高可靠的雲存儲服務。

    您可以在任何應用、任何時間、任何地點存儲和通路任意類型的資料。

  • Alibaba Cloud SchedulerX: 阿裡中間件團隊開發的一款分布式任務排程産品,提供秒級、精準、高可靠、高可用的定時(基于 Cron 表達式)任務排程服務。

Nacos服務注冊和配置中心

Nacos:

英文全稱Dynamic Naming and Configuration Service,是指該注冊/配置中心都是以服務為核心。(Nacos就是注冊中心+配置中心的組合)

一個更易于建構雲原生應用的動态服務發現,配置管理和服務管理中心。

作用:

  • 替代Eureka做服務注冊中心
  • 替代Config做服務配置中心

各種注冊中心比較:

架構 CAP模型 控制台管理 社群活躍度
eureka AP 支援
zookeeper CP 不支援
consul CP 支援
nacos AP 支援

一、安裝并運作Nacos

1.環境要求:Java8+Maven環境

2.https://github.com/alibaba/nacos/releases/tag/1.1.4

3.解壓安裝包,直接運作bin目錄下的startup.cmd

4.指令運作成功後直接通路http://localhost:8848/nacos

賬号密碼都是nacos

二、Nacos作為服務注冊中心示範

1.基于Nacos的服務提供者

1)建立module:alibaba-nacos-provider-payment9001

2)pom

  • 父pom
<!-- spring cloud alibaba 2.1.0.RELEASE -->
<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>

- 本子產品pom
<dependencies>

    <!-- nacos -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <!-- devtools -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>

    <!-- lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

    <!-- test -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- fastjson -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.62</version>
    </dependency>

 </dependencies>
           

3)yml

server:
  port: 9001

spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #配置Nacos位址

management:
  endpoints:
    web:
      exposure:
        include: '*'
           

4)主啟動

@EnableDiscoveryClient
@SpringBootApplication
public class NacosPayment9001 {
    public static void main(String[] args) {
        SpringApplication.run(NacosPayment9001.class,args);
    }
}
           

5)業務類

@RestController
@RequestMapping("/payment")
public class PaymentController
{
    @Value("${server.port}")
    private String serverPort;

    @GetMapping(value = "/nacos/{id}")
    public String getPayment(@PathVariable("id") Integer id)
    {
        return "nacos registry, serverPort: "+ serverPort+"\t id"+id;
    }
}
           

6)測試

  • http://lcoalhost:9001/payment/nacos/1
  • 檢視nacos控制台
  • nacos服務注冊中心+服務提供者9001都ok了

7)nacos的負載均衡,參照9001建立9002,步驟同上

2.基于Nacos的服務消費者

1)建立Module:alibaba-nacos-consumer-order83

2)pom

<dependencies>
    <!--SpringCloud ailibaba nacos -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
     <!--公共子產品-->
    <dependency>
        <groupId>cn.chen.demo</groupId>
        <artifactId>api-common</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

   <!-- devtools工具 -->
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-devtools</artifactId>
       <scope>runtime</scope>
       <optional>true</optional>
   </dependency>

   <!-- lombok -->
   <dependency>
       <groupId>org.projectlombok</groupId>
       <artifactId>lombok</artifactId>
       <optional>true</optional>
   </dependency>

   <!-- test -->
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-test</artifactId>
       <scope>test</scope>
   </dependency>
</dependencies>
           

3)yml

server:
  port: 83

spring:
  application:
    name: nacos-consumer-order
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

service-url:
  nacos-user-service: http://nacos-payment-provider
           

4)主啟動

@EnableDiscoveryClient
@SpringBootApplication
public class NacosOrder83 {
    public static void main(String[] args) {
        SpringApplication.run(NacosOrder83.class,args);
    }
}
           

5)業務類

  • ApplicationContextConfig
@Configuration
public class ApplicationContextConfig {
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}
           
  • NacosOrderController
@RestController
@Slf4j
@RequestMapping("/consumer")
public class NacosOrderController {
    @Resource
    private RestTemplate restTemplate;

    @Value("${service-url.nacos-user-service}")
    private String serverURL;

    @GetMapping(value = "/payment/nacos/{id}")
    public String paymentInfo(@PathVariable("id") Long id) {
        return restTemplate.getForObject(serverURL+"/payment/nacos/"+id,String.class);
    }

}
           

6)測試

nacos控制台檢視

http://localhost:83/consumer/payment/nacos/13

83通路9001/9002,輪詢負載OK

springcloud學習-19 Spring Cloud Alibaba、Nacos服務注冊和配置中心【周陽springcloud2020學習筆記】
springcloud學習-19 Spring Cloud Alibaba、Nacos服務注冊和配置中心【周陽springcloud2020學習筆記】
springcloud學習-19 Spring Cloud Alibaba、Nacos服務注冊和配置中心【周陽springcloud2020學習筆記】
springcloud學習-19 Spring Cloud Alibaba、Nacos服務注冊和配置中心【周陽springcloud2020學習筆記】

Nacos支援AP和CP模式的切換

三、Nacos作為服務配置中心示範

Nacos作為配置中心-基礎配置

1.建module:alibaba-config-nacos-client3377

2.pom

<dependencies>
    <!-- nacos-config -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>

    <!-- nacos-discovery -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>

    <!-- web + actuator -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <!-- devtools -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <!-- lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <!-- test -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

</dependencies>
           

3.yml:配置兩個

nacos同spring cloud config 一樣,在項目初始化時,要保證先從配置中心進行配置拉取,拉取後才能保證項目的正常啟動。

springboot項目中配置檔案加載存在優先級順序,bootstrap優先級高于application

1)bootstrap.yml

#nacos注冊中心
server:
  port: 3377

spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #服務注冊中心位址
      config:
        server-addr: localhost:8848 #配置中心位址
        file-extension: yaml #指定yaml格式的配置,目前隻支援 properties 和 yaml 類型
           

2)application.yml

#nocaos注冊配置
spring:
  profiles:
    active: dev
           

4.主啟動類

@EnableDiscoveryClient
@SpringBootApplication
public class NacosConfigClient3377
{
    public static void main(String[] args) {
        SpringApplication.run(NacosConfigClient3377.class, args);
    }
}
           

5.業務類

@RestController
@RefreshScope //實作配置自動重新整理
@RequestMapping("/nacosclient")
public class ConfigClientController
{
	@Value("${config.info}")
	private String configInfo;

	@GetMapping("/config/info")
	public String getConfigInfo() {
		return configInfo;
	}
}
           

6.在Nacos中添加配置資訊

  • Nacos中的比對規則:Nacos中的dataid的組成格式與SpringBoot配置檔案中的比對規則
  • 配置管理->配置清單->新增 nacos-config-client-dev.yaml

    1)設定DataId

公式:

  • prefix 預設為 spring.application.name 的值
  • spring.profile.active 既為目前環境對應的 profile,可以通過配置項 spring.profile.active 配置
  • file-exetension 為配置内容的資料格式,可以通過配置項 spring.cloud.nacos.config.file-extension 配置

    【公式對應

    ${spring.application.name} -> bootstrap.yml中nacos-config-client

    ${spring.profile.active} -> application.yml中dev

    ${spring.cloud.nacos.config.file-extension} -> bootstrap.yml中yaml

    即 nacos-config-client-dev.yaml

2)配置内容

config:
    info: "test config for dev. From nacos config center. No.1 "
           

7.啟動3377。

通路http://localhost:3377/nacosclient/config/info

正常,ok了。

四、Nacos叢集和持久化配置(重要)

springcloud學習系列目錄