gpt4 book ai didi

spring-boot - Spring Boot节俭

转载 作者:行者123 更新时间:2023-11-28 23:54:55 25 4
gpt4 key购买 nike

我正在学习本教程 https://dzone.com/articles/building-microservices-spring在 Spring 上使用节俭。当我运行教程时它运行良好,但是当我做一个例子时它没有做正确的映射。

@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 缓存映射到 [/]”,而它应该是“Servlet 缓存映射到 [/cache/]”

本教程是在 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");
}

关于spring-boot - Spring Boot节俭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53009822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com