项目源码:https://github.com/Vmetrio/SpringCloud
项目是最基础的框架(父子项目模式),运行流程看 github 的 README.md
网上讲解SpringCloud文章有很多了,推荐大家看一下方志鹏的博客:
https://blog.csdn.net/forezp/category_9268575.html
本文只是分享个人使用心得
1、eureka的安全性与高可用
eureka的安全性
①内网部署,私有ip,nginx做代理
②使用 spring security
pom文件:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
springboot启动文件:
#增加以下代码即可
@EnableWebSecurity
static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests()
.anyRequest()
.authenticated()
.and()
.httpBasic();
}
}
application.yml:
#增加用户和密码
spring:
security:
basic:
enabled: true
user:
name: meng
password: meng
eureka:
client:
serviceUrl:
#增加用户和密码
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@localhost8600/eureka/
eureka的高可用
把eureka部署到两台以上的服务器,搭建集群,相互注册即可
2、swagger搭建与安全
swagger搭建
pom文件:
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.9.0.RELEASE</version>
</dependency>
springboot启动文件:
增加Swagger注解,此方式比网上很多教程简单,方便,快捷
@EnableSwagger2Doc
举例:
@RestController
@Api(tags = "v1.0", value = "测试接口", description = "测试接口", produces = "application/json, application/xml")
public class DaoController {
private final static Logger logger = LoggerFactory.getLogger(DaoController.class);
@GetMapping(value = "/info")
@ApiOperation(value = "测试web数据传输")
@ApiImplicitParams({
@ApiImplicitParam(name = "name", value = "名称", dataType = "String", required=false, paramType = "query")
})
public String demo(@RequestParam("name") String name) {
String info = "欢迎 "+ name + " 使用SpringCloud脚手架!";
return info;
}
}
访问http://localhost:8500/swagger-ui.html
swagger的安全性
1、项目使用私有IP部署,nginx做代理
2、使用 spring security 对url:/swagger-ui.html 进行权限控制
3、服务划分的重要性
如果要使用 springcloud ,一定要对整体项目模块,进行详细的划分和讨论,不然后期很难处理。
4、服务的监控系统与安全
使用springboot官方的admin项目(简称:SBA),可以对每个服务进行实时监控。
底层用的是springboot的四大神器之 Actuator,具体配置请看我的另一篇博客:暂更
5、服务的链路追踪与安全
使用springcloud的Sleuth组件,对整体服务进行链路追踪
具体配置请看我的另一篇博客:暂更