springboot集成eureka
时间: 2023-04-25 12:01:55 浏览: 189
Spring Boot可以很方便地集成Eureka,实现服务注册和发现。以下是集成步骤:
1. 添加Eureka依赖
在pom.xml文件中添加以下依赖:
```
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
```
2. 配置Eureka Server
在Spring Boot应用的配置文件中添加以下配置:
```
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
```
其中,server.port指定Eureka Server的端口号,eureka.client.register-with-eureka和eureka.client.fetch-registry设置为false表示该应用不会向Eureka Server注册自己,也不会从Eureka Server获取服务列表。
3. 启动Eureka Server
在Spring Boot应用的启动类上添加@EnableEurekaServer注解,启动Eureka Server。
```
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
```
4. 配置Eureka Client
在Spring Boot应用的配置文件中添加以下配置:
```
server.port=808
spring.application.name=my-service
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
```
其中,server.port指定应用的端口号,spring.application.name指定应用的名称,eureka.client.service-url.defaultZone指定Eureka Server的地址。
5. 启动Eureka Client
在Spring Boot应用的启动类上添加@EnableDiscoveryClient注解,启动Eureka Client。
```
@SpringBootApplication
@EnableDiscoveryClient
public class MyServiceApplication {
public static void main(String[] args) {
SpringApplication.run(MyServiceApplication.class, args);
}
}
```
6. 测试
启动Eureka Server和Eureka Client后,可以在Eureka Server的管理界面(http://localhost:8761/)上看到注册的服务。可以通过Eureka Client的应用名称访问该服务,例如:http://localhost:808/hello。
以上就是Spring Boot集成Eureka的步骤。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)