微服务中必不可少的组件可能就是注册中心了。注册中心的优点就不多说了,主要来看如何在 Docker 中搭建一个注册中心。
docker 中安装 Nacos
1、拉取镜像
[root@localhost ~]# docker pull nacos/nacos-server:2.0.32.0.3: Pulling from nacos/nacos-server 5ad559c5ae16: Pull complete 6119216bf5e5: Pull complete 49571fe4335c: Pull complete 91725161f3cf: Pull complete 53a8e3117049: Pull complete 8e310b7f7d64: Pull complete 181c5afae53f: Pull complete a807caf8a203: Pull complete 58881861df40: Pull complete 21be4d5690b9: Pull complete Digest: sha256:da74348293e1dbc489c07a48e0f170053ef7b22ec882e84e2254f190024bd996 Status: Downloaded newer image for nacos/nacos-server:2.0.3 docker.io/nacos/nacos-server:2.0.3
2、启动命令
[root@localhost ~]# docker run -d -e MODE=standalone -v /mydata/nacos/conf:/home/nacos/conf --name nacos -p 8848:8848 nacos/nacos-server:2.0.3 6f67c8be23f95cefbcc30ad5c211f0afa5d918283855431e4f8028dd24f18906
这里 Nacos 是以单机的方式启动的,这里并没有配置集群。
3、查看启动情况
[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6f67c8be23f9 nacos/nacos-server:2.0.3 "bin/docker-startup.…"4 minutes ago Up 4 minutes 0.0.0.0:8848->8848/tcp, :::8848->8848/tcp nacos 6284d4089225 redis "docker-entrypoint.s…"4 days ago Up 6 hours 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp mall-redis 4f6de6ccec9f mysql:5.7 "docker-entrypoint.s…"4 days ago Up 6 hours 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
4、查看无法访问的原因并处理
前面的步骤几乎都一样,但是我们这里 Nacos 并不能访问,因为我们指定的目录下确少配置文件。查看错误的方式通过 docker logs 命令来查看。
[root@localhost ~]# docker logs 6f+exportCUSTOM_SEARCH_NAMES=application,custom +CUSTOM_SEARCH_NAMES=application,custom +exportCUSTOM_SEARCH_LOCATIONS=/home/nacos/init.d/,file:/home/nacos/conf/ +CUSTOM_SEARCH_LOCATIONS=/home/nacos/init.d/,file:/home/nacos/conf/ …… Logging system failed to initialize using configuration from '/home/nacos/conf/nacos-logback.xml'java.io.FileNotFoundException: /home/nacos/conf/nacos-logback.xml (No such file or directory) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.<init>(FileInputStream.java:138) at java.io.FileInputStream.<init>(FileInputStream.java:93) at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90) at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188) at java.net.URL.openStream(URL.java:1068) at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:293) at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:264) at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:226) at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:203) at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) …… 15:26:43.963 [main] DEBUG org.springframework.boot.context.logging.ClasspathLoggingApplicationListener - Application failed to start with classpath: [jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/classes!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-config-2.0.3.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-boot-starter-web-2.1.17.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-boot-starter-json-2.1.17.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/jackson-datatype-jdk8-2.9.10.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/jackson-datatype-jsr310-2.9.10.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/jackson-module-parameter-names-2.9.10.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/hibernate-validator-6.0.20.Final.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/validation-api-2.0.1.Final.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/jboss-logging-3.3.3.Final.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/classmate-1.4.0.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-web-5.1.18.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-webmvc-5.1.18.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-api-2.0.3.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/reflections-0.9.11.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/javassist-3.21.0-GA.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/javax.annotation-api-1.3.2.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-core-2.0.3.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-common-2.0.3.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-consistency-2.0.3.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/hessian-4.0.63.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-auth-2.0.3.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-sys-2.0.3.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/jjwt-api-0.11.2.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/jjwt-impl-0.11.2.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/jjwt-jackson-0.11.2.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-test-5.1.18.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-boot-test-2.1.17.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/jraft-core-1.3.5.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/asm-6.0.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/rocksdbjni-5.18.4.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/jna-4.5.2.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/jctools-core-2.1.1.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/disruptor-3.3.7.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/commons-lang-2.6.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/hessian-3.3.6.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/metrics-core-4.0.7.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/rpc-grpc-impl-1.3.5.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/guava-30.1-jre.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/failureaccess-1.0.1.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/jsr305-3.0.2.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/checker-qual-3.5.0.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/j2objc-annotations-1.3.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-boot-starter-jdbc-2.1.17.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/HikariCP-3.4.2.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-jdbc-5.1.18.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-tx-5.1.18.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/commons-io-2.7.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/mysql-connector-java-8.0.21.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/derby-10.14.2.0.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/logback-classic-1.2.3.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/aspectjrt-1.9.6.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/cglib-nodep-2.1.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/httpasyncclient-4.1.3.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/httpcore-nio-4.4.13.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/commons-logging-1.2.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-boot-starter-tomcat-2.1.17.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/tomcat-embed-websocket-9.0.38.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/jackson-core-2.12.2.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/jackson-databind-2.12.2.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/jackson-annotations-2.12.2.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/micrometer-registry-prometheus-1.1.18.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/micrometer-core-1.1.18.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/HdrHistogram-2.1.9.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/LatencyUtils-2.0.3.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/simpleclient_common-0.5.0.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/micrometer-registry-influx-1.1.18.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/micrometer-registry-elastic-1.1.18.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-boot-starter-aop-2.1.17.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/aspectjweaver-1.9.6.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-boot-starter-actuator-2.1.17.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-boot-actuator-autoconfigure-2.1.17.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-boot-actuator-2.1.17.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/snakeyaml-1.23.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/tomcat-embed-jasper-9.0.40.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/tomcat-embed-core-9.0.38.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/tomcat-annotations-api-9.0.38.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/tomcat-embed-el-9.0.38.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/ecj-3.18.0.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-naming-2.0.3.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/netty-all-4.1.59.Final.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-boot-2.1.17.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-core-5.1.18.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-jcl-5.1.18.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-context-5.1.18.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/commons-collections-3.2.2.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/jackson-core-asl-1.9.13.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/slf4j-api-1.7.7.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/logback-core-1.2.3.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/mina-core-2.0.0-RC1.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/javatuples-1.2.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/httpcore-4.4.13.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/httpclient-4.5.12.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/commons-codec-1.11.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-cmdb-2.0.3.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-istio-2.0.3.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-client-2.0.3.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/simpleclient-0.5.0.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/grpc-netty-shaded-1.24.0.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/grpc-core-1.24.0.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/gson-2.8.6.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/annotations-4.1.1.4.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/perfmark-api-0.17.0.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/opencensus-api-0.21.0.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/opencensus-contrib-grpc-metrics-0.21.0.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/grpc-protobuf-1.24.0.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/grpc-api-1.24.0.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/grpc-context-1.24.0.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/error_prone_annotations-2.3.2.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/animal-sniffer-annotations-1.17.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/grpc-protobuf-lite-1.24.0.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/grpc-stub-1.24.0.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/proto-google-common-protos-1.17.0.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/protobuf-java-3.8.0.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/api-0.1.27.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/log4j-over-slf4j-1.7.30.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/jcl-over-slf4j-1.7.30.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/jul-to-slf4j-1.7.30.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-boot-starter-security-2.1.17.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-boot-starter-2.1.17.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-boot-autoconfigure-2.1.17.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-boot-starter-logging-2.1.17.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/log4j-to-slf4j-2.11.2.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/log4j-api-2.13.3.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-aop-5.1.18.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-beans-5.1.18.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-security-config-5.1.12.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-security-core-5.1.12.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-security-web-5.1.12.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-expression-5.1.18.RELEASE.jar!/, jar:file:/home/nacos/target/nacos-server.jar!/BOOT-INF/lib/spring-boot-jarmode-layertools-2.5.3.jar!/] 15:26:45.524 [background-preinit] DEBUG org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator - Loaded expression factory via original TCCL 15:26:45.773 [background-preinit] DEBUG org.hibernate.validator.internal.engine.ValidatorFactoryImpl - HV000234: Using org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator as ValidatorFactory-scoped message interpolator. 15:26:45.778 [background-preinit] DEBUG org.hibernate.validator.internal.engine.ValidatorFactoryImpl - HV000234: Using org.hibernate.validator.internal.engine.resolver.TraverseAllTraversableResolver as ValidatorFactory-scoped traversable resolver. 15:26:45.778 [background-preinit] DEBUG org.hibernate.validator.internal.engine.ValidatorFactoryImpl - HV000234: Using org.hibernate.validator.internal.util.ExecutableParameterNameProvider as ValidatorFactory-scoped parameter name provider. 15:26:45.780 [background-preinit] DEBUG org.hibernate.validator.internal.engine.ValidatorFactoryImpl - HV000234: Using org.hibernate.validator.internal.engine.DefaultClockProvider as ValidatorFactory-scoped clock provider. 15:26:45.781 [background-preinit] DEBUG org.hibernate.validator.internal.engine.ValidatorFactoryImpl - HV000234: Using org.hibernate.validator.internal.engine.scripting.DefaultScriptEvaluatorFactory as ValidatorFactory-scoped script evaluator factory. 15:27:37.181 [main] ERROR org.springframework.boot.SpringApplication - Application run failed java.lang.IllegalStateException: java.io.FileNotFoundException: /home/nacos/conf/nacos-logback.xml (No such file or directory)
从上面的日志信息中可以看出,确少配置文件。查看下面目录中的内容。
[root@localhost ~]# cd /mydata/nacos/conf[root@localhost conf]# ls
最简单的方式是从本地上传一份对应版本的配置文件到该目录下,然后重启容器。
docker restart nacos
这样,Nacos 就可以启动了。
注:如果机器配置低的话,Nacos 的启动会比较慢,多用 docker logs 进行查看,以确保 Nacos 真的启动了再进行使用。