编写Eureka Server
加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
加注解
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
写配置
server:
port: 8761
eureka:
client:
# 是否要注册到其他Eureka Server实例
register-with-eureka: false
# 是否要从其他Eureka Server实例获取数据
fetch-registry: false
service-url:
defaultZone: http://localhost:8761/eureka/
将应用注册到Eureka Server上
加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
加注解
@EnableDiscoveryClient
@SpringBootApplication
public class ProviderUserApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderUserApplication.class, args);
}
}
添加配置:
spring:
application:
name: springcloud-consumer
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
# 是否注册IP到eureka server,如不指定或设为false,那就会注册主机名到eureka server
prefer-ip-address: true
Eureka安全
Eureka本身不具备安全认证的能力,Spring Cloud使用Spring Security为Eureka Server进行了增强。
在Eureka Server端,pom添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
在bootstrap.yml添加如下配置
spring:
security:
user:
name: admin # 配置登录的账号是admin
password: admin # 配置登录的密码是admin
# 将Eureka Server中的 eureka.client.service-url.defaultZone 修改为为http://{user}:{password}@EUREKA_HOST:EUREKA_PORT/eureka/ 的形式:
eureka:
client:
service-url:
defaultZone: http://admin:admin@localhost:8761/eureka/
启动Eureka Server并访问http://localhost:8761 ,可跳转至登录页面,输入账号admin ,密码admin 后,即可正常访问Eureka Server首页。
Eureka集群
修改host
vim /etc/hosts
# 添加如下内容
127.0.0.1 peer1
127.0.0.1 peer2
127.0.0.1 peer3
配置:
spring:
application:
name: @artifactId@
profiles:
active: dev
security:
user:
name: admin
password: admin
---
spring:
profiles: dev
server:
port: 8761
eureka:
instance:
hostname: springcloud-eureka #eureka服务端的实例名称
prefer-ip-address: true
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://admin:admin@${eureka.instance.hostname}:8761/eureka/
---
spring:
profiles: peer1
server:
port: 8761
eureka:
instance:
hostname: peer1 #eureka服务端的实例名称
prefer-ip-address: true
instance-id: peer1
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://admin:admin@localhost:8762/eureka/,http://admin:admin@localhost:8763/eureka/
---
spring:
profiles: peer2
server:
port: 8762
eureka:
instance:
hostname: peer2 #eureka服务端的实例名称
prefer-ip-address: true
instance-id: peer2
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://admin:admin@localhost:8761/eureka/,http://admin:admin@localhost:8763/eureka/
---
spring:
profiles: peer3
server:
port: 8763
eureka:
instance:
hostname: peer3 #eureka服务端的实例名称
prefer-ip-address: true
instance-id: peer3
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://admin:admin@localhost:8761/eureka/,http://admin:admin@localhost:8762/eureka/
启动:
java -jar microservice-discovery-eureka-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar microservice-discovery-eureka-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
java -jar microservice-discovery-eureka-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer3
第一个实例会报错,这是正常的,因为它会尝试连接其他两个实例,但第三个实例尚未启动,所以会报连接不上的异常。
对Spring Boot的Profile不熟悉,贴个拓展阅读
SpringBoot - 多Profile使用与切换
将应用注册到Eureka Server集群上
eureka:
client:
serviceUrl:
defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/,http://peer2:8763/eureka/
网友评论