安装Spring+搭建Spring开发环境

https://blog.csdn.net/csdnsjg/article/details/80152815

https://jingyan.baidu.com/article/219f4bf798e0cfde442d3831.html

 

类似spring 、 maven 这类插件,需要再eclipse中提前安装;

 

1、首先下载eclipse 对应的spring 的版本,查看eclipse 版本在 help-- about eclipse 中

2、找到打开eclipse,选择 "Help" --> "Install New Software" --> "add" -->"Archive" --> 选择刚才下载好的springsource-tool-suite “打开”--> "OK"

3、查看是否安装成功,点击“Window”--> "Preferences"查看是否安装成功

 

 

配置好后,新建spring 调用jsf 接口:

1、 file --- new -- spring starter project 

2、maven 依赖配置添加,在pom.xml 中,可以依照官方的xml文档,只需要添加需要的依赖即可

3、在spring 项目下面,新建源文件,里面包含log4j.xml  和 jsf-consumer.xml  也是依据官方的文档进行修改,传入参数改称需要的即可

4、然后修改接口调用函数,导入相关的包,这里采用spring集成的方式调用接口

5、参考如下:

package com.example.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jd.erqing.application.jsf.VenderInfoService;
import com.jd.fastjson.JSONObject;

public class DemoApplicationTests {

	/**
	 * SLF4J logger
	 */
	private final static Logger LOGGER = LoggerFactory.getLogger(DemoApplicationTests.class);

	/**
	 * Method Name main
	 * 
	 * @param args Return Type void
	 */
	public static void main(String[] args) {
		ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("/jsf-consumer.xml");

		VenderInfoService service = (VenderInfoService) appContext.getBean("venderInfoService");
	    while (true) {
			try {
				long start = System.currentTimeMillis();
		        String result = service.venderDownAmountList("10021");
		        long end = System.currentTimeMillis();
		        System.out.println("耗时:"+(end - start));
		        System.out.println(result);
				LOGGER.info("response msg from server :{}", result);
				
			} catch (Exception e) {
				LOGGER.error(e.getMessage(), e);
			}

			try {
				Thread.sleep(2000);
			} catch (Exception e) {
			}
		}
	}
}

  

还有使用API的方式(不依赖spring)调用JSF服务的方式。

 实例如下:

package com.example.demo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jd.erqing.application.jsf.VenderInfoService;
import com.jd.fastjson.JSONObject;

import com.jd.jsf.gd.config.ConsumerConfig;
import com.jd.jsf.gd.config.RegistryConfig;





public class DemoApplication {

	/**
	 * SLF4J logger
	 */
	private final static Logger LOGGER = LoggerFactory.getLogger(DemoApplication.class);

	/**
	 * Method Name main
	 * 
	 * @param args Return Type void
	 */
	public static void main(String[] args) {
//		// 引入杰夫接口
//		ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("/jsf-consumer.xml");
//		VenderInfoService service = (VenderInfoService) appContext.getBean("venderInfoService");
		
		
		RegistryConfig jsfRegistry = new RegistryConfig();
		jsfRegistry.setIndex("i.jsf.jd.com");
		LOGGER.info("实例RegistryConfig");

		// 服务提供者连接注册中心,设置属性
		ConsumerConfig<VenderInfoService> consumerConfig = new ConsumerConfig<VenderInfoService>();
		consumerConfig.setInterfaceId("XXXXXXXXXXXXXXXXXXXXXXXXX");
		// consumerConfig.setAlias("XXXXXXXXXXXXXXXXX");  // 线下
		consumerConfig.setAlias("XXXXXXXXXXXXXXXXXXXX");     //线上
		consumerConfig.setProtocol("jsf");
		Map<String,String> parameter = new HashMap();
		parameter.put(".token", "XXXXXXXXXXXXXXXXXXXX");
//		parameter.put("hide", "true");
		
		consumerConfig.setParameters(parameter);

		// 如果外面已经有xml配的注册中心,使用consumerConfig.setRegistry(RegistryFactory.getRegistryConfigs());
		consumerConfig.setRegistry(jsfRegistry);
		LOGGER.info("实例ConsumerConfig");
		VenderInfoService service = consumerConfig.refer();
		LOGGER.info("得到调用端代理:{}", service);
		
		
//		// 读入店铺id数据
//		SparkSession spark = SparkSession.builder().appName("demo_spark").enableHiveSupport().getOrCreate();
//		spark.sql("insert overwrite table dev.dev_spark_demo_temp partition (dt = '2019-08-08') select pop_vender_id, sale_ord_id from app.app_sjzt_payout_apply_with_order where  dt = '2019-08-05'");
//		Dataset<Row> vender_set = spark.sql("select pop_vender_id from app.app_sjzt_payout_apply_with_order where dt = '2019-08-05' limit 2000");
//
//		// 将数据去重,转换成 List<Row> 格式
//		vender_set =  vender_set.distinct();
//		vender_set = vender_set.na().fill(0L);
//		JavaRDD<Row> vender= vender_set.toJavaRDD();
//		List<Row> vender_list = vender.collect();
//		
//		// 遍历商家id,调用jsf接口
//		for(Row row:vender_list) {
//			String id = row.getString(0);
//			String result = service.venderDownAmountList(id);
//			System.out.print(id + " "+ result + "\n");
//		}
//		
//		System.out.println( "Hello World!" );
		
		
	    while (true) {
			try {
				long start = System.currentTimeMillis();
		        String result = service.venderDownAmountList("10021");
		        long end = System.currentTimeMillis();
		        System.out.println("耗时:"+(end - start));
		        System.out.println(result);
				LOGGER.info("response msg from server :{}", result);
				
			} catch (Exception e) {
				LOGGER.error(e.getMessage(), e);
			}

			try {
				Thread.sleep(2000);
			} catch (Exception e) {
			}
		}
	}
}

  

posted @ 2018-08-01 21:27  静悟生慧  阅读(567)  评论(0编辑  收藏  举报