step 1 , 创建maven工程
引入 pom 依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/>
</parent>
<properties>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<global.version>1.0.0</global.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
step 2 , 引入eureka server需要的依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
step 3 , 启动类配置
@EnableEurekaServer
@SpringBootApplication
public class RegisterCenterApplication {
public static void main(String[] args) {
SpringApplication.run(RegisterCenterApplication.class, args);
}
}
step 4 , 配置security 类
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();//关闭csrf
super.configure(http);
}
}
step 5 , YML 文件的配置
#========================
#========================
# 独立的服务器配置
#========================
#========================
spring:
application:
name: eureka-center
security:
user:
name: test001
password: test001
#服务注册中心端口号
server:
port: 8761
eureka:
instance:
#服务注册中心实例的主机名
hostname: eureka-center
prefer-ip-address: true
client:
serviceUrl:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@192.168.0.114:${server.port}/eureka/
#是否向服务注册中心注册自己
register-with-eureka: false
#是否检索服务
fetch-registry: false
step 6 ,运行 application 文件 启动程序
2019-12-30 22:44:54.162 INFO 15348 --- [ Thread-13] o.s.c.n.e.server.EurekaServerBootstrap : Initialized server context
2019-12-30 22:44:54.162 INFO 15348 --- [ Thread-13] c.n.e.r.PeerAwareInstanceRegistryImpl : Got 1 instances from neighboring DS node
2019-12-30 22:44:54.163 INFO 15348 --- [ Thread-13] c.n.e.r.PeerAwareInstanceRegistryImpl : Renew threshold is: 1
2019-12-30 22:44:54.163 INFO 15348 --- [ Thread-13] c.n.e.r.PeerAwareInstanceRegistryImpl : Changing status to UP
2019-12-30 22:44:54.168 INFO 15348 --- [ Thread-13] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2019-12-30 22:44:54.181 INFO 15348 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8761 (http) with context path ''
2019-12-30 22:44:54.182 INFO 15348 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8761
2019-12-30 22:44:54.184 INFO 15348 --- [ main] com.grantz.RegisterCenterApplication : Started RegisterCenterApplication in 9.796 seconds (JVM running for 10.363)
step 7 ,输入地址查看注册中心服务
url: localhost:8761 账号密码: test001
登录成功页面: