org.zodiac.autoconfigure.netty2.EnableNettyEmbedded Maven / Gradle / Ivy
package org.zodiac.autoconfigure.netty2;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Import;
import org.zodiac.autoconfigure.netty2.server.NettyEmbeddedAutoConfiguration;
import org.zodiac.netty.core.AbstractProtocol;
import org.zodiac.netty.springboot2.server.HttpServletProtocolSpringAdapter;
import org.zodiac.netty.springboot2.server.NRpcProtocolSpringAdapter;
import org.zodiac.netty.springboot2.server.NettyRequestUpgradeStrategy;
import org.zodiac.netty.springboot2.server.NettyTcpSpringServerFactory;
import java.lang.annotation.*;
/**
* Enable embedded TCP container.
* The following functions will be enabled.
*
*
* -
* HTTP server protocol, Servlet Web or Reactive Web. {@link NettyTcpSpringServerFactory} {@link HttpServletProtocolSpringAdapter}
* Websocket. {@link NettyRequestUpgradeStrategy}
*
* - RPC server protocol. {@link NRpcProtocolSpringAdapter}
* - Other user-defined protocols.
*
*
*
* If you want to add your own protocol, you only need implement {@link AbstractProtocol}, Next restart, do not need to do other things
*
{@code
* \@Component
* public class MyProtocolsRegister extends AbstractProtocolsRegister{
* public static final byte[] PROTOCOL_HEADER = {
* 'M', 'Y',
* 'H', 'E', 'A', 'D', 'E', 'R'
* };
*
* public String getProtocolName() {
* return "my-protocol";
* }
*
* public boolean canSupport(ByteBuf msg) {
* if (msg.readableBytes() < PROTOCOL_HEADER.length) {
* return false;
* }
* for (int i = 0; i < PROTOCOL_HEADER.length; i++) {
* if (msg.getByte(i) != PROTOCOL_HEADER[i]) {
* return false;
* }
* }
* return true;
* }
*
* public void addPipeline(Channel channel, ByteBuf clientFirstMsg) throws Exception {
* channel.pipeline().addLast(new StringDecoder());
* channel.pipeline().addLast(new StringEncoder());
* channel.pipeline().addLast(new MyChannelHandler());
* }
* }
*
* }
*
* -----------------------------------------------------------
* If you want to enable websocket protocol, you need use NettyRequestUpgradeStrategy.class.
* example..
*
{@code
* public class WebsocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
* public RequestUpgradeStrategy requestUpgradeStrategy() {
* // return new JettyRequestUpgradeStrategy();
* // return new TomcatRequestUpgradeStrategy();
* return new NettyRequestUpgradeStrategy();
* }
*
* public void registerStompEndpoints(StompEndpointRegistry registry) {
* StompWebSocketEndpointRegistration endpoint = registry.addEndpoint("/my-websocket");
* endpoint.setHandshakeHandler(new DefaultHandshakeHandler(requestUpgradeStrategy()) {
* protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map attributes) {
* String token = request.getHeaders().getFirst("access_token");
* return () -> token;
* }
* });
* endpoint.setAllowedOrigins("*").withSockJS();
* }
*
* public void configureMessageBroker(MessageBrokerRegistry registry) {
* registry.enableSimpleBroker("/topic/");
* registry.setApplicationDestinationPrefixes("/app");
* registry.setUserDestinationPrefix("/user/");
* }
* }
* }
*
* @see NettyServerProperties
* @see org.zodiac.autoconfigure.netty2.server.NettyEmbeddedAutoConfiguration
* @see org.zodiac.netty.springboot2.server.NettyTcpSpringServerFactory
* @see org.zodiac.netty.springboot2.server.HttpServletProtocolSpringAdapter
* @see NRpcProtocolSpringAdapter
* @see org.zodiac.netty.springboot2.server.NettyRequestUpgradeStrategy
* @see AbstractProtocol
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Inherited
//@Import(value = {NettyEmbeddedAutoConfiguration.class})
//@EnableConfigurationProperties(value = {NettyServerProperties.class})
public @interface EnableNettyEmbedded {
}