# 用户中心

Account-ext是一个对用户中心(Account)常用、重要接口的二次封装。类似于Account-SDK的作用,使用方便,以及减少Account RestAPI代码开发,便于集成Account。不同之处在于加入了缓存这块(比如获取到的token进行缓存),还有通过hystrix来做资源隔绝,容错处理。具体如下:

  • a.封装了用户中心(Account)主要的Rest API的调用(依赖restClient);
  • b.加入了hystrix的容错机制,以及api调用的重试机制;
  • c.结合上面的hystrix增加了缓存机制(可以根据需要是否使用缓存),尽可能保证你的服务高可用,高可靠;
  • d.简单的性能监控日志输出。

account-ext 简介

account-ext

# 组成结构

目前包括下面4个模块: account-ext-core 用来实现调用Account RestAPI,获取相关数据。 account-ext-cache 依赖上面的core包,同时将上面的数据,进行缓存。 account-ext-hystrix 依赖上面的cache包,同时做到了资源隔绝,容错处理。 account-ext-filter RemoteTokenService.java,是Account-SDK中这个类的重写,实现了Spring-OAuth2 中ResourceServerTokenServices接口的实现,。这个类你可以根据自己的需要初始化的时候通过构造方法注入一个AccountService的实例对象进行初始化。

上面这些模块中,core包,cache,以及hystrix包共同点是,都实现了AccountService接口,所以这3个包都能满足对Account RestApi的调用。

# 什么时候需要使用

  • 统一你的应用对用户中心 oauth2 api的调用。
  • 当你的服务需要对用户中心 oauth2 api 做容错处理,可以考虑用这个包中的account-ext-hystrix模块。
  • 当然如果在某些情况下你不想使用hystrix的容错机制,也可以用这个包account-ext-core,或者account-ext-cache模块提供的AccountService来实现对account api的调用。

# 如何使用

# 1.配置settings.xml私仓地址

<!-- scg maven仓库 -->
<repository>
    <id>scg-private</id>
    <name>maven-scg-private</name>
    <url>http://packages.glodon.com/artifactory/maven-scg-private/</url>
</repository>
1
2
3
4
5
6

# 2项目中添加 pom 依赖

<dependency>
    <groupId>com.glodon.cloud</groupId>
    <artifactId>account-ext-spring-boot-starter</artifactId>
     <version>1.0.0</version>
</dependency>
1
2
3
4
5

如果结合spring-cloud-glodon使用,则通过引入spring-cloud-glodon-dependencies后管理依赖版本即可

<dependency>
    <groupId>com.glodon.cloud</groupId>
    <artifactId>account-ext-spring-boot-starter</artifactId>
</dependency>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.glodon.cloud</groupId>
            <artifactId>spring-cloud-glodon-dependencies</artifactId>
            <version>2.3.2.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 3配置yaml

使用account-ext-core模块进行自动装配时:

glodon:
  account-ext:
    core:
      accessKeyId: xxxxxx
      accessKeySecret: xxxxxx
      # 是否开启监控,默认false
      enable-metrics-collector: true
1
2
3
4
5
6
7

使用account-ext-cache模块进行自动装配时:

glodon:
  account-ext:
    cache:
      accessKeyId: xxxxxx
      accessKeySecret: xxxxxx
      # 是否开启监控,默认false
      enable-metrics-collector: true
      # redis 
      redisHost: host-application
      # 如果没有可忽略
      redisPassword: xxxxx
1
2
3
4
5
6
7
8
9
10
11

使用account-ext-hystrix模块进行自动装配时:

glodon:
  account-ext:
    hystrix:
      accessKeyId: xxxxxx
      accessKeySecret: xxxxxx
      # redis 
      redisHost: host-application
      # 如果没有可忽略
      redisPassword: xxxxx
1
2
3
4
5
6
7
8
9

使用account-ext-token-service模块进行自动装配时:

glodon:
  account-ext:
    token-service:
      # 可选项core/cache/hystrix,表示RemoteTokenService由哪个模块进行构造
      serviceType: core/cache/hystrix
      accessKeyId: xxxxxx
      accessKeySecret: xxxxxx
      # redis 
      redisHost: host-application
      # 如果没有可忽略
      redisPassword: xxxxx
1
2
3
4
5
6
7
8
9
10
11

# 4注入对象

// account-ext-core,account-ext-cache模块自动装配
@Resource
private AccountService<?> service;

// account-ext-hystrix模块自动装配
@Resource(name = "accountExtHystrixService")
private AccountService<?> service;

// account-ext-token-service模块自动装配
@Resource
private RemoteTokenService remoteTokenService;
1
2
3
4
5
6
7
8
9
10
11

# 5使用

String serviceToken = service.getServiceToken().getAccessToken();

String applicationAccessToken = remoteTokenService.getApplicationAccessToken().getValue();
1
2
3
  • 在线客服

  • 意见反馈