Spring Boot可以很方便地集成
,实现服务注册和发现。以下是集成步骤: 1. 添加
依赖 在pom.xml文件中添加以下依赖: ```
-server ``` 2. 配置
Server 在Spring Boot应用的配置文件中添加以下配置: ``` server.port=8761
.client.register-with-
=false
.client.fetch-registry=false ``` 其中,server.port指定
Server的端口号,
.client.register-with-
.client.fetch-registry设置为false表示该应用不会向
Server注册自己,也不会从
Server获取服务列表。 3. 启动
Server 在Spring Boot应用的启动类上添加@Enable
Server注解,启动
Server。 ``` @SpringBootApplication @Enable
Server public class
ServerApplication { public static void main(String[] args) { SpringApplication.run(
ServerApplication.class, args); } } ``` 4. 配置
Client 在Spring Boot应用的配置文件中添加以下配置: ``` server.port=808 spring.application.name=my-service
.client.service-url.defaultZone=http://localhost:8761/
/ ``` 其中,server.port指定应用的端口号,spring.application.name指定应用的名称,
.client.service-url.defaultZone指定
Server的地址。 5. 启动
Client 在Spring Boot应用的启动类上添加@EnableDiscoveryClient注解,启动
Client。 @SpringBootApplication @EnableDiscoveryClient public class MyServiceApplication { public static void main(String[] args) { SpringApplication.run(MyServiceApplication.class, args); } }
6. 测试 启动
Server和
Client后,可以在
Server的管理界面(http://localhost:8761/)上看到注册的服务。可以通过
Client的应用名称访问该服务,例如:http://localhost:808/hello。 以上就是Spring Boot集成
的步骤。
Original: https://blog.csdn.net/weixin_43548119/article/details/122844547
Author: Vezzz
Title: Eureka 优化