当前位置:网站首页>Microservice system design -- service registration, discovery and configuration design

Microservice system design -- service registration, discovery and configuration design

2022-06-27 04:21:00 Zhuangxiaoyan

Abstract

In large-scale systems, many services are called to complete a certain function , This involves the mutual invocation and discovery of services . This blog mainly introduces the design of service registration and discovery .

One 、 Service invocation problem

When analyzing business requirements , There is a simple function point : Members can open monthly cards , When opening the monthly card , You need to add corresponding points . The monthly card function is maintained in the member service module , However, the integral function is added to the integral service module for maintenance , This involves the service invocation between the two modules .

Single instance case : Point to point HTTP Call directly , use IP + Port + Interface . It can also be exposed WebService Services are invoked by external modules , but WebService In the form of Show than HTTP In a slightly heavier form , In the actual business development process , More and more products are developed with lightweight HTTP Protocol for data interaction . If more modules , Will form a spider's Web , It is very unfavorable for development and maintenance .

Multiple instances : To cope with the pressure of service , Multi instance cluster deployment has become a simple and easy-to-use solution . Only after multi instance deployment , Face a problem directly , How the caller knows which instance to call , When the instance fails to run , How to transfer to another instance to process requests ? If load balancing is used , But it is often static , When the service is not available , If the load balancing list is updated dynamically , To ensure the normal calling of the caller ?

In the face of the above two situations , The demand for service registries is pressing , Unify all services 、 Dynamic management .

Two 、 Service registry

The service registry is the core module of the distributed service framework , You can see that the function to be implemented is the registration of services 、 subscribe , The corresponding function is logout 、 Notify these four functions .

All services are connected to the registry , It is uniformly configured and managed by the registry , No longer called directly by the instance itself . The general process of service management is as follows :

  1. When the service provider starts , Actively submit the information of the service provider to the service registry for service registration .
  2. When the service caller starts , Download the service provider information from the registry to the caller's local , The caller is from the local service provider list , Select a service instance to initiate remote invocation based on a certain load balancing policy , This is a point-to-point call .
  3. The service registry can sense that an instance of a service provider is offline , At the same time, the instance service provider information is cleared from the registry , And notify each instance in the service caller cluster , Tell the service caller not to call this instance anymore , In case the call fails .

There are many service registry products to choose from during the development process :

  • Consul
  • Zookeeper
  • Etcd
  • Eureka
  • Nacos

such as Dubbo Often cooperate in development Zookeeper Use ,Spring Cloud During development, it will cooperate with Eureka Use , The community provides quite mature implementation schemes , In this case, the service registry adopts Nacos For actual combat , Use of other registries , Interested friends can do some research after class , The basic application is relatively simple .

3、 ... and 、 Distributed configuration center

Almost every project involves configuration parameters or configuration files , How to avoid hard coding , Through out of code configuration , To improve the security of variable parameters 、 timeliness , Flexible configuration is particularly important . This article will take you to talk about the configuration of software projects , In particular, the configuration problem under the microservice architecture style .

Evolution of parameter configuration

Early software development , Of course, it also includes the development of some small partners , There are hard coded cases , Write some variable parameters in the code . The disadvantages are also obvious , When the parameters change , The compiled code must be rebuilt , Maintenance costs are quite high .

later , The industry has formed rules , Extract some variable parameters , Form configuration files in multiple formats , Such as properties、yml、json、xml wait , Manage these parameters centrally , When there is a change , Just update the configuration file , No need to recode the code 、 Build release code block , Obviously, it is much more powerful than hard coding . There are also drawbacks :

  • Key information is exposed in the configuration file , Low security .
  • After the configuration file changes , The service also faces the problem of restarting

Then came the distributed configuration , Decouple configuration parameters from the project , When using the project , Obtain from the configuration center in time or push to the project when the configuration center changes , The advantages are obvious :

  1. Eliminating the problem of key information exposure
  2. Configuration parameters do not need to be coupled with code modules , It can flexibly manage permissions , safer
  3. The configuration can take effect in real time , It is very helpful for some code scenarios with complex rules
  4. When deploying in multiple environments , Be able to deal with

In the micro service scenario , We are also more inclined to adopt the mode of distributed configuration center , To manage configuration , When the number of service instances increases , There is no need to worry about configuration complexity at all .

Spring Cloud Config Namely Spring Cloud Distributed configuration components under the project , Of course, there is no way to complete the configuration function by itself , Need help Git or MQ And other components , Slightly more complex .

Many companies in the industry open source many distributed configuration components , For example, Ctrip Apollo( Apollo ), Taobao's Diamond, Baidu Disconf,360 Of QConf , Ali's Nacos wait , It can also be based on Zookeeper And so on , There are still many technology selection products .

