Spring Cloud Alibaba (II) Environment construction and NACOS registry configuration center

Technology selection

Maven development tools: IDEA registry, configuration center: nacOS-2.0.3 Spring-cloud Spring-cloud-Alibaba API gateway: Spring-cloud-Alibaba -gateway database: MySQL 5.7 noSQL: redis Distributed lock: redisson Persistence layer: mybatis- Plus Full link blue green gray publishing DISASTER recovery, gateway dynamic routing: Discovery -6.12.1 Distributed Transactions: SeATA Service Invocation: OpenFeign Service meltdown and Downgrade: HystrixCopy the code

Architecture design

I draw not good, do not spray me 🐶

First, Nacos installation and startup

1. Nacos is started independently

  1. Start by downloading NacOS

Github download: github.com/alibaba/nac…

Baidu cloud download address: pan.baidu.com/s/1n1l1lf8B… Extraction code: 41A5

  1. After decompressing, go to the nacos/bin directory

  2. Enter the command to start the service

Run the following command to start Linux standalone: sh startup.sh -m standalone

Windows: 'CMD startup. Cd-m standalone'Copy the code

See this is basically a start-up success, but through http://127.0.0.1:8848/nacos/index.html into the cabin of the console interface, the default username/password is nacos/nacos

2. The Nacos cluster starts

Three or more Nacos nodes can form a cluster. Only Linux/Unix/MAC nodes are supported

In the conf directory of the nacOS decompression directory, there is the configuration file cluster.conf (if there is no configuration file, manually create it), and set each line to IP :port. (Configure three or more nodes)

# cluster. The conf 192.168.0.1:8848 192.168.0.2:8848 192.168.0.3:8848Copy the code

After the configuration, run the sh startup.sh command on each node to start all services

3. Configure the Mysql

Embedded database is used by default, mysql data source support has been added after version 0.7

Initialize nacos related tables: run the conf/nacos – mysql. SQL file to modify the conf/application. The properties files, increase support for mysql data source configuration (currently only supports mysql), add a mysql data source url, user name and password

Spring. The 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 db.user=user db.password=passwordCopy the code

After the configuration, enter the command to start the service (refer to the subsequent command to start the service above).

  1. Nacos cluster startup 3. Mysql configuration is all borrowed from others, WE have not tried, have a problem don’t find me, find me I will not 🐶

Ii. Construction project

1. Create a skeleton-free Maven project as the parent project

Delete SRC and everything else, leave a POM, introduce a dependency as the parent dependency, and then the following child dependencies can be written without version numbers.

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.312..RELEASE</version>
    </parent>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud-alibaba.version>2.26..RELEASE</spring-cloud-alibaba.version> <spring-cloud.version>Hoxton.SR12</spring-cloud.version> </properties> <! - global into downloading depend on the address, will not introduce dependence - > < dependencyManagement > < dependencies > <! - download springcloud warehouse - > < the 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> <! --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>${spring-cloud-alibaba.version}</version>  </dependency> <! --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>${spring-cloud-alibaba.version}</version> </dependency> </dependencies> </dependencyManagement>Copy the code

Ncaos Registry

Create a new Module: zhc-common, which will place the common entity classes and the common utility classes and dependencies, so you don’t need to start the class and test

<? xml version="1.0" encoding="UTF-8"? > <project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>com.zhc.cloud</groupId>
        <version>0.01.-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0. 0</modelVersion>

    <artifactId>zhc-common</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
Copy the code

Then build two modules as above, one zhc-business and one ZHc-system. Then the common class introduces the Nacos registration discovery dependency and the SpringBoot Web dependency, and the two new modules introduce common

Then take the ZHc-system service as an example

  1. Lead rely on
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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> < the dependency > < groupId > com. ZHC. Cloud < / groupId > < artifactId > ZHC - common < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > </dependency> </dependencies>Copy the code
  1. Writing configuration files

Create the Resource folder and the application.yml file and write the configuration

Spring # nacOS Cloud: nacOS: # nacOS Discovery configuration Discovery: # Override local configuration with nacOS configuration center configuration Enabled: true # group group: Metadata: env: env1 group: ZHC_GROUP region: Dev version: 1.0 zone: zone1 # namespace ID: ZHC_GROUP # username: nacos # password: The default value is true register-enabled: true #nacos address server-addr: 127.0.0.1:8848 #nacos project name service: ZHC-SYSTEM-SERVICECopy the code
  1. Start the class
@ SpringBootApplication / / SpringBoot @ EnableConfigurationProperties / / open services registered public class ZhcSystemStartApplication { public static void main(String[] args) { SpringApplication.run(ZhcSystemStartApplication.class, args); }}Copy the code
  1. Services are registered with NACOS

Create a namespace in NACOS

Once created, start the project and see that zhc-system-service can be registered with nCAOS.

Ncaos configuration center

  1. Writing configuration files

Create the bootstrap.yml file and write the configuration

Spring: Application: name: zhc-system cloud: nacos: # nacos config Config: # enabled: false # nacos Cluster address Note: multiple IP can pass ", "isolation, such as 192.168.80.1:8848192168 80.1: fill in the domain name prefix don't add http:// server - 8848 addr: 127.0.0.1:8848 # Namespace ID Namespace: ZHC_GROUP # account username: nacos # Password password: nacos # Maximum number of retries in primary configuration max-retry: 5 # config-long-poll-timeout: 46000 # config-retry-time: 2000 # Primary configuration enable the registration listener preloading configuration service (this parameter is not recommended unless there are special service requirements) enable-remote-sync-config: true # primary configuration data-id name: Zhc-system # primary configuration group-id group: ZHC_GROUP # primary configuration fileExtension: Override: true # allow-override: true # override-system-properties: falseCopy the code
  1. Nacos configuration

Created under the ZHC_GROUP group

Configure the ZHc-system project after creation

Log printing this indicates that the nacOS log was successfully read

Source code: github.com/zhc-1999/sp… I built the project first and then made up the document. So the project and the document may be different, please ask me if you have any questions.)