Eureka是Netflix公司开源的一个服务注册与发现的组件。和Consul、Zookeeper类似。
2.项目结构
父工程pom
3.创建子工程(服务注册发现中心) eureka-server4.0.0 com.porridge.www springcloud-eureka pom 1.0-SNAPSHOT eureka-server eureka-client org.springframework.boot spring-boot-starter-parent 2.0.3.RELEASE UTF-8 UTF-8 1.8 Finchley.RELEASE org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import
pom文件
springcloud-eureka com.porridge.www 1.0-SNAPSHOT 4.0.0 eureka-server org.springframework.cloud spring-cloud-starter-netflix-eureka-server
application.yml文件
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eurka-server
EurekaServerApplication
@SpringBootApplication
//开启eureka注册发现
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
访问localhost:8761
pom文件
springcloud-eureka com.porridge.www 1.0-SNAPSHOT 4.0.0 eureka-client org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web
application.yml
server:
port: 8762
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: eurka-client
EurekaClientApplication
@SpringBootApplication
//开启向eureka注册功能
@EnableEurekaClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class,args);
}
}
IndexController控制层
@RestController
public class IndexController {
@RequestMapping("/")
public String indexService(){
return "this is indexService";
}
}
再次访问localhost:8761
访问localhost:8762



