java中websocket和Feign使用
JAVA中WebSocket使用
首先导入Jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
// 包含一些需要的WebSocketAPI
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>
</dependency>
配置WebSocket
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter(){
return new ServerEndpointExporter();
}
}
使用
@Component
@ServerEndpoint(value = "/websocket/{uuid}")
public class WebSocketServer {
private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketServer.class);
@Resource
private HttpServletRequest request;
private Session session;
private static final ConcurrentHashMap<String, WebSocketServer> websocketMap = new ConcurrentHashMap<>();
@OnOpen
public void onOpen(@PathParam("uuid") String uuid, Session session) {
LOGGER.info("新增uuid==>" + uuid);
WebSocketServer webSocketServer = new WebSocketServer();
webSocketServer.session = session;
websocketMap.put(uuid, webSocketServer);
}
@OnClose
public void onClose(@PathParam("uuid") String uuid) {
LOGGER.info("移除的uuid==>" + uuid);
websocketMap.remove(uuid);
}
@OnError
public void onError(Throwable throwable){
throwable.printStackTrace();
}
@OnMessage
public void send(String message,Session session) throws IOException{
LOGGER.info("收到来自uuid的消息==>" + message);
}
public void sendMessage(String message) {
LOGGER.info("发送的消息是==>{}",message);
String cookieStr = request.getSession().getAttribute("uuid").toString();
LOGGER.info("发送人ID==>{}",cookieStr);
try {
websocketMap.get(cookieStr).session.getBasicRemote().sendText(message);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
一些错误
java.lang.IllegalStateException: Failed to register @ServerEndpoint class: class
Caused by: javax.websocket.DeploymentException: Cannot deploy POJO class
原因是使用了SpringAOP切面,websocket不能使用Spring中AOP的切面,将使用AOP切面的类移动到其它目录。
注意:在websocket中,如果websocket是在另一个请求中使用的,则在这个请求的过程中,websocket发送的消息将会被阻塞,解决方法是将该请求变为异步操作。
使用AJAX时,加上async: true即可
JAVA中远程请求使用Feign
1. 首先定义接口RemoteJgProxyPoolService
@FeignClient(name = ProxyPoolConstants.JG_NAME,url = ProxyPoolConstants.JG_URL,configuration = OkhttpConfig.class,fallbackFactory = RemoteJgProxyPoolFallbackFactory.class)
public interface RemoteJgProxyPoolService {
/**
* getProxy
*/
@GetMapping()
String getProxy();
}
2. 在定义实现接口的类
@Slf4j
@Component
public class RemoteProxyPoolFallbackImpl implements RemoteProxyPoolService {
@Setter
private Throwable cause;
@Override
public String getProxy() {
log.error("获取代理IP池{}失败", cause.getMessage());
return null;
}
}
3. 注入到bean容器中
@Component
public class RemoteJgProxyPoolFallbackFactory implements FallbackFactory<RemoteJgProxyPoolService> {
@Override
public RemoteJgProxyPoolService create(Throwable throwable) {
RemoteJgProxyPoolFallbackImpl remoteJgProxyPoolFallback = new RemoteJgProxyPoolFallbackImpl();
remoteJgProxyPoolFallback.setCause(throwable);
return remoteJgProxyPoolFallback;
}
}
4. 然后就可以使用了
@Resource
private final RemoteJgProxyPoolService remoteJgProxyPoolService;