采用父-module 模块开发
父工程 -pom.xml
<!--配置 springboot的依赖的版本号, 方便 module 进行继承--> <dependencymanagement> <dependencies> <!--增加 springboot的依赖--> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-dependencies</artifactid> <version>3.2.5</version> <type>pom</type> <scope>import</scope> </dependency> <!--增加 springcloud的依赖--> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>2023.0.1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymanagement>
子模块 eureka server
pom.xml
<dependencies> <!--增加 boot web的依赖--> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <!--增加 eureka-server 的依赖--> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-netflix-eureka-server</artifactid> </dependency> </dependencies>
启动类:
package com.ly; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cloud.netflix.eureka.server.enableeurekaserver; @springbootapplication @enableeurekaserver public class cloudeurekaserver7001 { public static void main(string[] args) { springapplication.run(cloudeurekaserver7001.class,args); } }
配置文件 application.yml
# 设置端口号为 7001 server: port: 7001 eureka: instance: hostname: localhost client: fetch-registry: false #如果fetch-registry为false, 则表示自己为注册中心 register-with-eureka: false #表示是否向eureka注册中心注册自己 service-url: defaultzone: http://${eureka.instance.hostname}:${server.port}/eureka # 服务地址
启动测试:
子模块 eureka-provider-8001
pom.xml
<dependencies> <!--增加 boot web的依赖--> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <!--增加 eureka client 依赖--> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-netflix-eureka-client</artifactid> </dependency> <!--增加 监控 boot 依赖--> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-actuator</artifactid> </dependency> </dependencies>
启动类
package com.ly; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cloud.client.discovery.enablediscoveryclient; @springbootapplication @enablediscoveryclient public class eurekaprovider8001 { public static void main(string[] args) { springapplication.run(eurekaprovider8001.class,args); } }
application.yaml
#设置端口号 server: port: 8001 eureka: client: fetch-registry: true #是提供者,不是注册中心 ,可省略 register-with-eureka: true #向注册中心 注册服务,可省略 service-url: #服务地址 defaultzone: http://localhost:7001/eureka
刷新 之前的 server
发表评论