Introduction to microservices
Microservice concept
In short, microservice is an architectural design style. Its main purpose is to split the original independent system into a small service. These small services operate in independent processes, and the services communicate and cooperate through the RESTful API of HTTP. The split small services are built around one or a business function with high coupling in the system, and each service independently maintains its own data storage, business development, automatic testing and independent deployment mechanism. With lightweight communication and collaboration, these microservices can be written in different languages.
Microservice splitting considerations
- Different microservices should not develop the same business repeatedly
- The microservice data is independent. Do not access the databases of other microservices
- Microservices can expose their business as interfaces for other microservices to call
Microservice instance
Spring cloud provides RestTemplate, which is used to access interfaces between different modules and obtain return values
A new RestTemplate needs to be injected into the container before use
@Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); }
Modify the service of Order, send a request to the User module and access the User module
public Order queryOrderById(Long orderId) { // 1. Query order Order order = orderMapper.findById(orderId); // 2. Request call String url = "http://localhost:8081/user/"+order.getUserId(); User user = restTemplate.getForEntity(url,User.class); // 3. Encapsulate user to Order order.setUser(user); // 4. Return return order; }
eureka
Introduction to eureka
Spring cloud encapsulates the Eureka module developed by Netflix to realize service governance.
In the traditional rpc remote invocation framework, it is complex to manage the dependency relationship between services. Therefore, it is necessary to use service governance to manage the dependency management between services, which can realize service invocation, load balancing, fault tolerance, service discovery and registration.
eureka action
- How can consumers obtain specific information about service providers?
- The service provider registers its own information with eureka at startup
- eureka saves this information
- Consumers pull provider information from eureka according to the service name
- If there are multiple service providers, how should consumers choose?
- Service consumers use the load balancing algorithm to select one from the service list
- How do consumers perceive the health status of service providers?
- The service provider will send a heartbeat request to EurekaServer every 30 seconds to report the health status
- eureka will update the recorded service list information, and abnormal heartbeat will be eliminated
- Consumers can get the latest information
eureka server operation
- Create a new moudle and introduce core dependencies
<dependencies> <!--eureka Server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>
- Start class annotation to start EurekaServer automatic assembly
@EnableEurekaServer @SpringBootApplication public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
- application. eureca configuration with YML
server: port: 10086 # Service port spring: application: name: eurekaserver # eureka's service name eureka: client: service-url: # Address information of eureka defaultZone: http://127.0.0.1:10086/eureka
-
Visit localhost:10086 and enter the eureca console
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-k3arohxq-1641344417967) (C: \ users \ acerola \ appdata \ roaming \ typora user images \ image-20220102230657768. PNG)]
eureka client practice
-
Add core dependencies to the pom file under the service module
<!--eureka Client dependency--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
-
Configure eureca in the yml file
eureka: client: service-url: # Address information of eureka defaultZone: http://127.0.0.1:10086/eureka
eureca registration summary
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-itawcvye-1641344417969) (C: \ users \ acerola \ appdata \ roaming \ typora user images \ image-20220102233007106. PNG)]
ribbon and eureca
Introduction to ribbon
Ribbon is a load balancer released by Netflix, which helps to control the behavior of HTTP and TCP clients. After configuring the service provider address for the ribbon, the ribbon can automatically help service consumers to request based on a load balancing algorithm. Ribbon provides us with many load balancing algorithms by default, such as polling, random, etc. Of course, we can also implement custom load balancing algorithms for ribbon.
In Spring Cloud, when Ribbon is used with Eureka, Ribbon can automatically obtain the address list of service providers from Eureka Server and request one of the service provider instances based on the load balancing algorithm. It shows the architecture when Ribbon is used with Eureka.
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-ep51dnsy-1641344417970) (C: \ users \ acerola \ appdata \ roaming \ typora user images \ image-20220103080435230. PNG)]
Detailed process
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-3ul93nny-1641344417971) (C: \ users \ acerola \ appdata \ roaming \ typora user images \ image-20220103093320503. PNG)]
IRule load balancing rules
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-daopda2i-1641344417972) (C: \ users \ acerola \ appdata \ roaming \ typora user images \ image-20220103094625178. PNG)]
Modify IRule rules
- Method 1: in the code mode, inject IRule type objects into the startup class (any configuration class can be used), define a new IRule, and take effect globally
@Bean public IRule randomRule(){ return new RandomRule(); }
- Method 2, configuration file, in application YML adds a new configuration rule, which is configured separately for a service
userservice: ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #Load balancing rules (random)
- Configuration files take precedence over code injection
Ribbon hungry loading
By default, the Ribbon uses lazy loading, that is, the L oadBalanceClient will be created during the first - access, and the request time will be very long.
Hungry loading will be created when the project is started to reduce the time-consuming of the first access. Hungry loading can be started through the following configuration:
ribbon: eager-load: enabled: true # Turn on hungry loading clients: # Specifies the name of the service to load - userservice
ribbon summary
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-dtnfhnv2-1641344417973) (C: \ users \ acerola \ appdata \ roaming \ typora user images \ image-20220103120608683. PNG)]
nacos
Introduction to nacos
It is an open source component of Alibaba that supports service registration and discovery, configuration management and micro service management. It is used to replace the previously used Registry (zookeeper, Eureka, etc.) and configuration center (spring cloud config, etc.). Nacos integrates the functions of registration center and configuration center to achieve two in one.
nacos installation
slightly
nacos registry
-
Add nacos dependency to parent project
<!--nacos Management dependency--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.5.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency>
-
Add nacos dependency. If the client has eureca dependency, it needs to be deleted
<!-- nacos Client dependent package --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
-
Add nacos configuration to the configuration file. If there is eureca configuration, it needs to be cleared
spring: cloud: nacos: server-addr: http://192.168.2.103:8848 # nacos service address
-
Start to see the instance
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-bu24kona-1641344417973) (C: \ users \ acerola \ appdata \ roaming \ typora \ typora user images \ image-20220103190356328. PNG)]
nacos service model
Service cluster instance
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-tcslolfv-1641344417974) (C: \ users \ acerola \ appdata \ roaming \ typora user images \ image-20220103190851849. PNG)]
Cluster settings
spring: cloud: nacos: server-addr: http://192.168.2.103:8848 # nacos service address discovery: cluster-name: HZ #Cluster name Hangzhou
After configuration
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-jihdwus8-1641344417975) (C: \ users \ acerola \ appdata \ roaming \ typora \ typora user images \ image-20220103202150864. PNG)]
According to cluster load balancing
-
Set the IRule of load balancing in order service as NacosRule. This rule will give priority to finding services in the same cluster, and load balancing will be carried out randomly in the same cluster
userservice: ribbon: NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule #Load balancing rules
-
If the service in the same cluster cannot be found, the cross cluster service will be tried and a warning will be given
Weighted load balancing
Set the weight of the service. If the weight is 0-1, it will not be accessed. If it is set to 0, it will be 1 by default. It is a random access of NacosRule to control the traffic and realize gray-scale update
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-f5txvq7e-1641344417976) (C: \ users \ acerola \ appdata \ roaming \ typora user images \ image-20220103213317872. PNG)]
summary
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-8mqvxyee-1641344417978) (C: \ users \ acerola \ appdata \ roaming \ typora user images \ image-20220103213645189. PNG)]
Environmental isolation
The outermost layer of service storage and data storage in Nacos is a thing called namespace, which is used for outermost isolation
-
Create a new namespace
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-wngknmir-1641344417979) (C: \ users \ acerola \ appdata \ roaming \ typora user images \ image-20220103215622425. PNG)]
-
Copy the namespace id and configure it in the configuration file
discovery: namespace: 3bb9e056-0291-44c5-aaf9-255e6b24cae5 # dev environment
nacos configuration management
Unified configuration management
Add configuration in the nacos control center to complete the unified configuration management
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-xxhgpgzq-1641344417982) (C: \ users \ acerola \ appdata \ roaming \ typora user images \ image-20220104144538161. PNG)]
However, we need to read the configuration in nacos first, then read the local configuration, merge and complete the project startup. In spring, bootstrap YML is better than application YML has a higher priority, so all the configuration dependencies related to nacos are put into bootstrap YML [external chain image transfer failed, and the source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-rszet9h6-1641344417983) (C: \ users \ acerola \ appdata \ roaming \ typora \ typora user images \ image-20220104143802805. PNG)]
Specific configuration process
-
Introducing the configuration management client dependency of nacos
<!--nacos Configuration management dependencies for--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>
-
Create a new boot file, bootstrap YML, where name + active + file extension is the Data ID added in the nacos control center, which binds the configuration file of the nacos registry
spring: application: name: userservice profiles: active: dev # environment cloud: nacos: server-addr: http://192.168.137.133:8848 # nacos address discovery: cluster-name: HZ config: file-extension: yaml # File suffix
Configure hot update
-
Method 1, in
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-jqr8gq9j-1641344417985) (C: \ users \ acerola \ appdata \ roaming \ typora user images \ image-20220104162518499. PNG)]
-
Method 2
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-eyf0p87u-1641344417986) (C: \ users \ acerola \ appdata \ roaming \ typora user images \ image-20220104164048580. PNG)]
summary
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-5wapsulz-1641344417988) (C: \ users \ acerola \ appdata \ roaming \ typora user images \ image-20220104164126981. PNG)]
Configure sharing
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-psgdrbuv-1641344417989) (C: \ users \ acerola \ appdata \ roaming \ typora user images \ image-20220104165831662. PNG)]
Configuration priority:
Service name - profile Yaml > service name Yaml > local configuration
Build nacos cluster
-
Cluster structure diagram
Official Nacos cluster diagram:
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-1m3eyanj-1641344417991) (D: / idea / cloud demo / day02 springcloud02 / data / assets/image-20210409210621117.png)]
It contains three Nacos nodes, and then a load balancer agent three Nacos. Here, the load balancer can use nginx.
Our planned cluster structure:
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-o4o1zgjx-1641344417992) (D: / idea / cloud demo / day02 springcloud02 / data / assets/image-20210409211355037.png)]
Addresses of three nacos nodes:
node | ip | port |
---|---|---|
nacos1 | 192.168.150.1 | 8845 |
nacos2 | 192.168.150.1 | 8846 |
nacos3 | 192.168.150.1 | 8847 |
-
Build clusters
Basic steps to build a cluster:
- Build the database and initialize the database table structure
- Download the nacos installation package
- Configuring nacos
- Start the nacos cluster
- nginx reverse proxy
2.1. Initialize database
Nacos default data is stored in the embedded database Derby, which is not a database available for production.
The best practice officially recommended is to use the highly available database cluster with master-slave mode. For the highly available database with master-slave mode, you can refer to the follow-up master courses of intelligence education.
Here we take the single point database as an example.
First create a new database named nacos, and then import the following SQL:
CREATE TABLE `config_info` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id', `data_id` varchar(255) NOT NULL COMMENT 'data_id', `group_id` varchar(255) DEFAULT NULL, `content` longtext NOT NULL COMMENT 'content', `md5` varchar(32) DEFAULT NULL COMMENT 'md5', `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time', `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Modification time', `src_user` text COMMENT 'source user', `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip', `app_name` varchar(128) DEFAULT NULL, `tenant_id` varchar(128) DEFAULT '' COMMENT 'Tenant field', `c_desc` varchar(256) DEFAULT NULL, `c_use` varchar(64) DEFAULT NULL, `effect` varchar(64) DEFAULT NULL, `type` varchar(64) DEFAULT NULL, `c_schema` text, PRIMARY KEY (`id`), UNIQUE KEY `uk_configinfo_datagrouptenant` (`data_id`,`group_id`,`tenant_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info'; /******************************************/ /* Full database name = nacos_config */ /* Table name = config_info_aggr */ /******************************************/ CREATE TABLE `config_info_aggr` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id', `data_id` varchar(255) NOT NULL COMMENT 'data_id', `group_id` varchar(255) NOT NULL COMMENT 'group_id', `datum_id` varchar(255) NOT NULL COMMENT 'datum_id', `content` longtext NOT NULL COMMENT 'content', `gmt_modified` datetime NOT NULL COMMENT 'Modification time', `app_name` varchar(128) DEFAULT NULL, `tenant_id` varchar(128) DEFAULT '' COMMENT 'Tenant field', PRIMARY KEY (`id`), UNIQUE KEY `uk_configinfoaggr_datagrouptenantdatum` (`data_id`,`group_id`,`tenant_id`,`datum_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Add tenant field'; /******************************************/ /* Full database name = nacos_config */ /* Table name = config_info_beta */ /******************************************/ CREATE TABLE `config_info_beta` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id', `data_id` varchar(255) NOT NULL COMMENT 'data_id', `group_id` varchar(128) NOT NULL COMMENT 'group_id', `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name', `content` longtext NOT NULL COMMENT 'content', `beta_ips` varchar(1024) DEFAULT NULL COMMENT 'betaIps', `md5` varchar(32) DEFAULT NULL COMMENT 'md5', `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time', `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Modification time', `src_user` text COMMENT 'source user', `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip', `tenant_id` varchar(128) DEFAULT '' COMMENT 'Tenant field', PRIMARY KEY (`id`), UNIQUE KEY `uk_configinfobeta_datagrouptenant` (`data_id`,`group_id`,`tenant_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info_beta'; /******************************************/ /* Full database name = nacos_config */ /* Table name = config_info_tag */ /******************************************/ CREATE TABLE `config_info_tag` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id', `data_id` varchar(255) NOT NULL COMMENT 'data_id', `group_id` varchar(128) NOT NULL COMMENT 'group_id', `tenant_id` varchar(128) DEFAULT '' COMMENT 'tenant_id', `tag_id` varchar(128) NOT NULL COMMENT 'tag_id', `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name', `content` longtext NOT NULL COMMENT 'content', `md5` varchar(32) DEFAULT NULL COMMENT 'md5', `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time', `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Modification time', `src_user` text COMMENT 'source user', `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip', PRIMARY KEY (`id`), UNIQUE KEY `uk_configinfotag_datagrouptenanttag` (`data_id`,`group_id`,`tenant_id`,`tag_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info_tag'; /******************************************/ /* Full database name = nacos_config */ /* Table name = config_tags_relation */ /******************************************/ CREATE TABLE `config_tags_relation` ( `id` bigint(20) NOT NULL COMMENT 'id', `tag_name` varchar(128) NOT NULL COMMENT 'tag_name', `tag_type` varchar(64) DEFAULT NULL COMMENT 'tag_type', `data_id` varchar(255) NOT NULL COMMENT 'data_id', `group_id` varchar(128) NOT NULL COMMENT 'group_id', `tenant_id` varchar(128) DEFAULT '' COMMENT 'tenant_id', `nid` bigint(20) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`nid`), UNIQUE KEY `uk_configtagrelation_configidtag` (`id`,`tag_name`,`tag_type`), KEY `idx_tenant_id` (`tenant_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_tag_relation'; /******************************************/ /* Full database name = nacos_config */ /* Table name = group_capacity */ /******************************************/ CREATE TABLE `group_capacity` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Primary key ID', `group_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Group ID,A null character indicates the entire cluster', `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Quota, 0 means the default value is used', `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Usage', `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'The maximum size of a single configuration, in bytes. 0 means the default value is used', `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum number of aggregate sub configurations, 0 means the default value is used', `max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'The upper limit of the sub configuration size of a single aggregate data, in bytes. 0 means the default value is used', `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum change history quantity', `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time', `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Modification time', PRIMARY KEY (`id`), UNIQUE KEY `uk_group_id` (`group_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Cluster, each Group Capacity information table'; /******************************************/ /* Full database name = nacos_config */ /* Table name = his_config_info */ /******************************************/ CREATE TABLE `his_config_info` ( `id` bigint(64) unsigned NOT NULL, `nid` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `data_id` varchar(255) NOT NULL, `group_id` varchar(128) NOT NULL, `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name', `content` longtext NOT NULL, `md5` varchar(32) DEFAULT NULL, `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `src_user` text, `src_ip` varchar(50) DEFAULT NULL, `op_type` char(10) DEFAULT NULL, `tenant_id` varchar(128) DEFAULT '' COMMENT 'Tenant field', PRIMARY KEY (`nid`), KEY `idx_gmt_create` (`gmt_create`), KEY `idx_gmt_modified` (`gmt_modified`), KEY `idx_did` (`data_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Multi tenant transformation'; /******************************************/ /* Full database name = nacos_config */ /* Table name = tenant_capacity */ /******************************************/ CREATE TABLE `tenant_capacity` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Primary key ID', `tenant_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Tenant ID', `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Quota, 0 means the default value is used', `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Usage', `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'The maximum size of a single configuration, in bytes. 0 means the default value is used', `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum number of aggregate sub configurations', `max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'The upper limit of the sub configuration size of a single aggregate data, in bytes. 0 means the default value is used', `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum change history quantity', `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time', `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Modification time', PRIMARY KEY (`id`), UNIQUE KEY `uk_tenant_id` (`tenant_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Tenant capacity information table'; CREATE TABLE `tenant_info` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id', `kp` varchar(128) NOT NULL COMMENT 'kp', `tenant_id` varchar(128) default '' COMMENT 'tenant_id', `tenant_name` varchar(128) default '' COMMENT 'tenant_name', `tenant_desc` varchar(256) DEFAULT NULL COMMENT 'tenant_desc', `create_source` varchar(32) DEFAULT NULL COMMENT 'create_source', `gmt_create` bigint(20) NOT NULL COMMENT 'Creation time', `gmt_modified` bigint(20) NOT NULL COMMENT 'Modification time', PRIMARY KEY (`id`), UNIQUE KEY `uk_tenant_info_kptenantid` (`kp`,`tenant_id`), KEY `idx_tenant_id` (`tenant_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='tenant_info'; CREATE TABLE `users` ( `username` varchar(50) NOT NULL PRIMARY KEY, `password` varchar(500) NOT NULL, `enabled` boolean NOT NULL ); CREATE TABLE `roles` ( `username` varchar(50) NOT NULL, `role` varchar(50) NOT NULL, UNIQUE INDEX `idx_user_role` (`username` ASC, `role` ASC) USING BTREE ); CREATE TABLE `permissions` ( `role` varchar(50) NOT NULL, `resource` varchar(255) NOT NULL, `action` varchar(8) NOT NULL, UNIQUE INDEX `uk_role_permission` (`role`,`resource`,`action`) USING BTREE ); INSERT INTO users (username, password, enabled) VALUES ('nacos', '$2a$10$EuWPZHzz32dJN7jexM34MOeYirDdFAZm2kuWj7VEOJhhZkDrxfvUu', TRUE); INSERT INTO roles (username, role) VALUES ('nacos', 'ROLE_ADMIN');
2.2. Download nacos
Nacos has a download address on GitHub: https://github.com/alibaba/nacos/tags , you can choose any version to download.
Only version 1.4.1 is used in this example:
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-bzxrrxng-1641344417995) (D: / idea / cloud demo / day02 springcloud02 / data / assets/image-20210409212119411.png)]
2.3 configuring Nacos
Unzip the package to any non Chinese directory, as shown in the figure:
[the external chain picture transfer fails, and the source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-i6jiqi31-1641344417996) (D: / idea / cloud demo / day02 springcloud02 / data / assets / image-202104021618843337. PNG)]
Catalog Description:
- bin: startup script
- conf: configuration file
Enter the conf directory of nacos and modify the configuration file cluster Conf.example, renamed cluster conf:
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-teu6rr6b-1641344417998) (D: / idea / cloud demo / day02 springcloud02 / data / assets/image-20210409212459292.png)]
Then add content:
127.0.0.1:8845 127.0.0.1.8846 127.0.0.1.8847
Then modify the application Properties file, add database configuration
spring.datasource.platform=mysql db.num=1 db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC db.user.0=root db.password.0=123
2.4 startup
Copy three copies of the Nacos folder, named nacos1, nacos2, and nacos3
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-6wr41xj8-1641344418000) (D: / idea / cloud demo / day02 springcloud02 / data / assets/image-20210409213335538.png)]
Then modify the application. In the three folders respectively properties,
nacos1:
server.port=8845
nacos2:
server.port=8846
nacos3:
server.port=8847
Then start three nacos nodes:
startup.cmd
2.5 nginx reverse proxy
Find the nginx installation package provided in the pre class materials:
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-h7slt3zs-1641344418001) (D: / idea / cloud demo / day02 springcloud02 / data / assets/image-20210410103253355.png)]
Unzip to any non Chinese Directory:
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-lgwxklmx-1641344418002) (D: / idea / cloud demo / day02 springcloud02 / data / assets/image-20210410103322874.png)]
Modify conf / nginx Conf file, configured as follows:
upstream nacos-cluster { server 127.0.0.1:8845; server 127.0.0.1:8846; server 127.0.0.1:8847; } server { listen 80; server_name localhost; location /nacos { proxy_pass http://nacos-cluster; } }
Then access in the browser: http://localhost/nacos Just.
Application. In the code The YML file configuration is as follows:
spring: cloud: nacos: server-addr: localhost:80 # Nacos address
2.6 optimization
-
During the actual deployment, you need to set a domain name for the nginx server as the reverse proxy, so that if a server migrates the nacos client in the future, there is no need to change the configuration
-
Each node of Nacos should be deployed to multiple different servers for disaster recovery and isolation
os node:
startup.cmd
2.5 nginx reverse proxy
Find the nginx installation package provided in the pre class materials:
[external chain picture transferring... (img-H7slT3Zs-1641344418001)]
Unzip to any non Chinese Directory:
[external chain picture transferring... (IMG lgwxklmx-1641344418002)]
Modify conf / nginx Conf file, configured as follows:
upstream nacos-cluster { server 127.0.0.1:8845; server 127.0.0.1:8846; server 127.0.0.1:8847; } server { listen 80; server_name localhost; location /nacos { proxy_pass http://nacos-cluster; } }
Then access in the browser: http://localhost/nacos Just.
Application. In the code The YML file configuration is as follows:
spring: cloud: nacos: server-addr: localhost:80 # Nacos address
2.6 optimization
-
During the actual deployment, you need to set a domain name for the nginx server as the reverse proxy, so that if a server migrates the nacos client in the future, there is no need to change the configuration
-
Each node of Nacos should be deployed to multiple different servers for disaster recovery and isolation