GithubHelp home page GithubHelp logo

Comments (4)

happyfish100 avatar happyfish100 commented on September 28, 2024

友情提示:TrackerClient和StorageClient都是非线程安全的,需要每个线程实例化一份。

from fastdfs-client-java.

Puffin86 avatar Puffin86 commented on September 28, 2024

每次进行上传文件的时候,都已经实例化TrackerClient和StorageClient;
`public class FastDFSTest{
public static void main(String[] args) throws IOException, MyException{
String local_filename = "D:\smallData.xml";
FastDFSBean fb = new FastDFSBean();
fb.setFileName("TestFile");
fb.setFileExt("xml");
File file = new File(local_filename);
fb.setFile(file);

	Resource resource = new FileSystemResource("d:\\fdfs_client.conf");
	File file2 =  resource.getFile();
	String configFile = file2.getAbsolutePath();
	ClientGlobal.init(configFile);
	
	ExecutorService fixedThreadPool = Executors.newFixedThreadPool(100);
	for (int i = 0; i < 50; i++) {
		fixedThreadPool.execute(new FastdfsThread(fb));
	}
}

}`

线程如下:

` @OverRide
public void run() {
try {
TrackerServer trackerServer = null;
StorageServer storageServer = null;

		for (int i = 0; i < 100000; i++) {
			
			TrackerClient tracker = new TrackerClient();
			trackerServer = tracker.getConnection();
			
			StorageClient storageClient = new StorageClient(trackerServer, storageServer);
			NameValuePair nvp[] = new NameValuePair[] {
					 new NameValuePair("age", i+""), 
	                 new NameValuePair("sex", Thread.currentThread().getName()+i) 
					};
			
			byte[] filebtyes = fileToBytes(this.bean.getFile());//bean为file的实体类
			String fileIds[] = storageClient.upload_file(filebtyes, this.bean.getFileExt(), nvp);
			System.out.println("文件数:"+i+"  组名:" + fileIds[0]+"  路径: " + fileIds[1]);
			
			trackerServer.close();
		}
	} catch (IOException e) {
		e.printStackTrace();
	} catch (MyException e) {
		e.printStackTrace();
	}
	
}`

根据上述的代码执行,无法通过压力测试....也尝试过将TrackerClient tracker = new TrackerClient(); trackerServer = tracker.getConnection(); 放到for循环外,结果一样;期待解答,谢谢

from fastdfs-client-java.

Jianper avatar Jianper commented on September 28, 2024

我的一个猜测,没有验证,仅供参考哈。
你这总共有50个线程,每个线程会创建10w个storageClient,每个storageClient都会去建立一个socket上传完成之后又关闭,所以,这段代码理论上会执行500w次socket的连接和关闭操作,是不是超出了服务器的承受能力了呢?

from fastdfs-client-java.

liwei-link avatar liwei-link commented on September 28, 2024

楼上说的没错,短时间内500w个连接,会占用500w个socket端口,而linux上socket最多只有65535个,超过后就开始从头开始分配,而释放tcp端口是需要几十秒的时间的,所以会出现Address already in use。你没必要在线程的for循环里创建storageClient,只要在线程开始的时候创建一个storageClient就可以,然后这个线程共享这个storageClient。

from fastdfs-client-java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.