目录:


Springboot结合hbase

首先在pom中添加依赖

        <dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api-2.5</artifactId>
</exclusion>
<exclusion>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api-2.5-6.1.14</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.6.0</version>
<exclusions>
<exclusion>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
</exclusions>
</dependency>

代码:

    @Autowired
public SearchDaoImpl(AppSettings appSettings){
this.appSettings = appSettings; Configuration HBASE_CONFIG = new Configuration();
//与hbase/conf/hbase-site.xml中hbase.zookeeper.quorum配置的值相同
HBASE_CONFIG.set("hbase.zookeeper.quorum", appSettings.getHbasename());
//与hbase/conf/hbase-site.xml中hbase.zookeeper.property.clientPort配置的值相同
HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", appSettings.getHbaseport());
conf = HBaseConfiguration.create(HBASE_CONFIG);
}

Springboot结合elasticsearch

1.maven设置

springboot的版本是1.5.1.RELEASE。这个版本springboot使用的ES版本是2.4.4,其依赖的guava是16.0.1

这里我们需要手动指定guava为18.0版本,否则会出现异常 java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor()Ljava/util/concu‌rrent/Executor

...

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency> ....

2.修改配置,在application.properties中添加以下内容

###########  es setting  ###########
spring.data.elasticsearch.cluster-name=yq-cluster
spring.data.elasticsearch.cluster-nodes=node3.test.cn:9300,node4.test.cn:9300,node5.test.cn:9300
spring.data.elasticsearch.local=false
spring.data.elasticsearch.repositories.enabled=true

3.创建实体类(与ES中存储的内容格式对应)

package com.product;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document; @Document(indexName = "yuqing", type = "emp", shards = 3, replicas = 1, refreshInterval = "-1")
public class MyClient {
@Id
private String id; private String first_name; private String last_name; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getFirst_name() {
return first_name;
} public void setFirst_name(String first_name) {
this.first_name = first_name;
} public String getLast_name() {
return last_name;
} public void setLast_name(String last_name) {
this.last_name = last_name;
} }

4.建立资源库

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface ClientRepository extends ElasticsearchRepository<MyClient, String> {

}

5.服务接口

public interface EsService {
MyClient findClient(String id);
}

6.服务实现

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class EsServiceImpl implements EsService { @Autowired
private ClientRepository clientDao; private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(EsServiceImpl.class); public MyClient findClient(String id) {
MyClient client = clientDao.findOne(id);
LOG.info(" get cliente by id {} is {}", id, client);
return client;
}
}

7.应用

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping(value = "/es/")
public class EsController {
@Autowired
private EsService esl; @RequestMapping(value = "test", method = RequestMethod.POST)
public Object test(@RequestBody String id) {
return esl.findClient(id);
}
}

参考:Spring boot + elasticsearch的最简单实践

在实际使用中要注意,安装的es版本与springboot中引用的es版本不能有差异不能太大。

例如安装的是2.4.2的,那么api使用的2.4.x基本没问题,但如果引用5.x以上那肯定就不行了,会提示 None of the configured nodes are available

所以使用时要先看一下当前 spring-data-elasticsearch 所使用的es版本

Springboot结合RestTemplate处理Http请求

先定义config bean

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate; @Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
return new RestTemplate(factory);
} @Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(5000);// ms
factory.setConnectTimeout(15000);// ms
return factory;
}
}

应用

注:针对返回结果的序列化问题,目前没有找到方便快捷的方式,只好通过Object转HashMap的途径解决(虽然这种方法在属性多的情况下也比较复杂)

RestTemplate.getForObject虽然可以指定为业务类,但是转换始终有问题。

@RestController
@EnableAutoConfiguration
@Import(value = { RestTemplateConfig.class })
public class SpringRestTemplateApp { @Autowired
RestTemplate restTemplate; /*********** HTTP GET method *************/
@RequestMapping("")
public String hello() {
String url = "http://localhost:8081/user";
HashMap<String,Object> user = (HashMap<String,Object>)restTemplate.getForObject(url, Object.class);
return user.get("name").toString();
} @RequestMapping("/user")
public User genUser() {
User user = new User();
user.setAge(18);
user.setName("Ray");
return user;
}public class User {
private int age;
private String name; public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
} public static void main(String[] args) throws Exception {
SpringApplication.run(SpringRestTemplateApp.class, args);
}
}

