Spring Cloud Eureka Server

Spring Cloud Eureka Server

运行Eureka Server

将Eureka Server集成到应用中,只需要在pom中加入

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

接着在启动类上加入@EnableEurekaServer

最后配置application.yml

1
2
3
4
5
6
7
8
9
10
11
server:
port: 8761

eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

高可用

在生产环境下,往往会部署多个eureka实例,使它们相互注册,实现的方式是通过修改配置实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
---
spring:
profiles: peer1
eureka:
instance:
hostname: peer1
client:
serviceUrl:
defaultZone: http://peer2/eureka/

---
spring:
profiles: peer2
eureka:
instance:
hostname: peer2
client:
serviceUrl:
defaultZone: http://peer1/eureka/

这样客户端在使用的时候,在指定注册中心时,指定多个,用逗号隔开

1
2
3
4
eureka:
client:
serviceUrl:
defaultZone: http://peer1/eureka/,http://peer2/eureka

安全的 Eureka Server

通过加入spring-boot-starter-security,使服务的注册与发现操作开启认证。

禁用CSRF

1
2
3
4
5
6
7
8
9
@EnableWebSecurity
class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().ignoringAntMatchers("/eureka/**");
super.configure(http);
}
}

在配置中加入

1
2
3
4
5
spring:
security:
user:
name: admin
password: pwd

修改defaultZone,指定name&password,这里需要注意的是,所有的配置defaultZone的地方都需要配置name&password,包括Eureka Server本身

1
2
3
4
5
6
7
8
9
eureka:
instance:
hostname: localhost
prefer-ip-address: true # 这里使用ip而不是默认的hostname
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://admin:pwd@${eureka.instance.hostname}:${server.port}/eureka/

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×