本文共 6324 字,大约阅读时间需要 21 分钟。
新建个主工程(这个pom文件作为父pom文件,起到依赖版本控制的作用,其他module工程继承该pom)并删除掉src目录
父pom.xml代码如下
4.0.0 com.example chapter1 0.0.1-SNAPSHOT jar chapter1 Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.1.0.RELEASE eureka-server eureka-client UTF-8 UTF-8 1.8 Greenwich.M1 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
二、创建eureka-server子工程(右键父工程 new module)
eureka-server pom.xml代码如下
4.0.0 eureka-server 0.0.1-SNAPSHOT jar eureka-server Demo project for Spring Boot com.example chapter2 0.0.1-SNAPSHOT org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-devtools true org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/milestone false
application.properties配置文件:
#服务启动端口server.port=8001#主机地址eureka.instance.hostname=localhost#这两个false表明这个服务为eureka servereureka.client.fetch-registry=falseeureka.client.register-with-eureka=false#服务地址eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/#服务名称spring.application.name=eureka-server
EurekaServerApplication 启动类:
package com.example;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;/** * @author xiaobu */@EnableEurekaServer@SpringBootApplicationpublic class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); }}
输入界面如下所示:
三、在父工程右键新建一个eureka-client模块 选择 eureka-discovery
对应的pom.xml如下所示
4.0.0 eureka-client 0.0.1-SNAPSHOT jar eureka-client eureka-client for Spring Boot com.example chapter1 0.0.1-SNAPSHOT UTF-8 UTF-8 1.8 org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/milestone false
application.properties配置如下:
eureka.client.service-url.defaultZone=http://localhost:8001/eureka/server.port=8002spring.application.name=eureka-client
启动类EurekaClientApplication配置如下:
package com.example;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;/** * @author xiaobu */@EnableEurekaClient@RestController@SpringBootApplicationpublic class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } @Value("${server.port}") String port; @RequestMapping("/test") public String test(@RequestParam(value = "name", defaultValue = "admin") String name) { return name +"test ,i am from port:" + port; }}
启动eureka-client刷新http://localhost:8001 效果如下所示:
输入 效果图如下:
启动多个实例以及自动编译(修改端口,取消热编译)
转载地址:http://ougai.baihongyu.com/