我正在按照本教程https://dzone.com/articles/building-microservices-spring在Spring上使用thrift。当我运行教程时它运行正常,但是当我做一个例子时,它没有做正确的映射。
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class CacheApplication {
public static void main(String[] args) {
SpringApplication.run(CacheApplication.class, args);
}
@Bean
public TProtocolFactory tProtocolFactory() {
return new TBinaryProtocol.Factory();
}
@Bean
public Servlet cache(TProtocolFactory protocolFactory, CacheServiceHandler handler) {
return new TServlet(new TCacheService.Processor<>(handler), protocolFactory);
}
}
这是我的主要内容,当我启动服务器时,它说“当Servlet缓存映射到[/ cache /]”时,“Servlet缓存映射到[/]”
教程是gradle,但我正在使用maven
<?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>
<groupId>com.example</groupId>
<artifactId>demo_thrift</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Demo Thrift</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.11.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
运行服务器我没有错误,但是当我使用此代码运行客户端时
@EnableAutoConfiguration
@ComponentScan(basePackageClasses = {CacheApplication.class})
public class ClientTest {
protected int port;
@Autowired
protected TProtocolFactory protocolFactory;
public static void main(String[] args) throws Exception {
new SpringApplicationBuilder().sources(ClientTest.class).profiles("client").web(false).run(args);
}
@Bean
public String test() throws Exception{
TTransport transport= new THttpClient("http://localhost:8080/cache/");
TProtocol protocol = protocolFactory.getProtocol(transport);
TCacheService.Client client;
client = new TCacheService.Client(protocol);
return client.query(10);
}
}
这是我得到的错误:
Caused by: org.apache.thrift.transport.TTransportException: HTTP Response code: 406
at org.apache.thrift.transport.THttpClient.flush(THttpClient.java:352) ~[libthrift-0.11.0.jar:0.11.0]
at org.apache.thrift.TServiceClient.sendBase(TServiceClient.java:73) ~[libthrift-0.11.0.jar:0.11.0]
at org.apache.thrift.TServiceClient.sendBase(TServiceClient.java:62) ~[libthrift-0.11.0.jar:0.11.0]
at com.example.demo.Generated.TCacheService$Client.send_query(TCacheService.java:56) ~[classes/:na]
at com.example.demo.Generated.TCacheService$Client.query(TCacheService.java:48) ~[classes/:na]
at com.example.demo.Client.ClientTest.test(ClientTest.java:36) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
通过修改servlet bean来管理解决它,如下所示:
@Bean
public ServletRegistrationBean cache(TProtocolFactory protocolFactory, CacheServiceHandler handler) {
TServlet servlet = new TServlet(new TCacheService.Processor<SampleServiceControllerHandler>(handler), protocolFactory);
return new ServletRegistrationBean(servlet, "/cache");
}