In this case Nacos As a model , Why Nacos ? First of all Spring Cloud Alibaba A member of the project , It fits closely with the ecology . secondly ,Nacos As a service registry , Already used in the project , It also has the function of configuration center , There is no need to introduce additional third-party components , Increase system complexity , One component completes Spring Cloud Config and Eureka The functions of the two components .

Nacos At the basic level DataID individual Group To locate a unique configuration item , Support different configuration formats , Such as JSON , TEXT , YMAL wait , Different formats , Just follow the grammar rules .

Four 、Nacos

Official website address :home, Open source by Alibaba , A dynamic service discovery that is easier to build cloud native applications 、 Configuration management and service management platform , Have done as Spring Cloud Alibaba A subproject of , Better and Spring Cloud Merge together . It's off Nacos You can open the official website to learn more about , I'm not going to tell you too much here . Let's go directly to our application section .

4.1 install Nacos

This time, we use 1.1.4 edition :nacos-server-1.1.4.tar.gz,( The right management function has been integrated in the latest version ) This test will be deployed in a stand-alone version , Decompress after downloading , Start directly with the corresponding command .

tar -xvf nacos-server-$version.tar.gz cd nacos/bin sh startup.sh -m standalone (standalone Stands for stand-alone mode operation , Non cluster mode ), The startup log is as follows :

appledeMacBook-Air:bin apple$ ./startup.sh -m standalone
/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/bin/java  -Xms512m -Xmx512m -Xmn256m -Dnacos.standalone=true -Djava.ext.dirs=/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/ext:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/lib/ext:/Users/apple/software/nacos/plugins/cmdb:/Users/apple/software/nacos/plugins/mysql -Xloggc:/Users/apple/software/nacos/logs/nacos_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -Dnacos.home=/Users/apple/software/nacos -Dloader.path=/Users/apple/software/nacos/plugins/health -jar /Users/apple/software/nacos/target/nacos-server.jar  --spring.config.location=classpath:/,classpath:/config/,file:./,file:./config/,file:/Users/apple/software/nacos/conf/ --logging.config=/Users/apple/software/nacos/conf/nacos-logback.xml --server.max-http-header-size=524288
nacos is starting with standalone
nacos is starting,you can check the /Users/apple/software/nacos/logs/start.out

The end of the log shows "staring" Indicates successful startup , open http://127.0.0.1:8848/nacos, Enter the default user name nacos、 password nacos, You can see the following interface .

You can see Nacos The main functions provided by have been marked in the menu on the left , This time we only use the service management function , Configuration management will be discussed in the next chapter . It's easy to shut down the service , Execute the corresponding script provided .

Linux / Unix / Mac  Under the platform  sh shutdown.sh

Windows  Under the platform  cmd shutdown.cmd  Or double click  shutdown.cmd  Run the file .

Here we are , The service registry is ready , Let's register the service in the registry .

4.2 Application in service Nacos

1、 First, introduce in the parent project Spring Cloud,Spring Cloud Alibaba jar Package dependency , Reference resources Spring Boot How to introduce . Consider the version issue between the three projects , This time, we use Greenwich.SR4 edition .

Parent project parking-project Of pom.xml The following configuration is added in :

<properties>
        <spring-cloud.version>Greenwich.SR4</spring-cloud.version>
        <spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version>
</properties>

    <dependencyManagement>
       <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

  Introduce... In the sub module service nacos jar package , In the sub module pom.xml Add the following configuration to the file

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>0.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
        </dependency>

Add... To the module startup class @EnableDiscoveryClient annotation , This is with the use of Eureka when , The annotation configuration is consistent , This annotation is based on spring-cloud-commons , It's a universal solution . In the corresponding project configuration file application.properties Add configuration item in :

# Must fill in application.name, Otherwise, the service cannot be registered to nacos
spring.application.name=card-service/member-service
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

Start project , adopt nacos The console checks whether the service is registered to nacos, Two service instances can be found under normal circumstances :

The current service is run in the form of a single instance , The number of service clusters indicated in the figure is 1、 The number of instances is 1、 The number of health instances is 1, If we target the same service , Start two instances , The registry can instantly monitor , And show it . Operation bar " Sample code " link , It provides us with strategies for using services in different development languages , It is quite humanized . It can also be seen here Nacos The future of is definitely cross language , It cannot be limited to Java field , This is also consistent with the language independence of microservices .

Here's a test , stay eclipse project application.properties Modify the service port in the configuration file , Ensure that the same service port does not conflict . start-up 3 An example , To form a card-service Small clusters of services , You can configure the weight of each instance on the console , Different weights , The number of responses will also vary when processing requests , The increase of instances , The response efficiency of the service is greatly improved .

thus , We can register a service to the service registry to uniformly manage the configuration , Other subsequent services can refer to this method , Do a good job in basic configuration , Uniformly register the service to Nacos Registry management and maintenance , Lay the foundation for subsequent inter service calls .

4.3 Nacos Configuration Management

