Introduction to the

Nacos. IO/useful – cn/docs /…

Install & configure nacOS

The installation
  1. download

Wget HTTP: / / https://github.com/alibaba/nacos/releases/download/2.0.0-BETA/nacos-server-2.0.0-BETA.zip

  1. Unpack the
Unzip nacos - server - 2.0.0 - BETA.zip CD nacos/binCopy the code
  1. Start the
  • Stand-alone mode
# Linux/Unix/Mac
sh startup.sh -m standalone 
# ubuntu
bash startup.sh -m standalone
# windows
cmd startup.cmd -m standalone
Copy the code
  • Cluster pattern
sh startup.sh
bash startup.sh
cmd startup.cmd
Copy the code
  1. page

The default port is 8848, visit http://127.0.0.1:8848/nacos

The default username and password are both nacos, so you can access the page normally without any problems

  1. Data persistence

IO /zh-cn/docs/… Create the database and table according to the SQL statement nacos-mysql. SQL in the configuration file, connect to mysql as follows

Spring. The datasource. Platform = mysql db. Num = 1 db. Url. 0 = JDBC: mysql: / / 11.162.196.16:3306 / nacos_devtest? characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true db.user=nacos_devtest db.password=youdontknowCopy the code

Pay attention to the high availability of mysql. Otherwise, nacOS and subsequent service configurations may fail to be obtained

  1. Configuring startup
  • The new file

vim /lib/systemd/system/nacos.service

[Unit] Description=nacos After=network.target [Service] Environment="JAVA_HOME= XXX /jdk1.8.0_151" Type=forking # Note that there is a stand-alone mode or cluster mode ExecStart = XXX/nacos nacos/bin/startup. Sh -m standalone ExecReload = XXX/nacos nacos/bin/shutdown. Sh ExecStop=xxx/nacos/nacos/bin/shutdown.sh PrivateTmp=true [Install] WantedBy=multi-user.targetCopy the code
  • Overloading service

systemctl daemon-reload

  • Setting boot

systemctl enable nacos.service

  • View the startup status of the service

systemctl is-enabled nacos.service

  • Start the service
systemctl start nacos.service
Copy the code
  • View the running status of the service

systemctl status nacos.service

Environment=”JAVA_HOME= XXX /jdk1.8.0_151″; Environment=”JAVA_HOME= XXX /jdk1.8.0_151″ In addition, set the javA_HOME directory in startup.sh as its own path

abnormal

Mar 05 10:00:40 iz2ze70su5hanmrh2q4quuz startup.sh[1164]: readlink: missing operand Mar 05 10:00:40 iz2ze70su5hanmrh2q4quuz startup.sh[1164]: Try 'readlink --help' for more information. Mar 05 10:00:40 iz2ze70su5hanmrh2q4quuz startup.sh[1164]: dirname: missing operand Mar 05 10:00:40 iz2ze70su5hanmrh2q4quuz startup.sh[1164]: Try 'dirname --help' for more information. Mar 05 10:00:40 iz2ze70su5hanmrh2q4quuz startup.sh[1164]: ERROR: Please set the JAVA_HOME variable in your environment, We need java(x64)! jdk8 or later is better! !!!!! Mar 05 10:00:40 iz2ze70su5hanmrh2q4quuz systemd[1]: nacos.service: control process exited, code=exited status=1 Mar 05 10:00:40 iz2ze70su5hanmrh2q4quuz systemd[1]: Failed to start nacos. Mar 05 10:00:40 iz2ze70su5hanmrh2q4quuz systemd[1]: Unit nacos.service entered failed state. Mar 05 10:00:40 iz2ze70su5hanmrh2q4quuz systemd[1]: nacos.service failed.Copy the code

Configuration startup. Sh

. [! - e "$JAVA_HOME/bin/Java"] && JAVA_HOME = / XXX/jdk1.8.0 _211 # [! - e "$JAVA_HOME/bin/Java"] && JAVA_HOME=$HOME/jdk/java #[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=/usr/java #[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=/opt/taobao/java #[ ! -e "$JAVA_HOME/bin/java" ] && unset JAVA_HOME ...Copy the code
  • stop

systemctl stop nacos.service

configuration
  1. The namespace

By default, there is a public reserved space. I create a space named after my user name, for example, Zhang SAN. If the id of the space is not specified by myself, it will automatically generate a unique ID.

  1. Configuration center

For the configuration of different users and different environments, my space is named in the order of user > Environment > project

This isolates configuration files for different users, environments, and projects

After the NACOS cluster is deployed, the configuration files are centrally managed for later maintenance

  1. Configure the central file configuration
  • First, in a new configuration file in the configuration center, such as test-application-dev.yml

Configuration is as follows

