Eureka的系统架构图
我们可以理解为 Eureka 是一个写字楼,里面有很多办公区租给各个公司使用,M公司再写字楼中租了一层楼在前台注册后,需要交物业费,如果三个月没有交物业费的话就无法使用了。而小A是一名访客,进入M公司参观也需要在前台注册。这样的话在写字楼前台就会有公司登记信息,访客登记信息,公司与访客都是client。
cloud-eureka-server7001
这个Module好比是一个前台
1.引入pom依赖
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
注意是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
,出现下面标识则配置成功
cloud-provider-payment8001
这个Module好比是M公司,需要在前台进行注册
1.引入pom依赖
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
此时作为client进行访问
2.application.properties
server.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/eureka
3.在主启动类上加注解
@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);
}
}