尝试从微服务内部连接数据库,但无法连接数据库
错误:连接ECONNREFUSED 172.18.0.2:3306
服务/index.js
var http = require('http');
//create a server object:
http.createServer(function (req, res) {
res.write('Hello World!'); //write a response to the client
res.end(); //end the response
}).listen(8080); //the server object listens on port 8080
console.log("Listening at 8080");
var mysql = require('mysql');
var con = mysql.createConnection({
host: "database",
user: "root",
password: "password"
});
con.connect(function(err) {
if (err) throw err;
console.log("Database Connected!");
});
Run Code Online (Sandbox Code Playgroud)
docker-compose.yml
version: '3'
services:
database:
build:
./database
ports:
- "6603:3306"
image: "test-mysql"
container_name: "test-mysql"
service:
build:
./service …Run Code Online (Sandbox Code Playgroud) 我正在尝试修改大猩猩聊天示例,以将消息发送到特定客户端而不是广播。首先,我根据特定客户端的 ID 将其存储在集线器中。
Hub.go
type Hub struct {
Clients map[int]*Client // Changed this piece to store id (int)
Broadcast chan []byte
Register chan *Client
Unregister chan *Client
}
func (h *Hub) Run() {
for {
select {
case client := <-h.Register:
fmt.Println("hub client register")
h.Clients[client.Id] = client
case client := <-h.Unregister:
fmt.Println("hub client Unregister")
fmt.Println(h.Clients[client.Id])
if h.Clients[client.Id] != nil {
delete(h.Clients, client.Id)
close(client.Send)
}
case message := <-h.Broadcast:
fmt.Println("to send to a specific client", string(message))
}
}
}
Run Code Online (Sandbox Code Playgroud)
客户
我在 …
哪种情况会导致调用此处理程序?我找不到此方法引发错误的任何实例。
我尝试离线使用设备,xmlHttpRequest.status = 0但没有出现错误。
问题是我可以创建哪种情况来测试此处理程序的功能。
var xmlhttp = new XMLHttpRequest(),
method = 'GET',
url = 'https://developer.mozilla.org/';
xmlhttp.open(method, url, true);
xmlhttp.onerror = function () {
console.log("** An error occurred during the transaction");
};
xmlhttp.send();
Run Code Online (Sandbox Code Playgroud)
来自:https : //developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequestEventTarget/onerror
我有一个以下实用程序类,但是每当我通过验证方法检查过期的令牌时,它都不会抛出JWtVerificationException.
public class Token {
private static String SECRET = "c3bff416-993f-4760-9275-132b00256944";
public static String get(String name, String value) throws UnsupportedEncodingException {
return JWT.create()
.withIssuer("auth0")
.withClaim(name, value)
.withClaim("random", String.valueOf(UUID.randomUUID()))
.withExpiresAt(new Date(System.currentTimeMillis() + (4 * 60 * 60 * 1000)))
.sign(Algorithm.HMAC256(Token.SECRET));
}
public static DecodedJWT verify(String token) throws JWTVerificationException, UnsupportedEncodingException {
JWTVerifier verifier = JWT.require(Algorithm.HMAC256(Token.SECRET))
.withIssuer("auth0")
.acceptExpiresAt(4)
.build();
return verifier.verify(token);
}
}
Run Code Online (Sandbox Code Playgroud)
根据网站https://github.com/auth0/java-jwt
验证令牌时,时间验证会自动发生,从而导致
JWTVerificationException在值无效时抛出异常。
编辑:
当客户端每 5 分钟更新一次令牌时,接下来的工作是否有效,或者我应该添加几秒钟来适应任何网络延迟?
创造
.withExpiresAt(new Date(System.currentTimeMillis() + (5 * 60 * 1000))) …Run Code Online (Sandbox Code Playgroud)