server: port: 8083 spring: application: name: test-application cloud: nacos: discovery: server-addr: 127.0.0.1:8848 config: server-addr: 127.0.0.1:8848 group: DEV_GROUP gateway: Discovery: locator: enabled: true routes: -id: test # target service address URI: http://www.baidu.com # Predicates: # exposure: include: "*"Copy the code
  1. Configure your own engineering projects
  • Yml and application. Yml are created under resource to distinguish different environments during packaging. The loading priority of bootstrap.yml is higher than that of application

  • Configure the bootstrap. Yml
server:
  port: ${server.port}
spring:
  application:
    name: @nacosAppName@
  cloud:
    nacos:
      discovery:
        server-addr: @nacosAddr@
        namespace: @nacosNamespace@
        group: @nacosGroup@
      config:
        server-addr: @nacosAddr@
        prefix: ${spring.application.name}
        file-extension: yml
        namespace: @nacosNamespace@
        group: @nacosGroup@
    sentinel:
      transport:
        dashboard: @nacosAddr@
Copy the code

The parameters here are dynamically captured in the POM to differentiate the environment at packaging time

  • pom.xml
. <! -- Environment information --> <profiles> <! Properties > <nacosAddr> Localhost :8848</nacosAddr> <nacosNamespace>f6cf53e2-4721-4866-9rdf-b65812e8227b</nacosNamespace> <nacosAppName>test-gateway-dev</nacosAppName> <nacosGroup>DEV_GROUP</nacosGroup> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <! --> <profile> <id>test</id> <properties> <nacosAddr> <nacosNamespace>f6cf53e2-4721-4866-9rdf-b65812e8227b</nacosNamespace> <nacosAppName>test-application-test</nacosAppName>  <nacosGroup>TEST_GROUP</nacosGroup> </properties> <activation> <activeByDefault>false</activeByDefault> </activation> </profile> <! Properties > <nacosAddr> Localhost :8848</nacosAddr> <nacosNamespace>f6cf53e2-4721-4866-9rdf-b65812e8227b</nacosNamespace> <nacosAppName>test-application-prod</nacosAppName>  <nacosGroup>PROD_GROUP</nacosGroup> </properties> <activation> <activeByDefault>false</activeByDefault> </activation> </profile> </profiles> ...Copy the code

Above, start the NACOS service, and then start the project, generally will read the configuration information in the configuration Chinese, and the above has configured the service discovery function, so in nacOS can also see the automatic registration service, simple registry and configuration center roughly for this.

By the way, the corresponding problem between nacOS SpringCloud and Springboot version is mentioned. The springboot version used in the project is version 2.4.1, but nacOS currently officially supports the highest version as followsGithub.com/alibaba/spr…

So in the introduction, there will be some unexpected problems with the initial version, and after many trials, the final successful introduction of the dependency

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> The < version > 2.2.6. RELEASE < / version > < / dependency > <! -- monitoring module --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-starter -actuator</artifactId> The < version > against 2.4.1 < / version > < / dependency > <! --> <dependency> <groupId>com.alibaba.cloud</groupId> < artifactId > spring - the cloud - starter - alibaba - nacos - config < / artifactId > < version > 2.2.5. RELEASE < / version > < exclusions > <exclusion> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-context</artifactId> </exclusion> </exclusions> </dependency> <! --> <dependency> <groupId>com.alibaba. Cloud </groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>${nacos-discovery.version}</version> <exclusions> <exclusion> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-context</artifactId> </exclusion> </exclusions> </dependency> <! -- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-bootstrap --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> < version > 3.0.0 < / version > < exclusions > < exclusion > < groupId > org. Springframework. Cloud < / groupId > <artifactId>spring-cloud-starter</artifactId> </exclusion> </exclusions> </dependency> <! -- The dependence does not add, Request the registration of the service to 503 when mistakes -- -- > < the dependency > < groupId > org. Springframework. Cloud < / groupId > < artifactId > spring - the cloud - starter - loadbalancer < / artifactId > < version > 3.0.0 < / version > < / dependency > <! --> <dependency> <groupId>com.alibaba. Cloud </groupId> < artifactId > spring -- cloud - starter - alibaba - sentinel < / artifactId > < version > 2.2.5. RELEASE < / version > < / dependency >Copy the code

Security issues

Nacos has security issues, search the Internet and there are plenty

  • zhuanlan.zhihu.com/p/349905688
  • Github.com/alibaba/nac…
  • Github.com/alibaba/spr…

So nacOS port 8848 should not be exposed to the public network environment

【 Summary 】

Error: SpringCloud + Gateway

Whitelabel Error Page
This application has no configured error view, so you are seeing this as a fallback.

Thu Mar 04 11:14:50 CST 2021
[29ad555a-1] There was an unexpected error (type=Service Unavailable, status=503).
Copy the code

Tried a variety of ways to solve the dead or alive, once suspected life, eventually, in the plug in doubt is the version of the problem, in the issues found a solution github.com/alibaba/nac… Problem solved. After returning to 503, there are no other clues in the log, and I have no idea what the problem is, or because I am not familiar with this area. If you want to implement load balancing on the client side, such as @loadBalanced or lb://service_name, you need to import packages