springcloud Eureka相关配置
#注册中心配置
server:
port: 8761
eureka:
instance:
hostname: localhost #设置当前实例的主机名称
client:
registerWithEureka: false #是否启动服务注册
fetchRegistry: false #是否从eureka拉取服务实例注册信息,检索服务
serviceUrl:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/ #服务注册中心的地址
server:
eviction-interval-timer-in-ms: 10000
spring:
application:
name: eurekaServer #注册到服务器的名称
security: #配置保护机制的相关信息
user:
name: user
password: 123
#需在启动类之上添加@EnableEurekaServer注解
#需在启动类中添加如下安全代码:
@EnableWebSecurity
public static class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable(); #关闭CSRF认证
super.configure(http);
}
}
#需在启动类之上添加@EnableDiscoveryClient注解
#服务提供者配置
spring:
application:
name: eurekaProvider
eureka:
instance:
hostname: localhost
lease-renewal-interval-in-seconds: 5 #定义服务心跳的间隔
lease-expiration-duration-in-seconds: 15 #定义服务的失效时间
client:
serviceUrl:
defaultZone: http://user:123@localhost:8761/eureka/
server:
port: 10086
#服务消费者配置
server:
port: 10010
spring:
application:
name: eurekaConsumer
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://user:123@localhost:8761/eureka/
registry-fetch-interval-seconds: 10 #从eureka服务拉取服务的频率,单位/秒
其中配置项的命名应使用驼峰命名法代替原有IDE工具提示出的配置项名称
server-url 应使用 serverUrl 代替
在首次配置eureka是出现了一个非常低级的错误,当配置defaultZone(服务注册中心)的地址时,在provider提供方的配置文件中,配置服务中心的地址时,误将端口号配置为本配置文件中配置的端口号。结果一直报错,找了将近一天的原因,还是没有发生什么改变,服务一直注册不上,就在将要放弃的时候,猛地一瞥服务提供者的配置文件,我的天!!!竟然是端口号配置出错,唉!将端口号改为注册中心配置的端口号之后,终于注册上了,真是…!写一篇文章来记录一下此次犯得低级错误!!!