如果想将返回结果转换为实体对象(比如前面代码中的User),需要以字符串接收结果,然后再转换为对象。

不过java的json工具(我用的jackson)貌似不是很给力,也或许是我使用不当,总感觉比C#的差多了,转换个json都好麻烦,尤其是复杂对象的时候(嵌套多层类,并且有List)

先定义实体类,这里要注意的是,如果有嵌套的类,必须要定义为静态类

public class Company {

    @JsonIgnoreProperties(ignoreUnknown=true)
public static class Employee { public Employee () {
} private String name; public String getName() {
return name;
} public void setName(String name) {
this.name= name;
}
} public Company () {
} private List<Employee> emps; public List<Employee> getEmployees() {
return emps;
} public void setEmployees(List<Employee> Employees) {
this.emps= Employees;
}
}

转换

public class TransferTest {
private static TransferTest instance; public static TransferTest instance() {
if (instance == null) {
instance = new TransferTest();
}
return instance;
} public TransferTest() {
     //只需要定义一次
objectMapper = new ObjectMapper() {
private com.fasterxml.jackson.databind.ObjectMapper jacksonObjectMapper = new com.fasterxml.jackson.databind.ObjectMapper(); public <T> T readValue(String value, Class<T> valueType) {
try {
return jacksonObjectMapper.readValue(value, valueType);
} catch (IOException e) {
throw new RuntimeException(e);
}
} public String writeValue(Object value) {
try {
return jacksonObjectMapper.writeValueAsString(value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
};
} private Company doTransfer(String origin, String from, String to) throws UnirestException {
String resp = restTemplate.getForObject(<url>,<data>,String.Class); //使用某种http工具类以字符串获取返回结果
Company data = objectMapper.readValue(str, Company.class);
return data ;
}
}

真的感觉好麻烦/(ㄒoㄒ)/~~

Springboot的maven相关

在将springboot项目发布的时候我们都是用maven打包成jar包,然后使用  java -jar xxx.jar  直接启动

但是有时候发现执行之后会提示找不到启动程序。

解决方法:在pom文件中添加以下内容

    <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.2.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Springboot的banner设定

Springboot的banner很容易设置,只需要将banner.txt文件加到 src/main/resources 目录下即可

这里给出一个码农保护神

${AnsiColor.BRIGHT_YELLOW}
////////////////////////////////////////////////////////////////////
// _ooOoo_ //
// o8888888o //
// 88" . "88 //
// (| ^_^ |) //
// O\ = /O //
// ____/`---'\____ //
// .' \\| |// `. //
// / \\||| : |||// \ //
// / _||||| -:- |||||- \ //
// | | \\\ - /// | | //
// | \_| ''\---/'' | | //
// \ .-\__ `-` ___/-. / //
// ___`. .' /--.--\ `. . ___ //
// ."" '< `.___\_<|>_/___.' >'"". //
// | | : `- \`.;`\ _ /`;.`/ - ` : | | //
// \ \ `-. \_ __\ /__ _/ .-` / / //
// ========`-.____`-.___\_____/___.-`____.-'======== //
// `=---=' //
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
// 佛祖保佑 永不宕机 永无BUG //
//////////////////////////////////////////////////////////////////// ${AnsiColor.BRIGHT_RED}
Application Version: ${application.version}${application.formatted-version}
Spring Boot Version: ${spring-boot.version}${spring-boot.formatted-version}

参考:https://blog.csdn.net/baochanghong/article/details/54286422

Spring boot学习整理的更多相关文章

  1. 整理了八个开源的 Spring Boot 学习资源

    Spring Boot 算是目前 Java 领域最火的技术栈了,松哥年初出版的 <Spring Boot + Vue 全栈开发实战>迄今为止已经加印了 3 次,Spring Boot 的受 ...

  2. 15 个优秀开源的 Spring Boot 学习项目,一网打尽!

    Spring Boot 算是目前 Java 领域最火的技术栈了,松哥年初出版的 <Spring Boot + Vue 全栈开发实战>迄今为止已经加印了 8 次,Spring Boot 的受 ...

  3. 15 个优秀开源的 Spring Boot 学习项目

    Spring Boot 算是目前 Java 领域最火的技术栈了,松哥年初出版的 <Spring Boot + Vue 全栈开发实战>迄今为止已经加印了 8 次,Spring Boot 的受 ...

  4. Spring Boot 学习笔记(六) 整合 RESTful 参数传递

    Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...

  5. Spring Boot 学习1-创建Spring Boot应用

    如果使用Maven, 确保先安装好Maven再继续. 创建POM文件 在这里有两种方式: 继承Spring Boot parent的pom. 不继承. 继承Spring Boot pom 1 2 3 ...

  6. Github点赞超多的Spring Boot学习教程+实战项目推荐!

    Github点赞接近 100k 的Spring Boot学习教程+实战项目推荐!   很明显的一个现象,除了一些老项目,现在 Java 后端项目基本都是基于 Spring Boot 进行开发,毕竟它这 ...

  7. Spring Boot学习大全(入门)

    Spring Boot学习(入门) 1.了解Spring boot Spring boot的官网(https://spring.io),我们需要的一些jar包,配置文件都可以在下载.添置书签后,我自己 ...

  8. Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客

    ==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...

  9. Spring boot学习1 构建微服务:Spring boot 入门篇

    Spring boot学习1 构建微服务:Spring boot 入门篇 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...

随机推荐

  1. Android———最详细的系统对话框(AlertDialog)详解

    在实际应用开发中,用到系统对话框中的情况几乎是没有的.按开发流程来说,UI工程师都会给出每一个弹窗的样式,故而在实际开发中都是自定义弹窗的. 即使用到的地方不多,但是我们也是需要了解并且能熟练的运用它 ...

  2. IIS无法连接LocalDb,怎么办?

    最近安装了vs 2013,电脑配置不太好,所以没有安装数据库,直接使用vs2013自带的localdb工具,直接运行访问本地mdf数据库文件.但是部署到IIS就出问题了.问题就像下面的图片一样. 最后 ...

  3. 全局enter回车键js

    js实现敲回车键触发事件 document.onkeydown = function(e){ var ev = document.all ? window.event : e; ){ alert(&q ...

  4. MySQL中的索引提示Index Hint

    MySQL数据库支持索引提示(INDEX HINT)显式的高速优化器使用了哪个索引.以下是可能需要用到INDEX HINT的情况 a)MySQL数据库的优化器错误的选择了某个索引,导致SQL运行很慢. ...

  5. 设计一个算法,採用BFS方式输出图G中从顶点u到v的最短路径(不带权的无向连通图G採用邻接表存储)

    思想:图G是不带权的无向连通图.一条边的长度计为1,因此,求带顶点u和顶点v的最短的路径即求顶点u和顶点v的边数最少的顶点序列.利用广度优先遍历算法,从u出发进行广度遍历,类似于从顶点u出发一层一层地 ...

  6. linux增加 路由使两个不同的网段可以访问

    举例:在交换机上有2个vlan 地址分别是192.168.10.1/24 192.168.20.1/24 2台server:一台A:server地址是192.168.10.3/24,一台B:serve ...

  7. 修改Linux的基本配置(修改主机名修改ip地址安装JDK/Tomcat/MySQL等等)

    (一)基本操作修改 修改主机名 vi /etc/sysconfig/network NETWORKING=yes HOSTNAME=server1.itcast.cn 修改ip地址 vi /etc/s ...

  8. 如何用云存储和CDN加速网站图片视频、阿里云OSS的使用(转)

    总有人说阿里云主机带宽小,那只是因为你还停留在单机架构上. 阿里的架构设计,云主机主要用来跑程序的,附件的存储和访问主要靠OSS. 有人又会说了,OSS按存储费+流量双重计费伤不起,只是你不知道OSS ...

  9. 20170421 F110 常见问题

    F110常見問題以及處理方式 1. Vendor中沒有與F110中相同的Payment method 解決辦法: 在Vendor主檔中維護Payment method 2. 結報被Block 解決辦法 ...

  10. kafka connect简介以及部署

    https://blog.csdn.net/u011687037/article/details/57411790 1.什么是kafka connect? 根据官方介绍,Kafka Connect是一 ...