eureka 服务注册中心
zuul 网关/Filter
feign 微服务间调用(包含ribbon)
Hystringx 服务熔断
zipkin 链路追踪
rabbitMQ 消息队列
apoollo 集中配置中心
Swagger2 自动生成接口文档和客户端服务端代码
Swagger2使用
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.basePackage("com.xxx.controller"))
.paths(PathSelectors.any())
.build().apiInfo(new ApiInfoBuilder()
.title("标题")
.description("详细信息")
.version("9.0")
.contact(new Contact("111","222","333"))
.license("The Apache License")
.licenseUrl("http://www.baidu.com")
.build());
}
}
//在Controller类外面标注
// @Api 用来描述Controller
// @ApiOperation注解用来标记一个方法的作用。
// @ApiImplicitParam注解用来描述参数
@RestController
@Api(tags = "用户相关接口")
@RequestMapping("/user")
public class UserController {
@PostMapping("/")
@ApiOperation("添加用户")
@ApiImplicitParams({
@ApiImplicitParam(name = "uid", value = "用户名", defaultValue = "1"),
@ApiImplicitParam(name = "pwd", value = "密码", defaultValue = "", required = true)
}
)
public RespBean addUser(String username, @RequestParam(required = true) String address) {
return new RespBean();
}
}
密码加密与微服务鉴权JWT
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.6.0</version>
</dependency>
JWT是由三段信息构成的,将这三段信息文本用.链接一起就构成了Jwt字符串。就像这样:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
第一部分我们称它为头部(header)
第二部分我们称其为载荷(payload, 类似于飞机上承载的物品)
第三部分是签证(signature).
生成
//为了方便测试,我们将过期时间设置为1分钟
long now = System.currentTimeMillis();//当前时间
long exp = now + 1000*60;//过期时间为1分钟
JwtBuilder builder= Jwts.builder().setId("888")
.setSubject("小白")
.setIssuedAt(new Date())
.signWith(SignatureAlgorithm.HS256,"xiaocai")
.setExpiration(new Date(exp));//设置过期时间
System.out.println( builder.compact() );
验证
String token="eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI4ODgiLCJzdWIiOiLlsI_nmb0iLCJpYXQiO
jE1MjM0MTM0NTh9.gq0J‐cOM_qCNqU_s‐d_IrRytaNenesPmqAIhQpYXHZk";
Claims claims =
Jwts.parser().setSigningKey("xiaocai").parseClaimsJws(token).getBody();
System.out.println("id:"+claims.getId());
System.out.println("subject:"+claims.getSubject());
System.out.println("IssuedAt:"+claims.getIssuedAt());
网友评论