# shareprog-eureka
**Repository Path**: shareprogram/shareprog-eureka
## Basic Information
- **Project Name**: shareprog-eureka
- **Description**: 服务发现和治理--Eureka
- **Primary Language**: Unknown
- **License**: MIT
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2021-11-14
- **Last Updated**: 2024-09-17
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# Eureka
截止文档更新时间,eureka停止维护仅是2.x版本,一概论之是谣传,java17版本已升级至4.1.0版本。
## 1.POM文件
```xml
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-security
```
## 2.启动类
```java
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
```
## 3.配置文件(bootstrap.yml)
```yml
server:
port: 8001
spring:
application:
name: eureka
security:
user:
name: eureka
password: eureka
eureka:
instance:
hostname: 127.0.0.1 #应用实例主机名
client:
register-with-eureka: false #是否向注册中心注册自己
fetch-registry: false #是否从Eureka获取注册信息
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
# server:
# wait-time-in-ms-when-sync-empty: 5 #在服务器接收请求之前等待的初始时间(分钟),用于生产配置
# enable-self-preservation: false #自我保护是否启用,用于测试配置
# eviction-interval-timer-in-ms: 5000 #清理间隔,用于测试配置
```
## 4.相关配置文件
由于在上面的配置文件中添加security配置,所以有认证配置,如果只在pom文件中添加,但是bootstrapt中没有添加,所以用户名为user,密码将会在控制台打印
```java
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
/**
* 注意:为了可以使用 http://${user}:${password}@${host}:${port}/eureka/
* 这种方式登录,所以必须是httpBasic,
* 如果是form方式,不能使用url格式登录
* 这里sessionManagement设置为不会创建HttpSession,但如果它已经存在,将可以使用HttpSession
*/
http.csrf().disable()
.authorizeRequests().anyRequest().authenticated()
.and().httpBasic()
.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
}
}
```
## 5.集群
Eureka使用集群配置时,只需要使用相互注册即可;
例如存在eureka1(192.168.0.1:8001)、eureka2(192.168.0.2:8001)、eureka3(192.168.0.3:8001):
```yaml
eureka:
instance:
hostname: 192.168.0.1 #应用实例主机名 192.168.0.1
client:
register-with-eureka: false #是否向注册中心注册自己
fetch-registry: false #是否从Eureka获取注册信息
service-url:
defaultZone: http://eureka:eureka@192.168.0.2:8001/eureka/,http://eureka:eureka@192.168.0.3:8001/eureka/
```
```yaml
eureka:
instance:
hostname: 192.168.0.2 #应用实例主机名 192.168.0.2
client:
register-with-eureka: false #是否向注册中心注册自己
fetch-registry: false #是否从Eureka获取注册信息
service-url:
defaultZone: http://eureka:eureka@192.168.0.1:8001/eureka/,http://eureka:eureka@192.168.0.3:8001/eureka/
```
```yaml
eureka:
instance:
hostname: 192.168.0.3 #应用实例主机名 192.168.0.3
client:
register-with-eureka: false #是否向注册中心注册自己
fetch-registry: false #是否从Eureka获取注册信息
service-url:
defaultZone: http://eureka:eureka@192.168.0.1:8001/eureka/,http://eureka:eureka@192.168.0.2:8001/eureka/
```
## 6.CAP理论
CAP理论指出,一个分布式系统不可能同时满足C(一致性)、A(可用性)和P(分区容错性)。由于分区容错性P在是分布式系统中必须要保证的,因此我们只能在A和C之间进行权衡。
### Zookeeper,consul保证CP
Zookeeper 为主从结构,有leader节点和follow节点。当leader节点down掉之后,剩余节点会重新进行选举。选举过程中会导致服务不可用,丢掉了可用行。
### Eureka保证AP
Eureka各个节点都是平等的,几个节点挂掉不会影响正常节点的工作,剩余的节点依然可以提供注册和查询服务。而Eureka的客户端在向某个Eureka注册或时如果发现连接失败,则会自动切换至其它节点,只要有一台Eureka还在,就能保证注册服务可用(保证可用性),只不过查到的信息可能不是最新的(不保证强一致性)。
## 7.集成配置中心
添加依赖
```xml
org.springframework.cloud
spring-cloud-config-server
${spring-cloud.version}
```
config-server提供了控制层,有两个
- `org.springframework.cloud.config.server.resource.ResourceController`
- `org.springframework.cloud.config.server.environment.EnvironmentController`
都可以获取配置属性
### 数据库加载配置
```yaml
spring:
profiles:
active: jdbc
cloud:
config:
server:
jdbc:
enabled: true
default-label: master
sql-without-profile: SELECT `key` , `value` from properties where APPLICATION=? and PROFILE is null and LABEL=?
sql: SELECT `key`, `value` from properties where APPLICATION=? and PROFILE=? and LABEL=?
prefix: config-api
```
其中spring.profiles.active有多种匹配,包含jdbc、redis、vault等等
具体配置代码位于 `org.springframework.cloud.config.server.config.EnvironmentRepositoryConfiguration`
添加依赖
```xml
org.springframework.boot
spring-boot-starter-jdbc
com.mysql
mysql-connector-j
8.2.0
```
启动类添加`@EnableConfigServer`注解
```java
@SpringBootApplication
@EnableEurekaServer
@EnableConfigServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
```
### 前端地址
https://gitee.com/shareprogram/shareprog-eureka-ui.git