赣州新闻网

首页 / 科技

HBase 初探:架构 + 原理 + 对比 + 实践

2020-01-20 · 赣州新闻网 · 科技

背景

在互联网发达的今天,容器化和微服务化是一种潮流,已经不是趋势了,而是潮流。不管是出去面试还是自己日常项目开发,容器化可能还没普及,但是微服务化是不能缺少的。在微服务如此盛行的天下,Spring Clound 已经很流行了,作为 SpringCloud 的基石 SpringBoot 自然也是不容忽视。关于 SpringBoot 我们 Java 极客技术团队专门为知识星球的用户制作了一套视频教程,视频已经发布了几章了,还在持续更新中,欢迎大家到知识星球中学习,进入知识星球后请记得发帖要机器码哦。这篇文章我们先了解一下SpringBoot Starter,SpringBoot 的 Starter 我们可以说是天天都在用,但是到底什么是 Starter,如何自己编写一个 Starter 呢?这篇文章我们来一探究竟。

SpringBoot-Starter

什么是 Starter

我们先看下官方是咱们定义 Starter 的,如下

Starters are a set of convenient dependency descriptors that you can include in your application.

意思是说 Starts 是一组可以让你很方便在应用增加的依赖关系描述符的集合。或者可以这样理解,平时我们开发的时候很多情况下都会有一个模块依赖另一个模块,这个时候我们一般都是采用 maven 的模块依赖,进行模块的依赖,但是这种情况下我们完全可以采用 Starter 的方式,将需要被依赖的模块用 Starter 的方式去开发,最后直接引入一个 Starter 也可以达到这样的效果。

命名规则

讲起来可以比较抽象,我们这里以一个例子来试一下效果。在实战之前我们需要先了解的 Starter 的命名规则,在官方文档上很清楚的写了如下内容:

Do not start your module names with spring-boot . Let’s assume that you are creating a starter for “acme”, name the auto-configure module acme-spring-boot-autoconfigure and the starter acme-spring-boot-starter. If you only have one module combining the two, use acme-spring-boot-starter.

由于 SpringBoot 官方本身就会提供很多 Starter,为了区别哪些 Starter 是官方,哪些是私人的或者第三方的,所以 SpringBoot 官方提出,第三方在建立自己的 Starter 的时候命名规则统一用xxx-spring-boot-starter,而官方提供的 Starter 统一命名方式为spring-boot-starter-xxx。

使用

假设我们现在有一个需求是将对象转换成 JSON,并在字符串前面加上一个名称,前轴我们支持通过配置文件配置的方式。(当然这个需求只是一个假设,我们用来测试功能用,实际的开发当然不会只是这么简单)。

  1. 我们先创建一个 Starter,名字叫myjson-spring-boot-starter,并加入自动装配和 fastjson 的依赖,如下
SpringBoot 精髓之 SpringBoot-starter


  1. 然后我们创建一个 Service,并增加一个public String objToJson(Object object) 方法,直接调用fastjson 的方法

import com.alibaba.fastjson.JSON;

/**
* <br>
* <b>Function:</b><br>
* <b>Author:</b>@author ziyou<br>
* <b>Date:</b>2019-07-20 18:16<br>
* <b>Desc:</b>无<br>
*/
public class MyJsonService {

private String name;

/**
* 使用 fastjson 将对象转换为 json 字符串输出
*
* @param object 传入的对象
* @return 输出的字符串
*/
public String objToJson(Object object) {
return getName() + JSON.toJSONString(object);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

  1. 编写配置类

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* <br>
* <b>Function:</b><br>
* <b>Author:</b>@author ziyou<br>
* <b>Date:</b>2019-07-20 18:36<br>
* <b>Desc:</b>无<br>
*/
@ConfigurationProperties(prefix = \"ziyou.json\")
public class MyJsonProperties {

public static final String DEFAULT_NAME = \"ziyou\";

private String name = DEFAULT_NAME;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}


  1. 创建自动化配置类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* <br>
* <b>Function:</b><br>
* <b>Author:</b>@author ziyou<br>
* <b>Date:</b>2019-07-20 18:39<br>
* <b>Desc:</b>无<br>
*/
@Configuration
@ConditionalOnClass({MyJsonService.class})
@EnableConfigurationProperties(MyJsonProperties.class)
public class MyJsonAutoConfiguration {

/**
* 注入属性类
*/
@Autowired
private MyJsonProperties myJsonProperties;

/**
* 当当前上下文中没有 MyJsonService 类时创建类
*
* @return 返回创建的实例
*/
@Bean
@ConditionalOnMissingBean(MyJsonService.class)
public MyJsonService myJsonService() {
MyJsonService myJsonService = new MyJsonService();
myJsonService.setName(myJsonProperties.getName());
return myJsonService;
}
}


  1. 然后我们再创建 resource/META-INF/spring.factories 文件,增加如下内容,将自动装配的类配置上

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.ziyou.starter.MyJsonAutoConfiguration

  1. 然后我们通过运行mvn install命令,将这个项目打包成 jar 部署到本地仓库中,提供让另一个服务调用。
  2. 创建一个新的 SpringBoot web 项目test-myjson-spring-boot-starter,提供一个接口去访问。


package com.ziyou.test.controller;

import com.ziyou.starter.MyJsonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* <br>
* <b>Function:</b><br>
* <b>Author:</b>@author ziyou<br>
* <b>Date:</b>2019-07-20 19:04<br>
* <b>Desc:</b>无<br>
*/
@RestController
public class MyJsonController {

@Autowired
private MyJsonService myJsonService;

@RequestMapping(value = \"tojson\")
public String getStr() {
User user = new User();
user.setName(\"dsfsf\");
user.setAge(18);
return myJsonService.objToJson(user);
}
}


  1. application.properties 中配置。

server.port=8089
ziyou.json.name=java-geek-teck

  1. pom.xml 中配置刚刚编写的 Starter。
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<project xmlns=\"http://maven.apache.org/POM/4.0.0\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.ziyou</groupId>
<artifactId>test-myjson-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>com.ziyou</groupId>
<artifactId>myjson-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>

</project>
  1. 启动测试项目,打开浏览器访问接口可以看到如下效果。
SpringBoot 精髓之 SpringBoot-starter

  1. 分析:从结果中我们可以看到在 Starter 中定义的MyJsonService已经被成功的调用和执行。

测试项目视频需求文章接口实践架构龙南