springcloud-02-eureka

Eureka(注册中心)

Eureka原理图


Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务注册和发现(请对比Zookeeper(对比在springcloud-01有))。

Eureka 采用了 C-S 的设计架构。Eureka Server 作为服务注册功能的服务器,它是服务注册中心。

Eureka包含两个组件:Eureka Server 和Eureka Client
Eureka Server提供服务注册服务各节点启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用的服务节点的信息,服务节点的信息可以在界面中直观的看到。
EurekaClient是一个java客户端,用于简化EurekaServer的交互,客户端同时也具备一个内置的,使用轮询(round-robin)负载算法的负载均衡器,在应用启动后,将会向EurekaServer发送心跳(默认是30秒)。如果EurekaServer在多个心跳周期内没有接收到某个节点的心跳,EurekaServer将会从服务注册表中把这个服务节点删除(默认90秒)。

Eureka的自我保护机制

在默认配置中,Eureka Server在默认90s没有得到客户端的心跳,则注销该实例,但是往往因为微服务跨进程调用,网络通信往往会面临着各种问题,比如微服务状态正常,但是因为网络分区故障时,Eureka Server注销服务实例则会让大部分微服务不可用,这很危险,因为服务明明没有问题。

 为了解决这个问题,Eureka 有自我保护机制,通过在Eureka Server配置如下参数,可启动保护机制

 eureka.server.enable-self-preservation=true

1它的原理是,当Eureka Server节点在短时间内丢失过多的客户端时(可能发送了网络故障),那么这个节点将进入自我保护模式,不再注销任何微服务,当网络故障回复后,该节点会自动退出自我保护模式。

 自我保护模式的架构哲学是宁可放过一个,决不可错杀一千。

Eureka高可用集群

 理论上来讲,因为服务消费者本地缓存了服务提供者的地址,即使Eureka Server宕机,也不会影响服务之间的调用,但是一旦新服务上线,已经在缓存在本地的服务提供者不可用了,服务消费者也无法知道,所以保证Eureka Server的高可用还是很有必要的。

在分布式系统中,任何的地方存在单点,整个体系就不是高可用的,Eureka 也一样,在上面的架构图中Eureka Server不是以单点存在的,而是以集群的方式对外提供服务。

下面是以下代码的实现:

注册中心的yml:

server:
  port: 7001
  
eureka:
  instance:
    hostname: eureka7001.com #localhost #eureka的服务名称
  client:
    fetch-registry: false #false表示我的职责是维护服务实例,而不是去检索服务
    register-with-eureka: false #false表示不会注册自己
    service-url:   #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
      #单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka

     defaultZone: http://eureka7003.com:7003/eureka,http://eureka7002.com:7002/eureka 这个是集群的配置


注册中心的pom:spring-cloud-starter-eureka-server 这个是eureka的服务端

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>
spring-cloud-starter-eureka-server
</artifactId>
</dependency>
</dependencies>
<!--spring-cloud-starter-netflix-eureka-server  -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

注册中心的启动类:

@SpringBootApplication
@EnableEurekaServer //一定要加上这个注解,表明这个服务时注册中心
public class Eureka7001_App {

public static void main(String[] args) {
SpringApplication.run(Eureka7001_App.class, args);
}


}

其他服务怎么注册到eureka中心,下面用代码:

yml:

spring:
  application:
    name: microservicecloud1-dept   #这个是服务的名称①(下面有一个①号对应)
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/test                   # 数据库名称
    dbcp2:
      initial-size: 5                                       # 初始化连接数
      min-idle: 5                                           # 数据库连接池的最小维持连接数
      max-total: 5                                          # 最大连接数
      max-wait-millis: 200                                  # 等待连接获取的最大超时时间


eureka: 
  client: #吧服务注册到eureka上面
    service-url:
      #单机 defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka7001.com:7001/eureka # 这是表示只是用在一个注册中心
     # defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka #这个是注册中心的集群,他们只要有一个存在,就能工作
  instance:
    instance-id: microservicecloud1-dept9001 #自定义服务名称信息 (这是地方有一个②号对应)
    prefer-ip-address: true  
#访问路径可以显示ip 这个设置成true后,当鼠标悬浮在服务名称信息上的时候会显示ip、端口号等,如 http://192.168.121.1/info
三号图(③号)
    
info:  (④四号)
  app.name: jrj-microservicecloud
  company.name: www.jrj.com.cn
  build.artifactId: $project.artifactId$

  build.version: $project.version$

------------------------------------

下面的不是yml里面的

#要使info有效:需要加pom依赖

<!-- actuator监控信息完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

下面的是在读info的时候:会动态的读值,$开始$结束

<build>
<finalName>microservicecloud1</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>
<delimit>$</delimit>
</delimiters>
</configuration>
</plugin>
</plugins>

</build>

---------------------------

下面的序号和上面的序号是对应的

①对应的就是服务名称,

②对应的就是服务信息,

③只有当prefer-ip-address: true 才会显示出ip、端口等,

④、就是点击②后会打开的一个连接,显示的是info配置的内容。



pom依赖:

<!-- 将微服务provider侧注册进eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>

</dependency>

<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>

</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>

启动类:

@EnableEurekaClient 这个注解就是eureka客户端需要的,
@SpringBootApplication
//@EnableDiscoveryClient //发现服务,这个注解可以这样使用,发现服务
public class DeptProvider9001_App {

public static void main(String[] args) {
SpringApplication.run(DeptProvider9001_App.class, args);
}

}

@EnableEurekaClient 和@EnableDiscoveryClient,前者包含后者

@Autowired

private DiscoveryClient client;


@RequestMapping(value = "/dept/discovery", method = RequestMethod.GET)
public Object discovery() {
List<String> list = client.getServices();
System.out.println("**********" + list);
List<ServiceInstance> srvList = client.getInstances("MICROSERVICECLOUD-DEPT");
for (ServiceInstance element : srvList) {
System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"
+ element.getUri());
}
return this.client;
}

安全性:

引入springboot的security的依赖
   <dependency>
        <groupId>
org.springframework.boot</groupId>
        <artifactId>
spring-boot-starter-security</artifactId>

    </dependency>

yml:

security:
  basic:
    enabled: true
  user:
    name: eureka
    password: eureka


参考:http://www.ccblog.cn/94.htm





猜你喜欢

转载自blog.csdn.net/wojiao228925661/article/details/80860165