默认eclipse的项目结构是扁平结构的。
由于eclipse的项目管理不能像IDEAJ的那样,可以在不同的窗口中打开不同的项目,故在多个项目下扁平展示结构,容易导致一些混乱,故建议修改它。
多个项目下,默认的项目结构展示方式及修改后结果
指定JDK的编译版本
eclipse-4.6.2版本,默认JDK的编译版本是1.8,故在编译目标平台为JDK1.7.x的时候,需要修改其编译版本,否则在导入工程时,会导致导入后的工程默认JAVA编译版本为1.8。
此外,仍然要添加1.7.x的已经安装版本。如下图
至此,仍有一些特殊,如使用Gradle,也需要同步修改导入的JDK版本,才能正常编译与调试,否则会出错,如下图。
按道理应该是可以在build.gradle中指定编译版本,但发现网上说的方法,均在导入项目过程中报脚本属性错误,估计网上的方法是适用于Android版本。
形如: sourceCompatibility = "1.6" targetCompatibility = "1.6"
@ResponseBody和ResponseEntity
@RequestMapping(value="/response", method=RequestMethod.GET)
public class ResponseController {
//http://127.0.0.1:8010/response/annotation
@RequestMapping("/annotation")
public @ResponseBody String responseBody() {
return "The String ResponseBody";
}
@RequestMapping("/charset/accept")
public @ResponseBody String responseAcceptHeaderCharset() {
return "\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01 (\"Hello world!\" in Japanese)";
}
//http://127.0.0.1:8010/response/charset/produce
@RequestMapping(value="/charset/produce", produces="text/plain;charset=UTF-8")
public @ResponseBody String responseProducesConditionCharset() {
return "\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01 (\"Hello world!\" in Japanese)";
}
//http://127.0.0.1:8010/response/entity/status
@RequestMapping("/entity/status")
public ResponseEntity responseEntityStatusCode() {
return new ResponseEntity("The String ResponseBody with custom status code (403 Forbidden)",
HttpStatus.FORBIDDEN);
}
//http://127.0.0.1:8010/response/entity/headers
@RequestMapping("/entity/headers")
public ResponseEntity responseEntityCustomHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
return new ResponseEntity("The String ResponseBody with custom header Content-Type=text/plain",
headers, HttpStatus.OK);
}
}
eclipse搭建gradle与springMVC的框架及Tomcat调试
经实际验证,发现eclipse-4.5.2版本,如果JDK只是1.7.x的版本,在安装STS离线包时,会跳过Spring的相关安装,故需要检查JDK是否为1.8.x版本。
——————————–
下载eclipse,不建议下载最新版本,因为很多外围辅助插件没有及时升级至最新版本的。如SpringMVC为例。
1.打下如下链接,下载STS的最新离线包版本,(可以通过eclipse来在线安装,但下载比较慢也容易失败。)
http://spring.io/tools/sts/all
2.下载相应的eclipse-for-jee,如果官网没有显示与之匹配的版本下载,则你需要尝试链接测试。
http://eclipse.stu.edu.tw/technology/epp/downloads/release/neon/2/eclipse-jee-neon-2-win32-x86_64.zip
3.或者你可以尝试以下地址,也就是从上述的下载地址猜测出的所有版本下载地址(也有可能会失效,但你可以仍然通过某一完整下载地址尝试测试)。
http://eclipse.stu.edu.tw/technology/epp/downloads/release/
4.打开刚下载的eclipe-4.6.2,help->install new software,打开如下安装对话框,选择你的离线包。
5.安装完成后,会提示重启,重启后,将可看到Gradle和Spring的相关项目如下。
File->New->Project
6.安装Tomcat,路径:Window->Preference。
7.测试Tomcat
8.添加并选择默认的项目JDK。
强制重置mysql密码
https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
jdk环境变量配置
备忘:
export JAVA_HOME=/jdk1.8.0_211
export JRE_HOME=/jdk1.8.0_211/jre
export CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
安装Elasticsearch&Kibana&X-Pack
1.下载文件:
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.2.0.tar.gz
https://artifacts.elastic.co/downloads/kibana/kibana-5.2.0-linux-x86_64.tar.gz
https://artifacts.elastic.co/downloads/logstash/logstash-5.2.0.tar.gz
https://artifacts.elastic.co/downloads/packs/x-pack/x-pack-5.2.0.zip
https://artifacts.elastic.co/downloads/beats/heartbeat/heartbeat-5.2.0-linux-x86_64.tar.gz
https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.2.0-linux-x86_64.tar.gz
https://artifacts.elastic.co/downloads/beats/packetbeat/packetbeat-5.2.0-linux-x86_64.tar.gz
2.分别解压elasticsearch\kibana\logstash后,各自执行以下命令安装xpack。
bin/elasticsearch-plugin install file:///path/to/file/x-pack-5.2.0.zip
bin/kibana-plugin install file:///path/to/file/x-pack-5.2.0.zip
bin/logstash-plugin install file:///path/to/file/x-pack-5.2.0.zip
卸载命令
bin/elasticsearch-plugin remove x-pack
bin/kibana-plugin remove x-pack
bin/logstash-plugin remove x-pack
3.启动相应应用
bin/elasticsearch
bin/kibana
bin/logstash
4.登录相关后台
----------------------------
kibana的后台:
http://localhost:5601
帐号与密码
Username: elastic Password: changeme
------------------------------
elasticsearch的restfullAPI
http://localhost:9200
-----------------------
logstash的后台
参考文档:
https://www.elastic.co/start
https://www.elastic.co/guide/en/x-pack/current/xpack-introduction.html
https://www.elastic.co/guide/en/x-pack/current/installing-xpack.html
solr快速入门
1.下载合适的solr版本。当前官网最新版本是6.4.1,但经验证,6.4.1版本在其管理后台中操作dataimport时,会显示空白页。故本人不建议使用最新版本进行学习和应用开发。经验证,其5.5.3版本的各项功能是可以正常工作的。
下载5.5.3版本,快捷路径是:https://mirrors.tuna.tsinghua.edu.cn/apache/lucene/solr
http://lucene.apache.org/solr/mirrors-solr-latest-redir.html
选择任意一个镜像,在进入镜像后,选择parent目录。
选择5.5.3的版本:https://mirrors.tuna.tsinghua.edu.cn/apache/lucene/solr/5.5.3/
https://mirrors.tuna.tsinghua.edu.cn/apache/lucene/solr/5.5.3/solr-5.5.3.zip
2.如下是其目录结构,初学者,应该习惯阅读Readme.txt文件,该文件记录了一些基本使用操作,很方便学习。
3.内置了几个例子,需要使用特殊命令开启,初学者应该每个例子都体验一下。
bin/solr -e where is one of:
cloud : SolrCloud example
dih : Data Import Handler (rdbms, mail, rss, tika)
schemaless : Schema-less example (schema is inferred from data during indexing)
techproducts : Kitchen sink example providing comprehensive examples of Solr features
4.体验dih例子。
bin/solr -e dih
5.打开管理后台页面:
在实际测试过程中,发现在window中,dataimport等一些相关操作,会失败。只有linux的才会成功,具体原因没有去分析。
http://mysql.mvware.com:8983/solr/
6.logging界面,当执行dataimport或其它操作,如果有错误或执行失败,可以检查该日志信息。
7.在CoreSelector中选择solr项,并选择dataimport项.
8.在dataimport项中,调试你的配置文件,经过该步骤,已经可以在query项和schemabrowser项中查询到相关记录了。
9.在dataimport项中,执行全量更新和增量更新,dataimport项是需要在solrconfig.xml中配置的。
solrconfig.xml中的requestHandler配置
solr-data-config.xml
solr-data-config.xml中的配置
10.浏览schemabrowser中的各个schema项,在solr6.x版本中,增加了schema的增删项,更方便从零搭建core项。
11.通过documents项,增加数据记录,通过schemabrowser或manage-schema.xml配置文件中可知道当前的schema有如下:。
在DocumentType选择JSON项,然后输入内容如下,并点击submit按钮提交:
{"id":12345, "author":"author_121","text":"text_121", "title":"title_1121"}
{"id":22345, "url":"url_121"}
如果执行成功,则提示如下:
Status: success
Response:
{
"responseHeader": {
"status": 0,
"QTime": 2
}
}
如果出错呢?也会有相应的错误提示,可依据提示进行修改输入项内容。
12.通过query项,进行查找刚才的输入项。
13.也可以通schemabrowser中的记录,快速跳转搜索的内容。
Dubbo的客户端负载均衡关键过程
String applicationConfig = "consumer-applicationContext.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(applicationConfig);
UserInfoService userInfoService = (UserInfoService) context.getBean("userInfoService");
//负载均衡的处理是一个典型的懒加载模式,只有在第一次调用接口时,才进行负载均衡的处理。
System.out.println(userInfoService.sayHello("zhangsan"));
1.在进入sayHello的调用时,堆栈信息和帧代码如下
其中Constants.LOADBALANCE_KEY的值是“loadbalance”,而Constants.DEFAULT_LOADBALANCE的值是“random”,每个接口(reference)均可以灵活配置一个均衡方式,默认不配置的情况下都是random的。
不配置loadbalance时,是random的模式。
2.假如当前的loadbalance设置了leastactive时,它是如何把名字和类LeastActiveLoadBalance的实例关联起来呢?
3.首先它在com.alibaba.dubbo.rpc.cluster.loadbalance目录下实现了4种均衡方式,如下:
ConsistentHashLoadBalance:一致哈希
LeastActiveLoadBalance:最久不使用
RandomLoadBalance:随机
RoundRobinLoadBalance:顺序循环
注:上1点中的loadbalance返回的则是上述的其中一个均衡策略类实例。
4.其它dubbo实现了一套类似spi的服务加载机制如下:
private void loadFile(Map> extensionClasses, String dir) {
String fileName = dir + type.getName();
try {
Enumeration urls;
ClassLoader classLoader = findClassLoader();
if (classLoader != null) {
urls = classLoader.getResources(fileName);
} else {
urls = ClassLoader.getSystemResources(fileName);
}
if (urls != null) {
while (urls.hasMoreElements()) {
java.net.URL url = urls.nextElement();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
try {
String line = null;
while ((line = reader.readLine()) != null) {
final int ci = line.indexOf('#');
if (ci >= 0) line = line.substring(0, ci);
line = line.trim();
if (line.length() > 0) {
try {
String name = null;
int i = line.indexOf('=');
if (i > 0) {
name = line.substring(0, i).trim();
line = line.substring(i + 1).trim();
}
if (line.length() > 0) {
Class> clazz = Class.forName(line, true, classLoader);
if (! type.isAssignableFrom(clazz)) {
throw new IllegalStateException("Error when load extension class(interface: " +
type + ", class line: " + clazz.getName() + "), class "
+ clazz.getName() + "is not subtype of interface.");
}
if (clazz.isAnnotationPresent(Adaptive.class)) {
if(cachedAdaptiveClass == null) {
cachedAdaptiveClass = clazz;
} else if (! cachedAdaptiveClass.equals(clazz)) {
throw new IllegalStateException("More than 1 adaptive class found: "
+ cachedAdaptiveClass.getClass().getName()
+ ", " + clazz.getClass().getName());
}
} else {
try {
clazz.getConstructor(type);
Set> wrappers = cachedWrapperClasses;
if (wrappers == null) {
cachedWrapperClasses = new ConcurrentHashSet>();
wrappers = cachedWrapperClasses;
}
wrappers.add(clazz);
} catch (NoSuchMethodException e) {
clazz.getConstructor();
if (name == null || name.length() == 0) {
name = findAnnotationName(clazz);
if (name == null || name.length() == 0) {
if (clazz.getSimpleName().length() > type.getSimpleName().length()
&& clazz.getSimpleName().endsWith(type.getSimpleName())) {
name = clazz.getSimpleName().substring(0, clazz.getSimpleName().length() - type.getSimpleName().length()).toLowerCase();
} else {
throw new IllegalStateException("No such extension name for the class " + clazz.getName() + " in the config " + url);
}
}
}
String[] names = NAME_SEPARATOR.split(name);
if (names != null && names.length > 0) {
Activate activate = clazz.getAnnotation(Activate.class);
if (activate != null) {
cachedActivates.put(names[0], activate);
}
for (String n : names) {
if (! cachedNames.containsKey(clazz)) {
cachedNames.put(clazz, n);
}
Class> c = extensionClasses.get(n);
if (c == null) {
extensionClasses.put(n, clazz);
} else if (c != clazz) {
throw new IllegalStateException("Duplicate extension " + type.getName() + " name " + n + " on " + c.getName() + " and " + clazz.getName());
}
}
}
}
}
}
} catch (Throwable t) {
IllegalStateException e = new IllegalStateException("Failed to load extension class(interface: " + type + ", class line: " + line + ") in " + url + ", cause: " + t.getMessage(), t);
exceptions.put(line, e);
}
}
} // end of while read lines
} finally {
reader.close();
}
} catch (Throwable t) {
logger.error("Exception when load extension class(interface: " +
type + ", class file: " + url + ") in " + url, t);
}
} // end of while urls
}
} catch (Throwable t) {
logger.error("Exception when load extension class(interface: " +
type + ", description file: " + fileName + ").", t);
}
}
5.每个均衡策略类均实现了select接口,通过该接口从多个可用的后端服务中返回其中一个服务。
Invoker select(List> invokers, URL url, Invocation invocation) throws RpcException;
Dubbo客户端的RPC构造关键过程
1.Dubbo的客户端请求配置和调用代码如下:
String applicationConfig = "consumer-applicationContext.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(applicationConfig);
注:在上一文章,曾介绍xml表的命名空间间解析过程,而dubbo:service是服务端配置,而dubbo:reference是客户端配置。
2.构造beanmap列表时,每一行dubbo::refence对应一个ReferenceBean配置。Reference会创建三个对象,如下:
FailoverClusterInvoker:失败转移使用,如服务器宕机等,是从com.alibaba.dubbo.common.Node接口派生的。
MockClusterInvoker:如其名,接口集群化,同一个接口,存在多个服务器为其提供服务支持时,需要一个均衡策略地访问这些服务器。
InvokerInvocationHandler:这个接口是真正的动态代理,它是从InvocationHandler中派生出来的,Failover和MockCluster都是从Node接口派生,用于管理集群的。
3.如下调用代码
UserInfoService userInfoService = (UserInfoService) context.getBean("userInfoService");
System.out.println(userInfoService.sayHello("zhangsan"));
4.如下代码,通过元数据RpcInvocation打包函数名和函数参数,然后通过RPC通信把请求发送到服务器端。
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String methodName = method.getName();
Class>[] parameterTypes = method.getParameterTypes();
if (method.getDeclaringClass() == Object.class) {
return method.invoke(invoker, args);
}
if ("toString".equals(methodName) && parameterTypes.length == 0) {
return invoker.toString();
}
if ("hashCode".equals(methodName) && parameterTypes.length == 0) {
return invoker.hashCode();
}
if ("equals".equals(methodName) && parameterTypes.length == 1) {
return invoker.equals(args[0]);
}
return invoker.invoke(new RpcInvocation(method, args)).recreate();
}