[Spring Cloud Eureka]
eureka-server项目
目录结构
1 2 3 4 5 6 7 8 9 10
| ├── pom.xml ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ └── tookbra │ │ │ └── eureka │ │ │ └── Application.java │ │ └── resources │ │ └── application.yml
|
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
|
application.java
1 2 3 4 5 6 7 8
| @EnableEurekaServer @SpringBootApplication public class Application {
public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
|
application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13
| spring: application: name: eureka
server: port: 10005
eureka: client: registerWithEureka: false fetchRegistry: false
|
启动eureka注册中心
运行spring-boot的maven插件
在游览器中打开http://localhost:10005/
我们可以看到注册中心的页面上涵盖了:
- 系统状态(环境、系统时间、续租时间等)
- 服务注册信息(服务名称、服务状态、服务地址)
- 基础信息(cpu、内存)
- 注册中心实例信息(ip地址、状态)
eureka 高可用
多个eureka服务相互注册,来保证服务高可用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| --- spring: profiles: node1 server: port: 10001
eureka: instance: hostname: node1 instanceId: node1
client: serviceUrl: defaultZone: http://node2:10002/eureka,http://node3:10003/eureka debug: true
--- spring: profiles: node2 server: port: 10002
eureka: instance: hostname: node2 instanceId: node2 client: serviceUrl: defaultZone: http://node1:10001/eureka,http://node3:10003/eureka
--- spring: profiles: node3 server: port: 10003
eureka: instance: hostname: node3 instanceId: node3 client: serviceUrl: defaultZone: http://node1:10001/eureka/,http://node2:10002/eureka/
|
eureka信息安全
在生产环境中,我们通常不会吧eureka暴露在外网
但是在开发环境中,我们可能会把eureka暴露在外网上,那么eureka的信息安全如何保证?
我们可以引入spring security,来做请求认证
1 2 3 4
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-security</artifactId> </dependency>
|
1 2 3 4 5 6
| security: basic: enabled: true user: name: user password: 123456
|
如果加了请求认证,需要把eureka.client.serviceUrl.defaultZone也加上认证头
1
| eureka.client.serviceUrl.defaultZone=http://user:password@node2:10002/eureka
|