一、搭建服务
1、引eureka服务端的依赖:注意是服务端:spring-cloud-starter-netflix-eureka-server
<!--eureka服务端--><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies>
2、配置application.yml
server:
port: 36366
eureka:
server:
enable-self-preservation: false #自我保护机制关闭
instance:
hostname: localhost #服务地址
client:
#由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己
registerWithEureka: false
#由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
多个配置
步骤一、创建多个配置文件
application.yml #主配置文件
application-dev.yml #开发环境的配置
application-prod.yml #生产环境的配置
application-test.yml #测试环境的配置
步骤二、在application.yml中指定配置文件
#多环境配置
profiles:
active: dev
3、再application启动类中添加 eureka的开关注解:@EnableEurekaServer
二、配置客户端
1、引入eureka的客户端依赖,注意是客户端:spring-cloud-starter-netflix-eureka-client
<!--eureka 客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2、配置yml
server:
port: 8001
spring:
application:
name: eureka-provider-A
#服务发现客户端
eureka:
instance:
#向Eureka注册时,是否使用IP地址+端口号作为服务实例的唯一标识。推荐设置为true
prefer-ip-address: true
#主机名 设置为ip地址
hostname: ${spring.cloud.client.ip-address}
#实例设置唯一id 项目名称:ip地址:端口号
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}
client:
#是否将自身的实例信息注册到Eureka服务端
register-with-eureka: true
#是否拉取并缓存其他服务注册表副本到本地
fetch-registry: true
# eureka client刷新本地缓存时间
registryFetchIntervalSeconds: 5
#注册到哪个Eureka服务实例
service-url:
defaultZone: http://192.168.12.105:36366/eureka/
3、再application启动类中添加 eureka的主车注解@EnableDiscoveryClient
