Spring Cloud Eureka注册中心
使用Eureka作为服务注册中心
本文主要包含3部分,1. 启动注册中心;2. 服务提供者注册服务;3. 服务消费者,请求注册中心中发布的服务
1. 创建项目,添加maven依赖
创建maven项目,取名为eureka-server,添加以下依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath />
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2. 注册中心启动类
创建Eureka启动类EurekaServerApplication;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
3. 添加配置文件application.properties
spring.application.name=eureka-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
spring.security.user.name=fragrans
spring.security.user.password=123456
eureka.server.enable-self-preservation=false
eureka.server.eviction-interval-timer-in-ms=5000
4. 启动注册中心
运行EurekaServerApplication ,访问localhost:8761,即可登陆到注册中心管理页面,如下图所示,输入配置文件中spring.security的用户名和密码,
看到上图所示的Web控制台,表示Eureka注册中心启动成功,接下来将服务提供者和消费者注册到该Eureka注册中心即可。
5. 代码样例github地址
服务器提供者注册服务 和 服务消费者,请参考接下来的文章…
参考资料:《Spring Cloud微服务 入门 实战与进阶》