Take the function of user mobile number binding system as an example , Shopping malls do promotional activities , The user binds the mobile number on the same day , And open a monthly card , Double the bonus points , There is also a chance to draw the event gift bag , The end of the event , Return to the original . This is a common way to play .

stay parking-member Project pom.xml add jar introduce , But we have already applied it to nacos Service registry function of , Has been introduced into the project .

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

The following will be related to nacos The connection configuration of , because Spring Boot There are two types of configuration files in the project , One is application Configuration of , One is bootstrap Configuration of , Which file is the configuration in ? Let's see first bootstrap And application What's the difference? .

nacos And spring-cloud-config Consistent in configuration , Must use bootstrap.yml/properties file , Load the configuration first , Fill in application.properties/yml Invalid in .

bootstrap.properties

spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.profiles.active=dev
spring.application.name=member-service
spring.cloud.nacos.config.shared-data-ids=${spring.application.name}-${spring.profiles.active}.properties
spring.cloud.nacos.config.refreshable-dataids=${spring.application.name}-${spring.profiles.active}.properties

Use spring.application.name−spring.application.name−{spring.profiles.active} The reason for dynamic configuration , It is to prepare for multi environment construction in the later stage , Of course, it can also be written directly nacos The configuration file , But this is not conducive to extended maintenance .refreshable-dataids The configuration item is not selected , But if missing , be-all nacos Configuration items will not be automatically refreshed , The configuration items must be refreshed in another way , Can be normally applied to the service .

Use configuration items in your code

Open the method where members bind mobile numbers , Define an internal variable , Accept the parameter values of the configuration center . Can be used directly Spring Of [@Value ] Method can be selected , stay Controller Layers or Service Layers can be used , Class must use [@RefreshScope ] annotation , To synchronize values in real time .

stay nacos Two configuration items are defined in onoffbindmobile and onbindmobile_mulriple, Represents a switch and integral multiple respectively . Define two local variables at the same time to accept nacos Values of two configuration items in .

Even after parameter externalization configuration , There must also be room in the code , For code logic switching , That is, the logic is preset in advance , Whether to execute logic , Related branches in logic, etc , See the value of the configuration parameter completely . For example, when registering , There are often some bonus points 、 Register for free coupons and coupons of what denomination , Registration has the opportunity to draw prizes, etc , Automatically switch to another activity when one activity ends , If it is not preset in advance , There is no way for the code to execute this logic .

  @Value("${onoff_bindmobile}")
    private boolean onoffBindmobile;

    @Value("${on_bindmobile_mulriple}")
    private int onBindmobileMulriple;

  @Override
    public int bindMobile(String json) throws BusinessException{
        Member member = JSONObject.parseObject(json, Member.class);
        int rtn = memberMapper.insertSelective(member);

        //invoke another service
        if (rtn > 0) {
            MemberCard card = new MemberCard();
            card.setMemberId(member.getId());
      // Determine whether the switch is on 
            if (onoffBindmobile) {
                //special logic
                card.setCurQty("" +50 * onBindmobileMulriple);
            }else {
                //normal logic
                card.setCurQty("50");
            }

            memberCardClient.addCard(JSONObject.toJSONString(card));
            log.info("creata member card suc!");
        }

        return rtn;
    }

Nacos Configure parameter items in

bootstrap It has been agreed in the configuration file : The configuration items Key And Value The format of the value is properties file , open nacos Console , Conduct " Configuration Management "->" Configuration list " page , Click on the top right corner " newly added ".

Enter the two configuration items agreed above , Configuration items can meet the configuration requirements of multiple environments , In other environments , Create the corresponding configuration , Input Data ID, such as member-service-prd.properties perhaps member-service-uat.properties,Group Use the default value , Of course, if you need to group , Need to define Group value , Save it . The configuration items value The type needs to be consistent with the Convention in the code , Otherwise, an error will occur during parsing .

To change the value of a configuration item , After modification , Just publish , You can see that the value of the configuration item takes effect immediately . Mainly with the help of [@RefreshScope ] annotation , And bootstrap In profile spring.cloud.nacos.config.refreshable-dataids Configuration to complete . If one of them is missing , The configuration item takes effect only by restarting the application , This is related to the maintainability of our programs 、 Robustness is against .

You can connect the corresponding database 、 Third party application access key 、 Parameter items that change frequently are filled in the configuration center , With the help of the permission control of the configuration center itself , It can ensure that sensitive items are not disclosed , At the same time, configuration isolation can be well done for different deployment environments .

thus , be based on Nacos Configuration center configuration completed for , According to this configuration , It can be migrated to other services normally . Leave a thought question , Use similar methods in the project properties Externalized configuration of files , It's more common , How to ensure that the key information is not leaked , For example, the user name of the database 、 password , Third party core Key wait .

5、 ... and 、 Cluster deployment Nacos service

Blog reference

原网站

版权声明
本文为[Zhuangxiaoyan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270411018162.html