Spring Cloud Alibaba [Why do you need a distributed configuration center, understand the mainstream configuration center, DataID configuration, Group grouping scheme, Namespace implementation scheme] (4)

 

Table of contents

Service call_Dubbo implements service downgrade

Distributed configuration center_Why do we need a distributed configuration center

Distributed Configuration Center_Learn about mainstream configuration centers

Data synchronization between the application and the configuration center usually follows the following three modes: 

Distributed configuration center_Namespace namespace

Distributed configuration center_DataID configuration

Distributed configuration center_Group grouping scheme

Distributed configuration center_Namespace implementation plan

Distributed configuration center_connect the application to the Nacos configuration center


 

Service call_Dubbo implements service downgrade

Set service producer cluster fault tolerance mode

@DubboService(timeout = 5000,
        version = "1.0",
        methods = {@Method(name = "index",retries = 2)},
        // 快速失败模式,调用只执行一次,失败则立即报错。
        cluster = "failfast")
public class PaymentServiceImpl implements IPaymentService {
    @Override
    public String index() {
        return "hello dubbo payment";
   }
}

 

 Write downgrade implementation class

public class PaymentFallback implements IPaymentService {
    //降级方法
    @Override
    public String index() {
        return "服务繁忙请稍后再试!";
   }
}

Write the order business layer

@Service
public class PaymentServiceImpl {
    @DubboReference(version = "1.0",
                    mock = "com.tong.service.PaymentFallback",
                    // 快速失败,去查询集群中其他机器
                    cluster = "failfast")
   
    IPaymentService iPaymentService;
    public String index() {
        return iPaymentService.index();
   }
}

Note: The mock here directly configures the full path of the service downgrade class.

test

Intentionally closing the order pays the producer service. Request http://localhost:80/order/index

 

Real-time effect feedback

1. How to implement service degradation_____ in Dubbo technology.

A Hystrix

B Resilience4j

C mock

D above are all wrong 

Distributed configuration center_Why do we need a distributed configuration center

Why do we need a distributed configuration center 

As a developer, I am no stranger to configuration files. In a Spring project, an application.yml or application.properties is provided by default.

 

 

Note: Some configuration information of each service is uniformly handed over to third-party middleware for maintenance. Data changes in distributed configuration management need to be pushed to corresponding application service nodes in real time. 

 

Real-time effect feedback

1. The disadvantages of not using the distributed configuration center are _____.

A does not support dynamic update of configuration files 

B does not support the configuration of centralized management

C does not support multi-environment deployment

D above are all correct

Distributed Configuration Center_Learn about mainstream configuration centers

There are three mainstream distributed configuration centers, among which Nacos is Alibaba's open source distributed configuration center. 

Nacos

This is a component in the SpingCloud alibaba technology stack, and we have used it as a service registry before. In fact, it also integrates the function of service configuration, and we can directly use it as a service configuration center.

 

Data synchronization between the application and the configuration center usually follows the following three modes: 

Pull mode: Let the application open and long poll, that is, periodically pull the latest configuration information from the configuration center and update it to the application memory.

Push mode: After the configuration data in the configuration center is changed, the configuration data is actively pushed to the specified application, and the application is updated to the local memory.

Mixed mode: The application and configuration center maintain a persistent connection through the "event mechanism + listener" mode. If the configuration information monitored by the application changes, the configuration center publishes an event of the corresponding type. Only after the application listens to the event, will it process the corresponding configuration information and update it to the local application. 

Apollo

Apollo is a distributed configuration center open sourced by Ctrip. There are many features, such as: configuration updates can take effect in real time, support grayscale release function, and can perform version management, operation audit and other functions for all configurations, and provide open platform API. Ctrip's distributed configuration center framework has a graphical interface to manage configuration file information, and the configuration file information is stored in the database.

Spring Cloud Config

It is seamlessly integrated with Spring, very convenient to use, and its configuration storage supports Git. However, it does not have a visual operation interface, and the configuration does not take effect in real time, and needs to be restarted or refreshed.

Comparison of distributed configuration centers

Real-time effect feedback

1. The following is not a distributed configuration center technology is _____. 

A Nacos

B Apollo

C SpringCloud Config

D Gateway

2. The three commonly used modes for data synchronization between applications and configuration centers in the following Nacos technologies are _____.

A Pull mode

B Push mode

C blending mode

All of the above are correct

Distributed configuration center_Namespace namespace

foreword 

Nowadays, in the microservice system, a system is often split into multiple services, each service has its own configuration file, and each system often prepares a development environment, a test environment, and a formal environment.

Question: Let's do the math. Suppose a system has 10 microservices, then there are at least 10 configuration files, and three environments (dev\test\prod), then there are 30 configuration files that need to be managed. 

concept 

Used for tenant granular configuration isolation. The configuration of the same Group or Data ID can exist under different namespaces. One of the common scenarios of Namespace is the separation and isolation of configurations in different environments, such as the isolation of resources (such as configurations and services) in development and test environments and production environments. Default namespace=public reserved space, does not support deletion; by default.

 

Scenes 

The best practice given by Nacos shows that the outermost namespace can be used to distinguish deployment environments, such as test, dev, prod, etc.

Note: Namespaces can be used to isolate configurations for different environments. Generally an environment is divided into a namespace. 

