SpringCloud-Eureka的搭建
搭建流程如下:
一.Eureka Server
- 新建SpringCloud项目
- 导入依赖
在pom.xml中导入eureka-server依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</<dependencies>
3.启动器
@SpringBootApplication
@EnableEurekaServer //该注解表示当前应⽤为Eureka Server
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
- 修改application.yml配置文件
server:
port: 9004 //当前服务的端口号
spring:
application:
name: eureka-server //注册到Eureka的服务名
eureka:
client:
service-url:
# eureka 服务地址,如果是集群的话;需要指定其它集群eureka地址
defaultZone: http://127.0.0.1:9004/eureka
# 是否注册到注册中心,默认为true
# 当前为eureka server,无需注册自己
register-with-eureka: false
# 是否从注册中心拉群服务列表,默认为true
# 当前为eureka server,无需拉取列表
fetch-registry: false
- 启动测试
启动应⽤访问http://localhost:9004/,效果如图所示。
二.Eureka Client
- 新建SpringCloud项目
- 导入依赖
在pom.xml中导入eureka-client依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka- client</artifactId>
</dependency>
</dependencies>
- 启动类
@SpringBootApplication
@EnableDiscoveryClient //该注解表示当前应⽤为Eureka Client,即eureka的客户端
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- 修改application.yml配置文件
server:
port: 9002 //当前服务的端口号
spring:
application:
name: cloud-order-service //注册到Eureka的服务名,之后可作为当前服务的id使用
eureka:
client:
service-url:
# eureka 服务地址,如果是集群的话;需要指定其它集群eureka地址
defaultZone: http://127.0.0.1:9004/eureka
# 是否注册到注册中心,默认为true
# register-with-eureka: true
# 是否从注册中心拉群服务列表,默认为true
# fetch-registry: true
启动应⽤访问http://localhost:9004,效果如图所示。