我们可以理解为 Eureka 是一个写字楼,里面有很多办公区租给各个公司使用,M公司再写字楼中租了一层楼在前台注册后,需要交物业费,如果三个月没有交物业费的话就无法使用了。而小A是一名访客,进入M公司参观也需要在前台注册。这样的话在写字楼前台就会有公司登记信息,访客登记信息,公司与访客都是client。
这个Module好比是一个前台
1.引入pom依赖
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
注意是spring-cloud-starter-netflix-eureka-server而不是spring-cloud-starter-netflix-eureka-client
2.application.properties配置如下:
server.port=7001
#eureka服务端的实例名称
eureka.instance.hostname=localhost
#false表示不向注册中心注册自己。
eureka.client.register-with-eureka=false
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
eureka.client.fetch-registry=false
#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址。
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
3.在主启动类上加注解
@SpringBootApplication
@EnableEurekaServer //Eureka注解
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class,args);
}
}
4.自测
访问http://localhost:7001,出现下面标识则配置成功
这个Module好比是M公司,需要在前台进行注册
1.引入pom依赖
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
此时作为client进行访问
2.application.propertiesserver.port=8001 #名字不要轻易变动,会在http://localhost:7001上显示 spring.application.name=cloud-payment-service #表示是否将自己注册进Eurekaserver默认为true。 eureka.client.register-with-eureka=true #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 eureka.client.fetch-registry=true #入住地址 eureka.client.service-url.defaultZone=http://localhost:7001/eureka3.在主启动类上加注解
@MapperScan("com.winky.springcloud.mapper")
@ServletComponentScan(basePackages = "com.winky.springcloud")
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
}
}
4. 同理处理cloud-consumer-order8080
5. 自测
如果出现上图则说明注册成功


