When creating a web application with SpringBoot, it is possible to start it by executing the artifact of the Jar file using the built-in Tomcat.
java -jar xxx.jar
However, since it is different from the conventional deployment with War file, I was addicted to the setting of cooperation with the built-in Tomcat + Apache + WebSocket.
The following JavaConfig will be the same as the traditional server.xml settings.
TomcatConfiguration.java
@Configuration
public class TomcatConfiguration
{
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
tomcat.addAdditionalTomcatConnectors(ajpConnector());
return tomcat;
}
private Connector ajpConnector() {
Connector connector = new Connector("org.apache.coyote.ajp.AjpNioProtocol");
connector.setAttribute("maxThreads", 100);
connector.setProtocol("AJP/1.3");
connector.setPort(8009);
connector.setRedirectPort(8043);
connector.setURIEncoding("UTF-8");
return connector;
}
}
server.xml
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
Add to gradle dependency
build.gradle
dependencies {
...
compile "org.springframework.boot:spring-boot-starter-websocket:1.3.3.RELEASE"
...
}
Add and enable the @EnableWebSocketMessageBroker
annotation
Application.java
@SpringBootApplication
@EnableWebSocketMessageBroker//add to
public class Application extends SpringBootServletInitializer
{
public static void main( String... args )
{
SpringApplication.run( Application.class, args );
}
}
WebSocketConfiguration.java
@Configuration
public class WebSocketConfiguration extends AbstractWebSocketMessageBrokerConfigurer { //Inherit AbstractWebSocketMessageBrokerConfigurer and customize WebSocket related bean definition
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/chat").setAllowedOrigins("*").withSockJS();//Endpoints subscribed to by the front desk
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/sock");
registry.enableSimpleBroker("/topic", "/queue");
}
}
ChatController.java
@RestController
@RequestMapping("/chat/")
public class ChatController
{
@MessageMapping( "/chat/{accountId}" )
@SendTo( "/topic/chat/all" )
public ChatList all( @DestinationVariable Integer accountId, ChatMessage message ) throws InterruptedException
{
...
}
}
This time we use Angular Js1 system
ChatService.js
import SockJS from "sockjs-client";
export function ChatService($http, ChatStore) {
let stompClient = {};
var connect = ( $scope ) => {
let socket = new SockJS( '/chat');
stompClient = Stomp.over(socket);
stompClient.connect({}, (frame) => {//WebSocket connection establishment
stompClient.subscribe('/topic/chat/all', ( chatList ) => {
...
});
});
}
var send = () => {
stompClient.send(`/sock/chat/${ChatStore.accountId}`, {},JSON.stringify({
'value': ChatStore.message
}));
}
return {
connect,
send
};
}
httpd.conf
ProxyPass /chat/info ajp://localhost:8009/chat/info #URL that connects SockJS and WebSocket automatically generated by SpringWebSocket
ProxyPass /chat ws://localhost:8080/chat #ws proxy settings
ProxyPass / ajp://localhost:8009/ #ajp proxy settings
With the above settings, the built-in Tomcat + Apache + WebSocket will work (should) work! !!
I've folded everything except the main points, but please forgive me as I will revise the article each time there is a mistake. .. .. orz
Well then.
Recommended Posts