SpringCloud版本:2021.0.1 SpringBoot版本:2.6.3
最近想搭建一个SpringCloud-微服务的脚手架,顺便也可以让自己练练手,熟悉熟悉一下微服务。
系列文章
SpringCloud学习(一)----- Eureka搭建
SpringCloud学习(二)----- SpringBoot Admin搭建(与Eureka整合)
SpringCloud学习(三)----- Gatewayw网关搭建
SpringCloud学习(四)----- Gatewayw网关完善(限流)
SpringCloud学习(五)----- Gatewayw网关完善(Resilience4j断路器)
SpringCloud学习(六)----- Gatewayw网关完善(防止SQL注入)
一、创建项目
1、打开IDEA,新建项目,选好自己用的jdk,直接下一步,之后项目的信息就自己填了
2、然后在这里选好自己要搭建的项目所需要的包,之后会自动引用,就不用自己再去引了,不过这里一般都会引用最新的,如果自己不改的话。
3、之后在项目的启动类上面加上@EnableEurekaServer这个注解
4、修改application.yml的配置,我用的是yml,默认不是这个文件类型的,这个看自己,想用啥用啥。
server:
port: 8001
spring:
application:
name: test-eureka
eureka:
instance:
hostname: 127.0.0.1
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
5、之后就可以启动了,不过这个是没有密码的,谁都可以进。
二、给eureka加上密码登陆的验证
1、修改application.yml的配置文件,加上账号密码
spring:
application:
name: test-eureka
security:
user:
name: test
password: test
2、pom.xml配置文件也得加上新依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
3、新建config文件夹,新建配置文件WebSecurityConfig
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// TODO Auto-generated method stub
// super.configure(http);
http.csrf().disable();
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}
4、再重启,就需要输入账号和密码了