Create a new dev/test Namespac 

View namespace 

Real-time effect feedback

1. The main role of the Nacos component Namespace namespace is _____.

A configuration isolation and management in multiple environments

B organization division configuration

C organization configuration

D above are all wrong 

2. Namespace in Nacos components can be used to distinguish____.

A project configuration

B configuration file

C deployment environment

D above are all wrong

Distributed configuration center_DataID configuration

 concept

The ID of a configuration set in Nacos. The configuration set ID is one of the dimensions for organizing and dividing configurations. Data IDs are often used to organize configuration sets for partitioned systems. A system or application can contain multiple configuration sets, and each configuration set can be identified by a meaningful name.

Note: In the system, a configuration file is usually a configuration set. The configuration of a general microservice is a configuration set.

splicing format of dataId 

explain:

1. prefix: The default is the value of spring.application.name.

2. spring.profiles.active: the profile corresponding to the current environment.

3. file-extension: file suffix 

When activeprofile is empty.

New dev configuration DataID 

write configuration 

Real-time effect feedback 

1. The meaning of the prefix in the Nacos component dataId__.

A application name

B environment

C suffix

D above are all wrong

2. The meaning of file-extension in the Nacos component dataId__.

A application name

B environment

C suffix

D above are all wrong

Distributed configuration center_Group grouping scheme

concept 

A set of configuration sets in Nacos is one of the dimensions of organizational configuration. Group configuration sets with a meaningful string to distinguish configuration sets with the same Data ID. When you create a configuration on Nacos, if the name of the configuration group is not filled in, the name of the configuration group defaults to DEFAULT_GROUP.

Realize environment distinction through Group 

Upload the new configuration file DataID on the Nacos GUI console 

Real-time effect feedback

1. The default group name of Group in Nacos component is _____.

A DEFAULT

B GROUP

C DEFAULT_GROUP

D above are all wrong 

Distributed configuration center_Namespace implementation plan

Nacos gives two practical solutions for Namespace

1. For a tenant

2. For multiple tenants

for one tenant

From the perspective of a tenant (user), if there are multiple sets of different environments, then different namespaces can be created according to the specified environment at this time to achieve isolation of multiple environments.

For example, you may have three different environments of dev, test and prod, then use a set of nacos clusters to build the following three different namespaces respectively.

 

Problem: The single tenant here is also suitable for small projects, or the implementation plan when there are not many projects. By defining different environments, projects in different environments are managed under different Namespaces, and different environments are isolated through Namespaces. 

 for multiple tenants

When multiple projects use the Nacos cluster at the same time, Group can also be used to perform granular grouping within the Namespace. Here we take Namespace: dev as an example, in which different projects in the same environment are reclassified through different groups.

Note: Through the above theoretical analysis, it can be seen that the second scheme has good scalability 

Real-time effect feedback

1. The Namespace namespace in Nacos technology is applicable to one tenant _____.

A small project 

B medium project

C large project

D above are all wrong

2. The best implementation scheme of Namespace in Nacos technology _____ has good scalability.

A for a single tenant

B for one tenant

C for multiple tenants

D above are all wrong

Distributed configuration center_connect the application to the Nacos configuration center

new configuration 

Create project cloud-nacos-config3344 

POM introduces dependencies 

  <dependencies>
        <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>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
           <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
    </dependencies>

Write the main startup class

/**
* 主启动类
*/
@SpringBootApplication
@Slf4j
@EnableDiscoveryClient
public class NacosConfigMain3344 {
    public static void main(String[] args) {
        SpringApplication.run(NacosConfigMain3344.class,args);
        log.info("***********NacosConfigMain3344 **********");
   }
}

Create configuration file

Create a configuration file named bootstrap.yml, note that it is bootstrap.yml, not application.

Reason: Nacos is the same as SpringCloud-Config. When the project is initialized, the configuration must be pulled from the configuration center first. After the configuration is pulled, the normal start of the project can be guaranteed. The loading of configuration files in SpringBoot has a priority order, and bootstrap has a higher priority than application. 

server:
 port: 3344
spring:
 application:
   name: nacos-config
 cloud:
   nacos:
     discovery:
       server-addr: 192.168.66.101:8848
     config:
        #服务器地址
       server-addr: 192.168.66.101:8848
        #默认为Public命名空间,可以省略不写 #对应建立的命名空间的UUID
       namespace: a26e7bb2-04df-4574-8313-3d8728ae88ba
        #指定文件后缀
       file-extension: yaml
        #文件名 -- 如果没有配置则默认为 ${spring.appliction.name}
       prefix: ${spring.application.name}
        #指定配置群组 --如果是Public命名空间 则可以省略群组配置
       group: DEFAULT_GROUP
 profiles:
   active: dev

 

The above configuration is to ensure the normal registration and configuration acquisition of the service, as well as the correctness of the configuration DataID 

Create an external interface to read configuration from nacos 

@RestController
public class NacosConfigController {
    @Value("${nacos.config}")
    private String config;
    @RequestMapping("/getValue")
    public String getValue() {
        return config;
   }

problem appear

An error occurs, the configuration must be wrong, check the 5-step song

For details, see the figure below 

Test request http://localhost:3344/getValue 

 

 

Guess you like

Origin blog.csdn.net/m0_58719994/article/details/131817448