Spring Cloud (Learning Nacos) 2



foreword

This article mainly talks about the registration center and configuration center in microservice components


1. Component relationship and technology selection

Microservice component diagram

The final technical collocation scheme is:
SpringCloud Alibaba - Nacos: Registration Center (service discovery/registration)
SpringCloud Alibaba - Nacos: Configuration Center (dynamic configuration management)
SpringCloud - Ribbon: Load balancing
SpringCloud - Feign: Declarative HTTP client (call remote Service)
SpringCloud Alibaba - Sentinel: service fault tolerance (current limiting, downgrading, circuit breaker)
SpringCloud - Gateway: API gateway (webflux programming mode)
SpringCloud - Sleuth: call chain monitoring
SpringCloud Alibaba - Seata: original Fescar, that is, a distributed transaction solution

Ali official documents

Chinese document

Correspondence between versions

springcloud relationship correspondence

其中不止springboot与springcloud版本对应关系,还有其它与springboot的版本对应关系

Introduce dependency management before introducing components, and you can ignore the version number when subsequently introducing component dependencies, and these dependencies are imported into the public module common.

 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2021.0.4.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2. Nacos

1. Nacos as a registration center

Introduce related dependencies:

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

  • Download Nacos Registration Center Nacos Server
  • Configure nacos service registration configuration for each service

server.port=8081 spring.application.name=nacos-provider spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 management.endpoints.web.exposure.include=*

  • Add the annotation @EnableDiscoveryClient to the startup item to enable service registration/discovery
@SpringBootApplication
@EnableDiscoveryClient
public class GuliCouponApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(GuliCouponApplication.class, args);
    }
}
  • Start the service for service registration discovery
    insert image description here

insert image description here


如果使用win启动nacos则需要更改win启动脚本文件使用单机启动
insert image description here
在脚本文件末尾加入pause endlocal 可以在启动错误时不闪退,可以看到报错信息
insert image description here

2. Use openFeign remote call

  • Feign is a declarative HTTP client that makes remote calls between services more convenient. It has built-in HTTP request templates, and you can define HTTP request parameters and other information through interfaces and annotations.
  • Feign integrates Ribbon (load balancing) and Hystrix (service fusing)
  • 这里在微服务版本超过2020时,Feign对负载均衡器做出了调整调整为Spring Cloud Loadbalancer,为此需要引入Spring Cloud Loadbalancer的相关依赖并移除Ribbon引用和增加配置,不然开启远程调用的服务会启动报错
spring.cloud.loadbalancer.ribbon.enabled: false
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

Introduce dependencies

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
 </dependency>
  • The remote call steps are
    • Introduce open-feign

    • Write an interface in the service that needs to be called remotely, and this interface needs to call the remote service

      • Each method of the declared interface is the request to call that remote service
        insert image description here
    • To enable the remote call function, add the annotation @EnableFeignClients(basePackages = “com.smz.guli.member.feign”) to the startup class, where basePackages indicates the location of the newly created interface.
      insert image description here

    • Use the remote call interface at the interface to obtain the value of other service interfaces
      insert image description here

3. Nacos as a configuration center

  • Introduction

The main problem solved by the configuration center is that when it is necessary to modify the configuration of a certain service, since the service is clustered and distributed, the modification is time-consuming and labor-intensive. The configuration center is unified and distributes the configuration to the corresponding service. It only needs to be configured once so that all services can read the configuration.

  • Add dependencies to public services
<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

如果微服务版本大于2020,则以下形式将需要引入bootstrap的依赖才可以,不导入依赖的方式比较麻烦这里不再赘述

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
            <version>3.1.0</version>
        </dependency>
  • In order to make the configuration center read the bootstrap.properties file before the configuration file , the bootstrap.properties file in springboot will be before the application.yml .
    insert image description here
  • Configure configuration center information
spring.application.name=guli-coupon
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
  • Add a data set (Data Id) guli-coupon.properties to the configuration center by default. Default rules, application name.properties.

  • Add configuration in guli-coupon.properties
    insert image description here
    insert image description here

  • Get configuration dynamically

    • Dynamic refresh configuration annotation @RefreshScope
    • Get configuration value @Value("${configuration item name}")

如果配置中心和配置文件中都配置了,优先使用配置中心的

4. Configuration center details

  • Namespaces

Configure isolation

  • Used to isolate configurations in different environments (development, testing, production)
    can specify namespaces in bootstrap.properties configuration
    spring.cloud.nacos.config.namespace=c700d749-47f7-4b10-9116-4fe586a1a2a2
    insert image description here
  • Configuration isolation between services Each
    microservice creates its own namespace

默认:public(保留空间);默认新增的所有配置都在public空间

  • configuration set

A collection of all configurations

  • configuration set ID

Data ID

  • configuration group

默认所有的配置集都属于DEFAULT_GROUP
insert image description here
The configuration group can be used to distinguish the environment,
and the configuration group can be specified in the bootstrap.properties configuration
spring.cloud.nacos.config.group=1111insert image description here

  • Load multiple configuration sets at the same time Configure the following code in the
    bootstrap.properties file
spring.cloud.nacos.config.extension-configs[0].data-id=datasource.yml // Data ID
spring.cloud.nacos.config.extension-configs[0].group=dev // 配置分组
spring.cloud.nacos.config.extension-configs[0].refresh=true // 是否动态更新

insert image description here

All configurations in microservices can be handed over to the nacos configuration center for management. Some configuration centers prefer to use the configuration center


Summarize

This chapter mainly talks about the two components of microservice registration discovery and configuration center.

Guess you like

Origin blog.csdn.net/smznbhh/article/details/131449342