令牌

concrete service本质上讲也是无状态的,所以,设计了令牌用来与客户端维持状态。和servlet的session相似,不同的是,concrete支持不限定服务提供方式。下面,我们来体验以下令牌的使用。

新建TokenDemoService,写实现,boot里例外掉,这些操作应该都熟悉了吧,不再一端端的贴了。

package org.coodex.concrete.demo.api;

import org.coodex.concrete.api.ConcreteService;

@ConcreteService
public interface TokenDemoService {

    /**
     * 往token里设置一个值
     * @param value
     */
    void setTokenValue(String value);

    /**
     * 获取token中的值
     * @return
     */
    String getTokenValue();

}
package org.coodex.concrete.demo.impl;

import org.coodex.concrete.common.Token;
import org.coodex.concrete.demo.api.TokenDemoService;

import javax.inject.Inject;
import javax.inject.Named;

@Named
public class TokenDemoServiceImpl implements TokenDemoService {

    private final static String TOKEN_KEY = "DEMO_KEY";

    @Inject // <-- 注入token
    private Token token;

    @Override
    public String getTokenValue() {
        return token.getAttribute(TOKEN_KEY, String.class);
    }

    @Override
    public void setTokenValue(String value) {
        token.setAttribute(TOKEN_KEY, value);
    }
}
package org.coodex.concrete.demo.client;

import org.coodex.concrete.Client;
import org.coodex.concrete.demo.api.TokenDemoService;

public class TokenDemoClient {

    public static void main(String[] args) {
        TokenDemoService tokenDemoService = Client.getInstance(TokenDemoService.class, "jaxrs");
        tokenDemoService.getTokenValue();
        tokenDemoService.setTokenValue("concrete");
        tokenDemoService.getTokenValue();
    }
}

客户端执行结果:

18:13:01.867 [main] INFO org.coodex.concrete.jaxrs.logging.ClientLogger - 1 * Sending client request on thread main
1 > GET http://localhost:8080/jaxrs/OrgCoodexConcreteDemoApiTokenDemoService/tokenValue
1 > Accept-Language: zh-CN
1 > X-CLIENT-PROVIDER: cocnrete-jaxrs-client 0.4.0-SNAPSHOT

18:13:03.162 [main] INFO org.coodex.concrete.jaxrs.logging.ClientLogger - 1 * Client response received on thread main
1 < 204
1 < Date: Tue, 06 Aug 2019 10:13:03 GMT

18:13:03.196 [main] INFO org.coodex.concrete.jaxrs.logging.ClientLogger - 2 * Sending client request on thread main
2 > PUT http://localhost:8080/jaxrs/OrgCoodexConcreteDemoApiTokenDemoService/tokenValue
2 > Accept-Language: zh-CN
2 > Content-Type: application/json;charset=UTF-8
2 > X-CLIENT-PROVIDER: cocnrete-jaxrs-client 0.4.0-SNAPSHOT
concrete

18:13:03.249 [main] INFO org.coodex.concrete.jaxrs.logging.ClientLogger - 2 * Client response received on thread main
2 < 204
2 < CONCRETE-TOKEN-ID: 5f2f3a1df9f349e8b0e71b38e50ce878
2 < Date: Tue, 06 Aug 2019 10:13:03 GMT

18:13:03.254 [main] INFO org.coodex.concrete.jaxrs.logging.ClientLogger - 3 * Sending client request on thread main
3 > GET http://localhost:8080/jaxrs/OrgCoodexConcreteDemoApiTokenDemoService/tokenValue
3 > Accept-Language: zh-CN
3 > CONCRETE-TOKEN-ID: 5f2f3a1df9f349e8b0e71b38e50ce878
3 > X-CLIENT-PROVIDER: cocnrete-jaxrs-client 0.4.0-SNAPSHOT

18:13:03.277 [main] INFO org.coodex.concrete.jaxrs.logging.ClientLogger - 3 * Client response received on thread main
3 < 200
3 < Content-Length: 8
3 < Content-Type: text/plain;charset=UTF-8
3 < Date: Tue, 06 Aug 2019 10:13:03 GMT
concrete

我们看到,第一次请求tokenValue的时候,返回204,也就是no content,对于java client实际上就是null,第二次请求是设置tokenValue,这时候响应里返回了一个CONCRETE-TOKEN-ID: 5f2f3a1df9f349e8b0e71b38e50ce878,这个就是令牌的id,这个令牌就是维护与客户端之前状态用的,第三次请求,带着上一个令牌,再去取tokenValue,结果取到了设置的值,达到预期。

令牌的持久化是可扩展的,我们实例中的令牌是在内存中,concrete-core还提供了一个org.coodex.concrete.core.token.sharedcache.SharedCacheTokenManager使用redismemcached管理令牌,为集群应用提供支持。当然,开发者也可以自行实现org.coodex.concrete.core.token.TokenManager来进行扩展。

(ɔ) coodex.org 2016 - 2020            修订时间: 2020-03-09 02:56:39

results matching ""

    No results matching ""