com.codingapi.tx.springcloud.service.impl.ModelNameServiceImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of transaction-springcloud Show documentation
Show all versions of transaction-springcloud Show documentation
transaction-springcloud project for Spring Boot
package com.codingapi.tx.springcloud.service.impl;
import com.lorne.core.framework.utils.encode.MD5Util;
import com.codingapi.tx.listener.service.ModelNameService;
import com.codingapi.tx.springcloud.listener.ServerListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* Created by lorne on 2017/7/12.
*/
@Service
@Configuration
public class ModelNameServiceImpl implements ModelNameService {
@Value("${spring.application.name}")
private String modelName;
@Autowired
private ServerListener serverListener;
private String host = null;
@Override
public String getModelName() {
return modelName;
}
private String getIp(){
if (host == null) {
try {
host = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
return host;
}
@Override
public String getUniqueKey() {
String address = getIp()+serverListener.getPort();
return MD5Util.md5(address.getBytes());
}
@Override
public String getIpAddress() {
String address = getIp() + ":" + serverListener.getPort();
return address;
}
}