[TOC]html
用惯了spring全家桶以后,试试dropwizard的Hello World也别有一帆风味。为了加强对外访问API的能力,须要引入open feign。这里简单在dropwizard中使用feign。java
Dropwizard is a Java framework for developing ops-friendly, high-performance, RESTful web services. react
Dropwizard
使成熟、稳定的java生态系统更加简单、轻量(light-weight), 让你更专一于业务逻辑。git
Dropwizard 为配置(configuration)、统计(application metrics)、日志(logging)、operational tools提供了开箱即用的能力。让您和您的团队可以在最短的时间内开发出具备生产环境的质量的Web服务。github
下面的简介来自REST微服务架构之Dropwizardweb
DropWizard是由Yammer开发团队贡献的一个后台服务开发框架,其集成了Java生态系统中各个问题域中最优秀的组件,帮助开发者快速的打造一个Rest风格的后台服务。 spring
对开发者来讲,使用DropWizard有以下好处:
一、和Maven集成良好,也就是说和Gradle集成也很良好;
二、开发迅速,部署简单;
三、代码结构好,可读性高;
四、自动为服务提供OM框架;
五、让开发者天然的把一个应用拆分为一个个的小服务 数据库
DropWizard结构的Web服务组成
一、Configuration:用于设置该服务的配置,比方说在服务开放在哪一个端口,数据库配置是怎样的等等。
二、Application(即Service):该服务的主入口,定义该服务使用哪一个配置文件,开放哪些Resource,该服务须要哪些HealthCheck等等。
三、Resource:定义一个资源,包括如何获取该资源,对该资源作Get/Post/Delete/Query时,对应的各类业务逻辑。
四、Representation:定义了一个服务返回值对象,当服务返回该对象时,会自动的把该对象按属性值生成一个Json格式的字符串返回给服务调用者。
五、HealthCheck:在DropWizard为每一个服务提供的OM框架中用到,经过它能够随时检测当前服务是否可用。 apache
Web应用程序不能没有HTTP,因此Dropwizard使用Jetty HTTP库将一个使人难以置信的HTTP服务器直接嵌入到您的项目中。 Dropwizard项目不须要将应用程序交给一个复杂的应用程序服务器,而是一个main
方法,它会自动链接一个HTTP服务器。将应用程序做为一个简单的过程运行,消除了Java在生产中的一些很差的东西(没有PermGen问题,没有应用程序服务器配置和维护,没有复杂的部署工具,没有类加载器(class loader)故障,没有隐藏的应用程序日志,没有尝试调整一个垃圾收集器来处理多个应用程序工做负载),并容许您使用全部现有的Unix进程管理工具。json
吹完牛逼,开始干活。
照例,首先本次测试(github.com/Ryan-Miao/l…
.
├── dependency-reduced-pom.xml
├── l4dropwizard.iml
├── pom.xml
├── readme.md
└── src
└── main
├── java
│ └── com
│ └── test
│ ├── HelloWorldApplication.java
│ ├── configuration
│ │ ├── HelloWorldConfiguration.java
│ │ └── modules
│ │ ├── ConnectAndReadConfig.java
│ │ └── GithubApiConfig.java
│ └── domain
│ ├── connect
│ │ ├── GithubClient.java
│ │ └── GithubConnector.java
│ ├── entiry
│ │ ├── GithubUser.java
│ │ └── Saying.java
│ ├── health
│ │ └── TemplateHealthCheck.java
│ └── resource
│ ├── GithubResource.java
│ └── HelloWorldResource.java
└── resources
└── config
└── dev.yml
14 directories, 16 files复制代码
依旧是maven项目,pom中添加dropwizard
<properties>
<dropwizard.version>1.0.6</dropwizard.version>
<java.version>1.8</java.version>
<mainClass>com.test.HelloWorldApplication</mainClass>
</properties>
<dependencies>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>${dropwizard.version}</version>
</dependency>
</dependencies>复制代码
dropwizard采用yaml做为配置文件,同时须要有个配置类对应yaml中的属性。
建立config/dev.yml
template: Hello, %s!
defaultName: Stranger
server:
# softNofileLimit: 1000
# hardNofileLimit: 1000
applicationConnectors:
- type: http
port: 8080
#this requires the alpn-boot library on the JVM's boot classpath
#- type: h2
# port: 8445
# keyStorePath: example.keystore
# keyStorePassword: example
adminConnectors:
- type: http
port: 8082复制代码
而后,新建对应的配置类com.test.configuration.HelloWorldConfiguration
package com.test.configuration;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.dropwizard.Configuration;
import org.hibernate.validator.constraints.NotEmpty;
/** * Created by rmiao on 3/14/2017. */
public class HelloWorldConfiguration extends Configuration {
@NotEmpty
private String template;
@NotEmpty
private String defaultName = "Stranger";
@JsonProperty
public String getTemplate() {
return template;
}
@JsonProperty
public void setTemplate(String template) {
this.template = template;
}
@JsonProperty
public String getDefaultName() {
return defaultName;
}
@JsonProperty
public void setDefaultName(String name) {
this.defaultName = name;
}
}复制代码
下一步就是启动类:com.test.application.HelloWorldApplication
package com.test;
import com.test.domain.health.TemplateHealthCheck;
import com.test.domain.resource.HelloWorldResource;
import com.test.configuration.HelloWorldConfiguration;
import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import java.util.Map;
/** * Created by Ryan Miao on 3/14/2017. */
public class HelloWorldApplication extends Application<HelloWorldConfiguration> {
public static void main(String[] args) throws Exception {
new HelloWorldApplication().run(args);
}
@Override
public String getName() {
return "hello-world";
}
@Override
public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
// nothing to do yet
}
@Override
public void run(HelloWorldConfiguration configuration, Environment environment) throws Exception {
final HelloWorldResource resource = new HelloWorldResource(
configuration.getTemplate(),
configuration.getDefaultName()
);
final TemplateHealthCheck healthCheck =
new TemplateHealthCheck(configuration.getTemplate());
environment.healthChecks().register("template", healthCheck);
environment.jersey().register(resource);
environment.jersey().register(healthCheck);
}
}复制代码
到此,配置基本完成,只须要添加接口resource
就好。
对应于springmvc中conroller, dropwizard采用jersey,使用resourc做为接口类:com.test.com.test.resource.HelloWorldResource
package com.test.domain.resource;
import com.codahale.metrics.annotation.Timed;
import com.test.domain.entiry.Saying;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;
/** * Created by rmiao on 3/14/2017. */
@Path("/hello-world")
@Produces(MediaType.APPLICATION_JSON)
public class HelloWorldResource {
private final String template;
private final String defaultName;
private final AtomicLong counter;
public HelloWorldResource(String template, String defaultName) {
this.template = template;
this.defaultName = defaultName;
this.counter = new AtomicLong();
}
@GET
@Timed
public Saying sayHello(@QueryParam("name") Optional<String> name) {
final String value = String.format(template, name.orElse(defaultName));
return new Saying(counter.incrementAndGet(), value);
}
}复制代码
这里的template没啥意思,官网用在这里就是为了彰显下读取配置文件的能力: 经过configuration类来操做配置属性。
另外,须要注意的是,resource并不能像Spring同样自动扫描,须要手动去environment.jersey().register(resource);
。
启动前还须要配置fat jar,同Spring-boot同样,fat jar首选. 在pom配置:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${mainClass}</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>${mainClass}</mainClass>
<arguments>
<argument>server</argument>
<argument>target/classes/config/dev.yml</argument>
</arguments>
<systemProperties>
<systemProperty>
<key>application.name</key>
<value>HelloWorld</value>
</systemProperty>
<systemProperty>
<key>application.home</key>
<value>.</value>
</systemProperty>
<systemProperty>
<key>application.environment</key>
<value>dev</value>
</systemProperty>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>复制代码
接下来,打包:
mvn package复制代码
而后,run jar:
java -jar target\l4dropwizard-1.0-SNAPSHOT.jar server target/classes/config/dev.yml复制代码
浏览器访问http://localhost:8080/hello-world?name=Ryan
将获得:
{
"id": 1,
"content": "Hello, Ryan!"
}复制代码
至此,hello world完成。
Feign是一个网络请求客户端,简化了网络请求代码,使得咱们能够采用更加友好的方式发送请求,而且管理请求。Feign采用注解驱动模板,因此目前只支持text-based apis.
首先,添加依赖:
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
<version>${feign.version}</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-hystrix</artifactId>
<version>${feign.version}</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-slf4j</artifactId>
<version>${feign.version}</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-jackson</artifactId>
<version>${feign.version}</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-gson</artifactId>
<version>${feign.version}</version>
</dependency>
<dependency>
<groupId>io.reactivex</groupId>
<artifactId>rxjava</artifactId>
<version>1.2.1</version>
<scope>compile</scope>
</dependency>复制代码
Feign的配置主要有三个,一个是isolation.thread
线程存活时间。一个是connectTimeoutMillis
链接超时,一个是readTimeoutMillis
。
本次测试将采用github的公共API,获取用户信息。首先配置线程存活时间。在dev.yml中添加:
hystrixConfig:
hystrix.command.GithubConnector#getUserProfile(String).execution.isolation.thread.timeoutInMilliseconds: 7000复制代码
而后是两个超时配置:
githubApiConfig:
baseUrl: "https://api.github.com"
getUserProfile:
connectTimeoutMillis: 2000
readTimeoutMillis: 5000复制代码
Dropwizard经过配置类和配置文件绑定的方式获取配置内容。所以,须要对应的在配置类中建立对应的字段。
com.test.configuration.modules.ConnectAndReadConfig
package com.test.configuration.modules;
/** * Created by Ryan Miao on 9/14/17. */
public class ConnectAndReadConfig {
private int connectTimeoutMillis;
private int readTimeoutMillis;
public int getConnectTimeoutMillis() {
return connectTimeoutMillis;
}
public int getReadTimeoutMillis() {
return readTimeoutMillis;
}
}复制代码
com.test.configuration.modules.GithubApiConfig
package com.test.configuration.modules;
/** * Created by Ryan Miao on 9/14/17. */
public class GithubApiConfig {
private String baseUrl;
private ConnectAndReadConfig getUserProfile;
public String getBaseUrl() {
return baseUrl;
}
public ConnectAndReadConfig getGetUserProfile() {
return getUserProfile;
}
}复制代码
在com.test.configuration.HelloWorldConfiguration中添加:
@NotEmpty
private Map<String, Object> hystrixConfig;
@NotNull
private GithubApiConfig githubApiConfig;复制代码
而后在application中配置好hystrix的配置:
在HelloWorldApplication#run方法中
//init hystrix config
Map<String, Object> hystrixConfig = configuration.getHystrixConfig();
for (final Map.Entry<String, Object> config : hystrixConfig.entrySet()) {
ConfigurationManager.getConfigInstance().setProperty(config.getKey(), config.getValue());
}复制代码
建立接口com.test.domain.connect.GithubConnector:
package com.test.domain.connect;
import com.test.domain.entiry.GithubUser;
import feign.Headers;
import feign.Param;
import feign.RequestLine;
import rx.Observable;
/** * Created by ryan on 9/14/17. */
public interface GithubConnector {
/** * @param username * @return */
@RequestLine("GET /users/{username}")
@Headers({"Accept: application/vnd.github.v3+json"})
Observable<GithubUser> getUserProfile(@Param("username") String username);
}复制代码
建立客户端com.test.domain.connect.GithubClient
package com.test.domain.connect;
import com.test.configuration.modules.ConnectAndReadConfig;
import com.test.configuration.modules.GithubApiConfig;
import com.test.domain.entiry.GithubUser;
import feign.Request;
import feign.Response;
import feign.gson.GsonDecoder;
import feign.gson.GsonEncoder;
import feign.hystrix.HystrixFeign;
import feign.slf4j.Slf4jLogger;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.Observable;
import java.io.IOException;
import java.util.UUID;
/** * Created by Ryan Miao on 9/14/17. */
public class GithubClient {
public static final Logger LOGGER = LoggerFactory.getLogger(GithubClient.class);
private GithubApiConfig githubApiConfig;
public GithubClient(GithubApiConfig githubApiConfig) {
this.githubApiConfig = githubApiConfig;
}
public Observable<GithubUser> getUserProfile(String username) {
String baseUrl = githubApiConfig.getBaseUrl();
ConnectAndReadConfig getUserProfile = githubApiConfig.getGetUserProfile();
GithubConnector connector = HystrixFeign.builder()
.decoder(new GsonDecoder())
.encoder(new GsonEncoder())
.logger(new Slf4jLogger())
.options(new Request.Options(getUserProfile.getConnectTimeoutMillis(), getUserProfile.getReadTimeoutMillis()))
.errorDecoder((methodKey, response) -> {
StringBuilder msg = new StringBuilder("status=").append(response.status())
.append(";request_headers=").append(response.request().headers())
.append(";response_headers=").append(response.headers())
.append(";body=");
Response.Body body = response.body();
if (body != null && body.length() > 0) {
try {
msg.append(IOUtils.toString(body.asReader()));
} catch (IOException e) {
msg.append("can not read body,"+e.getMessage());
}
}
return new RuntimeException(msg.toString());
})
.requestInterceptor(template -> template.header("requestId", UUID.randomUUID().toString()))
.target(GithubConnector.class, baseUrl);
return connector.getUserProfile(username).onErrorReturn(error -> {
LOGGER.error("Get github user profile failed. ", error);
return null;
});
}
}复制代码
最后,建立一个接口来测试下:
com.test.domain.resource.GithubResource
package com.test.domain.resource;
import com.codahale.metrics.annotation.Timed;
import com.test.configuration.modules.GithubApiConfig;
import com.test.domain.connect.GithubClient;
import com.test.domain.entiry.GithubUser;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
/** * Created by Ryan Miao on 9/14/17. */
@Path("/github")
@Produces(MediaType.APPLICATION_JSON)
public class GithubResource {
private GithubApiConfig githubApiConfig;
public GithubResource(GithubApiConfig githubApiConfig) {
this.githubApiConfig = githubApiConfig;
}
@GET
@Timed
@Path("/users/{username}")
public GithubUser getUserProfile(@PathParam("username") final String username){
GithubClient client = new GithubClient(githubApiConfig);
return client.getUserProfile(username).toBlocking().first();
}
}复制代码
run main方法启动。访问localhost:8080/github/users/Ryan-Miao
就能够获得个人github信息了:
{
"login": "Ryan-Miao",
"id": 11866078,
"avatar_url": "https://avatars3.githubusercontent.com/u/11866078?v=4",
"url": "https://api.github.com/users/Ryan-Miao",
"name": "Ryan Miao",
"email": null,
"location": "中国深圳",
"blog": "https://ryan-miao.github.io/"
}复制代码
至此,feign的简单集成就搞定了。
feign采用hystrix的配置的时候,grop key是baseUrl.上栗中,grop Key为https://api.github.com
, commandKey为接口+方法和参数,上栗中为GithubConnector#getUserProfile(String)
。所以,配置线程超时用了commandKey如上。若是要配置coreSize之类的,必须使用url作为group key了。