Spark整合Nacos实时修改配置

一、Nacos

  • git clone https://github.com/alibaba/nacos.git  直接拉代码

  • 编译:mvn -Prelease-nacos clean install -U -Dmaven.test.skip=true  注意:不加跳过测试类编译会报错

  • 进去/opt/nacos/distribution/target/nacos-server-1.4.0-SNAPSHOT/nacos/conf

  • 在mysql中创建nacos_config库,建表语句在conf/nacos-mysql.sql

  • 修改application.properties 添加Mysql

Spark整合Nacos实时修改配置_第1张图片 启动就好了!!!

二、Spark配置:

pom:



    4.0.0

    org.example
    SparkLearning
    1.0-SNAPSHOT

    
        1.8
        1.8
        2.11.8
        2.2.0
        2.8.0
        UTF-8
    

    
        
        
            com.alibaba.nacos
            nacos-client
            1.1.4
        
        
        
            com.alibaba.nacos
            nacos-common
            1.1.4
        
        
        
            com.alibaba.nacos
            nacos-core
            0.8.0
        

        
            org.scala-lang
            scala-library
            ${scala.version}
        


        
            org.apache.spark
            spark-core_2.11
            ${spark.version}
        
        
            org.apache.spark
            spark-streaming_2.11
            ${spark.version}
        


        
            org.apache.hadoop
            hadoop-client
            ${hadoop.version}
        

        
            mysql
            mysql-connector-java
            5.1.38
        




        
        
            commons-httpclient
            commons-httpclient
            3.1
        



        
        
            com.alibaba
            fastjson
            1.2.62
        


    

    
        
            
                
                    net.alchim31.maven
                    scala-maven-plugin
                    3.2.2
                
                
                    org.apache.maven.plugins
                    maven-compiler-plugin
                    3.5.1
                
            
        
        
            
                net.alchim31.maven
                scala-maven-plugin
                
                    
                        scala-compile-first
                        process-resources
                        
                            add-source
                            compile
                        
                    
                    
                        scala-test-compile
                        process-test-resources
                        
                            testCompile
                        
                    
                
            

            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    
                        compile
                        
                            compile
                        
                    
                
            

            
                org.apache.maven.plugins
                maven-shade-plugin
                2.4.3
                
                    
                        package
                        
                            shade
                        
                        
                            
                                
                                    *:*
                                    
                                        META-INF/*.SF
                                        META-INF/*.DSA
                                        META-INF/*.RSA
                                    
                                
                            
                        
                    
                
            
        
    

主类代码:

import java.util.Properties
import java.util.concurrent.Executor

import com.alibaba.nacos.api.NacosFactory
import com.alibaba.nacos.api.config.ConfigService
import com.alibaba.nacos.api.config.listener.Listener
import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}

object NacosTest {
  def main(args: Array[String]): Unit = {

    val conf = new SparkConf().setAppName(s"${this.getClass.getSimpleName}").setMaster("local")
    val ssc = new StreamingContext(conf, Seconds(1))

    val serverAddr = "xx.xx.xx.xx"
    val dataId = "test"
    val group = "DEFAULT_GROUP"
    val properties = new Properties()
    properties.put("serverAddr", serverAddr)
    val configService: ConfigService = NacosFactory.createConfigService(properties)
    var content: String = configService.getConfig(dataId, group, 5000)
    println("config content - - ->" + content)
    configService.addListener(dataId, group, new Listener() {
      override def getExecutor: Executor = null

      override def receiveConfigInfo(configInfo: String): Unit = {
        content = configInfo
        println("open Listener receive - - ->" + configInfo)
      }
    })
    Thread.sleep(30000000000L)
    println("obtain config infomation - - ->" + content)
    ssc.start()
    ssc.awaitTermination()
  }
}

三、Nacos页面配置

   Spark整合Nacos实时修改配置_第2张图片

Spark整合Nacos实时修改配置_第3张图片完成,快来试试吧!!!

你可能感兴趣的:(Spark,Streaming,spark,后端)