zoukankan      html  css  js  c++  java
  • SpringCloud入门系列0-Nacos的安装与配置

    • 背景 

        工作有一些年头了,自从19年初彻底转了java(这又是另一篇心酸的故事),突然感觉自己荒废了好几年(不是说.net不好,而是回顾自己这几年做的很多东西都浮于表面,有时候弄成很忙的样子,回头看看自己以前写的博客,竟然很多是关于js的,真是感觉浪费时间,在此也奉劝各位老铁,做后端就好好深入做,不要分太多心到前端上,并不会给自己加分,当然全职转前端的除外)

        (路人甲:扯蛋一个顶俩,能不能赶紧说技术?)

        OKOK,书接上回,西门庆刚刚脱掉....你们想听这个我偏偏不说,其实是最近团队内部要重构一个服务业务,时间不那么紧张,架构搭建由本人负责,没有强制要求用什么实现,用微服务说实话挺没底的,但是时间又比较充裕,所以还是决定硬着头皮上一波(路人乙:波在哪里??要扯淡能不能专业点?最好带黄图的那种)

        本系列的目的主要是交流和记录,希望本系列结束的时候可以让您的工资翻一倍(前提是你工资只有3K啊:))

        万事开头难,毕竟之前没有什么微服务经验。最开始是一个组件一个组件查教程,然后一步一步集成,所以最开始的时候用的是主流的springcloud一波流:consul+zuul+hystrix,监控方面SpringbootAdmin+HystrixDashboard,随着我的不断深入(别想歪),发现很多组件已经不维护或者有更好的代替,所以楼主一步错步步错,最后步入了风尘之路...对不起串台了,马上开始

    • 技术点

        整体采用SpringCloud作为底部支撑,采用相对较活跃的组件,其中涉及的有(此处应该有一张很炫的图,大家可自行脑补,我实在懒得画:))

          Nacos-服务注册、发现与配置中心(期间还用了eureka)

          SpringcloudGateway-网关 

          Sentinel+dashboard-熔断降级,也叫断路器,这个是阿里出品的哨兵,一线互联网公司很多在用

          Zipkin-链路跟踪

          Elasticsearch+kibana-链路跟踪数据持久化以及查询

          SpringbootAdmin-整体资源监控

          Quartz-定时任务集成

          JWT-用户认证,自己实现了一套简单的jwt

          

        

        各组件版本列表

    名称 版本 git/maven 参考网址
    SpringBoot 2.1.9    
    SpringCloud Greenwich.SR3    
    Nacos(docker) 1.1.4 https://github.com/alibaba/nacos https://nacos.io/
    SpringcloudGateway 2.1.3 https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-gateway  
    Sentinel 2.1.1

    https://github.com/alibaba/Sentinel

    https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-sentinel

    https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D
    Zipkin-Slim(docker)  latest  https://github.com/openzipkin/zipkin  
    Elasticsearch(docker) 6.8.5  https://github.com/elastic/elasticsearch  
    Elasticsearch-head(node)      
    kibana(docker) 6.8.5    
    Quartz 2.1.9  https://github.com/quartz-scheduler/quartz  
           

        

        后续文章我将逐步展开每个组件的安装配置,各位大爷敬请期待

        开篇我就以nacos和ek的安装和配置作为前戏吧,俗话说前戏做的足,后面高潮多嘛(手动狗头)

    • Nacos

        Nacos是阿里18年开源的产品,目前更新非常活跃,属于springcloud-alibaba的一部分,文档也比较多,这里就不比较几个注册中心的对比了,也不复制nacos的介绍文档了,入门介绍可以看上表中的网址。

        直接开干:

        由于我的电脑配置不高,我选择安装在远程一台测试机的docker上,建议你也这么干

        直接一把梭:

        docker run --env MODE=standalone 
        -v /dockerdata/nacos/config/application.properties:/home/nacos/conf/application.properties
        --name nacos -d -p 8848:8848 nacos/nacos-server:1.1.4
       这里映射了8848端口,选择挂载了一个配置文件,配置文件内容如下
        
    # spring
    
    server.contextPath=/nacos
    server.servlet.contextPath=/nacos
    server.port=8848
    
    # nacos.cmdb.dumpTaskInterval=3600
    # nacos.cmdb.eventTaskInterval=10
    # nacos.cmdb.labelTaskInterval=300
    # nacos.cmdb.loadDataAtStart=false
    
    
    # metrics for prometheus
    #management.endpoints.web.exposure.include=*
    
    # metrics for elastic search
    management.metrics.export.elastic.enabled=false
    #management.metrics.export.elastic.host=http://localhost:9200
    
    # metrics for influx
    management.metrics.export.influx.enabled=false
    #management.metrics.export.influx.db=springboot
    #management.metrics.export.influx.uri=http://localhost:8086
    #management.metrics.export.influx.auto-create-db=true
    #management.metrics.export.influx.consistency=one
    #management.metrics.export.influx.compressed=true
    
    server.tomcat.accesslog.enabled=true
    server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D %{User-Agent}i
    # default current work dir
    server.tomcat.basedir=
    
    ## spring security config
    ### turn off security
    #spring.security.enabled=false
    #management.security=false
    #security.basic.enabled=false
    #nacos.security.ignore.urls=/**
    
    nacos.security.ignore.urls=/,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/login,/v1/console/health/**,/v1/cs/**,/v1/ns/**,/v1/cmdb/**,/actuator/**,/v1/console/server/**
    
    # nacos.naming.distro.taskDispatchPeriod=200
    # nacos.naming.distro.batchSyncKeyCount=1000
    # nacos.naming.distro.syncRetryDelay=5000
    # nacos.naming.data.warmup=true
    # nacos.naming.expireInstance=true
    
    nacos.istio.mcp.server.enabled=false
    
    spring.datasource.platform=mysql
    
    db.num=1
    db.url.0=jdbc:mysql://xxxx:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false&useAffectedRows=true
    db.user=root
    db.password=123456

      实际上大部分都是默认的配置文件,如果你本地测试过安装nacos,可以对比下默认的配置前面基本是一样的,我修改了后面

      spring.datasource.platform=mysql 这一行,代表数据存储在mysql中,后面db开头的参数都是mysql的配置,而nacos官方也提供了默认数据库的表结构,地址为:https://github.com/alibaba/nacos/blob/409838fe1f2dc53f7f5c63c660cd69b0fae1d49e/config/src/main/resources/META-INF/nacos-db.sql
     在你自己的mysql中建表就好,至于为啥我要保存到mysql中,自然是为了后面做准备了,这里先卖个关子,照做就好.
     安装好后启动容器,配置好端口防火墙就可以访问了(默认用户名和密码都是nacos,想修改的话去改users表就好了,密码生成规则在nacos源码里,由看官自行查找,三年之后在评论里公布方法。。谁的臭鸡蛋??乱扔杂物罚款五毛)
      

         

      

    • ES以及相关组件安装

        (这部分跟微服务关系不大,如果你不需要数据相关的要求,这部分可以跳过)

    1. Elasticsearch

        我也不详细解释了,网上安装的教程多如牛毛,直接上命令

        

    docker run  -e xpack.security.enabled=true 
                        -e xpack.security.transport.ssl.enabled=true 
                        -e ES_JAVA_OPTS="-Xms1024m -Xmx1024m" 
                        -d -p 9200:9200 -p 9300:9300 
                        -v /dockerdata/es/config/master.yml:/usr/share/elasticsearch/config/elasticsearch.yml 
                        -v /dockerdata/es/master:/usr/share/elasticsearch/data 
                        -v /dockerdata/es/config/log4j2.properties:/usr/share/elasticsearch/config/log4j2.properties 
                        --name es-master elasticsearch:6.8.5
    

        还是稍加解释下:映射的是9200(9300是通讯接口),挂载了配置文件master.yml,数据目录,以及日志文件,这里给出具体配置

        master.yml

        

    cluster.name: elasticsearch-cluster
    node.name: master
    network.bind_host: 0.0.0.0
    network.publish_host: xxxx
    network.host: 0.0.0.0
    http.port: 9200
    transport.tcp.port: 9300
    http.cors.enabled: true
    http.cors.allow-origin: "*"
    node.master: true
    node.data: true
    discovery.zen.ping.unicast.hosts: ["xxxx:9300","xxxx:9301"]
    http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
    

      

        log4j2.properties

    appender.rolling.type = RollingFile
    appender.rolling.name = rolling
    appender.rolling.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}.log
    appender.rolling.layout.type = PatternLayout
    appender.rolling.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] [%node_name]%marker %.-10000m%n
    appender.rolling.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}-%d{yyyy-MM-dd}-%i.log.gz
    appender.rolling.policies.type = Policies
    appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
    appender.rolling.policies.time.interval = 1
    appender.rolling.policies.time.modulate = true
    appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
    appender.rolling.policies.size.size = 256MB
    appender.rolling.strategy.type = DefaultRolloverStrategy
    appender.rolling.strategy.fileIndex = nomax
    appender.rolling.strategy.action.type = Delete
    appender.rolling.strategy.action.basepath = ${sys:es.logs.base_path}
    appender.rolling.strategy.action.condition.type = IfFileName
    appender.rolling.strategy.action.condition.glob = ${sys:es.logs.cluster_name}-*
    appender.rolling.strategy.action.condition.nested_condition.type = IfAccumulatedFileSize
    appender.rolling.strategy.action.condition.nested_condition.exceeds = 1GB
    

      

          ps:这里有一个坑,log4j2的日志文件如果你复制过去可能会有空格,注意检查每一行的前后空格,否则会出意外错误

    1.   head插件安装教程网上也很多,这里只提一个坑,不要使用docker安装head,原因是:
        官方docker镜像没有同步git主版本,所以存在一个bug:所有接口调用都报406错误,原因是es新版更新了请求机制,需要指定content-type

          ps:由于es开启了xpack,所以head访问时要在参数上添加用户名密码才可以访问,类似这样 http://xxxx:9100/?auth_user=elastic&auth_password=123456

    1.   kibana:(注意kibana要与es的版本一致)

          

    docker run -l es-master:elasticsearch -p 5601:5601 -v /dockerdata/es/kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml -d kibana:6.8.5
    

          

          
        kibana.yml:
        
    server.name: kibana
    server.host: "0"
    elasticsearch.url: http://xxxx:9200
    xpack.monitoring.ui.container.elasticsearch.enabled: true
    
    # Elasticsearch xpack
    elasticsearch.username: "elastic"
    elasticsearch.password: "123456"

      

    • 总结

       本文给出本系列涉及到的组件列表以及nacos的安装配置以及没有什么存在感的elk相关配置安装,而每一个组件的安装其实都有很多坑,不过大家可以自行查找,不属于本人原创的内容就不粘贴了

        整体来说这篇并没有什么干货(逃命中。。) 后续逐步展开各组件的使用和集成,尽快让各位老铁步入主题,覆雨翻云、高潮迭起:) 谢谢观看

        

        

  • 相关阅读:
    MOSS中的User的Title, LoginName, DisplayName, SID之间的关系
    如何在Network Monitor中高亮间隔时间过长的帧?
    SharePoint服务器如果需要安装杀毒软件, 需要注意什么?
    如何查看SQL Profiler? 如何查看SQL死锁?
    什么是Telnet
    The name or security ID (SID) of the domain specified is inconsistent with the trust information for that domain.
    Windows SharePoint Service 3.0的某个Web Application无搜索结果
    网络连接不上, 有TCP错误, 如果操作系统是Windows Server 2003, 请尝试一下这里
    在WinDBG中查看内存的命令
    The virtual machine could not be started because the hypervisor is not running
  • 原文地址:https://www.cnblogs.com/dzxw2371/p/12172949.html
Copyright © 2011-2022 走看看