spring cloud eurake server

简介: springcloud

1、概念:待补充

2、入门
2.1、启动eureka server
此处示例是maven-module搭建,第一段为maven项目的dependency,后面的为module-springcloud-server 的示例dependency

<parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>1.5.7.RELEASE</version>  
  </parent>  
  
  <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>

    <!-- Add typical dependencies for a web application -->  
    <dependencies>  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <scope>test</scope>
        </dependency>
    </dependencies>  
<dependencies>
      <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
      </dependency>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
        <version>1.3.5.RELEASE</version>
    </dependency>
    </dependencies> 
package org.demo.eureka.server;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class Application {
    public static void main( String[] args ){
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }
}

配置文件

spring.application.name=spring-cloud-server
server.port =1111
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.server.enable-self-preservation=false

eurake server启动
spring cloud eurake server启动的入口在类
org.springframework.cloud.netflix.eureka.server.EurekaServerInitializerConfiguration
代码如下

@Override
    public void start() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //TODO: is this class even needed now?
                    eurekaServerBootstrap.contextInitialized(EurekaServerInitializerConfiguration.this.servletContext);
                    log.info("Started Eureka Server");

                    publish(new EurekaRegistryAvailableEvent(getEurekaServerConfig()));
                    EurekaServerInitializerConfiguration.this.running = true;
                    publish(new EurekaServerStartedEvent(getEurekaServerConfig()));
                }
                catch (Exception ex) {
                    // Help!
                    log.error("Could not initialize Eureka servlet context", ex);
                }
            }
        }).start();
    }

启动完成:如下所示,eureka server启动http服务监听1111端口

···
2017-11-26 10:06:02.898  INFO 840 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
2017-11-26 10:06:02.898  INFO 840 --- [           main] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application spring-cloud-server with eureka with status UP
2017-11-26 10:06:02.916  INFO 840 --- [      Thread-11] o.s.c.n.e.server.EurekaServerBootstrap   : Setting the eureka configuration..
2017-11-26 10:06:02.916  INFO 840 --- [      Thread-11] o.s.c.n.e.server.EurekaServerBootstrap   : Eureka data center value eureka.datacenter is not set, defaulting to default
2017-11-26 10:06:02.916  INFO 840 --- [      Thread-11] o.s.c.n.e.server.EurekaServerBootstrap   : Eureka environment value eureka.environment is not set, defaulting to test
2017-11-26 10:06:02.920  INFO 840 --- [      Thread-11] o.s.c.n.e.server.EurekaServerBootstrap   : isAws returned false
2017-11-26 10:06:02.920  INFO 840 --- [      Thread-11] o.s.c.n.e.server.EurekaServerBootstrap   : Initialized server context
2017-11-26 10:06:02.921  INFO 840 --- [      Thread-11] c.n.e.r.PeerAwareInstanceRegistryImpl    : Got 1 instances from neighboring DS node
2017-11-26 10:06:02.921  INFO 840 --- [      Thread-11] c.n.e.r.PeerAwareInstanceRegistryImpl    : Renew threshold is: 1
2017-11-26 10:06:02.921  INFO 840 --- [      Thread-11] c.n.e.r.PeerAwareInstanceRegistryImpl    : Changing status to UP
2017-11-26 10:06:02.924  INFO 840 --- [      Thread-11] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2017-11-26 10:06:02.966  INFO 840 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 1111 (http)
2017-11-26 10:06:02.966  INFO 840 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 1111
2017-11-26 10:06:02.968  INFO 840 --- [           main] org.demo.eureka.server.Application       : Started Application in 3.856 seconds (JVM running for 4.325)

启动完成后,我们可以访问eureka server了
screenshot

2.2、注册服务
上面的例子演示了如何启动eureka server,那么server启动了,如何将我们正在运行的业务系统当做服务注册到eureka server中供其他的业务系统调用呢,下面演示hello world之spring cloud eureka service provider

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
      </dependency>

服务代码

package org.demo.eureka.provider.web;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloController {
    
    private final Logger logger = LoggerFactory.getLogger(getClass());
    
    @Resource
    private DiscoveryClient client;
    
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String index(HttpServletRequest request, HttpServletResponse response, @RequestParam String name) {
        try {
            ServiceInstance instance = client.getLocalServiceInstance();
            logger.info("hello, host:" + instance.getHost() + ", service_id:" + instance.getServiceId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "hello world, you are " + name;
        
    }

}

服务代码注册到eureka server

package org.demo.eureka.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class Application {
    public static void main( String[] args ){
        SpringApplication.run(Application.class, args);
    }
}

配置文件

server.port=1001
spring.application.name=hello-service
eureka.client.serviceUrl.defaultZone=http://192.168.54.240:1111/eureka/

再次访问eureka server,我们可以发现,服务已经注册到eureka server中了
screenshot

2.3、服务调用
eureka service已经注册到服务中心了,那么已经注册的服务应该如何使用呢,我们仍然需要通过eureka server,明确都有那些服务可用
下面将演示hello world之eureka service consumer

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
      </dependency>
      <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
      </dependency>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-ribbon</artifactId>
    </dependency>

springcloud eureka为c-s模式,分为client、server角色,其中client又可以细分为服务提供者、服务消费者。
服务提供者:
服务注册
服务续租
服务下线

服务消费者:
获取服务
服务调用

服务中心:
失效剔除
自我保护
服务状态同步

PeerAwareInstanceRegistryImpl

private void replicateInstanceActionsToPeers(Action action, String appName,
                                                 String id, InstanceInfo info, InstanceStatus newStatus,
                                                 PeerEurekaNode node) {
        try {
            InstanceInfo infoFromRegistry = null;
            CurrentRequestVersion.set(Version.V2);
            switch (action) {
                case Cancel:
                    node.cancel(appName, id);
                    break;
                case Heartbeat:
                    InstanceStatus overriddenStatus = overriddenInstanceStatusMap.get(id);
                    infoFromRegistry = getInstanceByAppAndId(appName, id, false);
                    node.heartbeat(appName, id, infoFromRegistry, overriddenStatus, false);
                    break;
                case Register:
                    node.register(info);
                    break;
                case StatusUpdate:
                    infoFromRegistry = getInstanceByAppAndId(appName, id, false);
                    node.statusUpdate(appName, id, newStatus, infoFromRegistry);
                    break;
                case DeleteStatusOverride:
                    infoFromRegistry = getInstanceByAppAndId(appName, id, false);
                    node.deleteStatusOverride(appName, id, infoFromRegistry);
                    break;
            }
        } catch (Throwable t) {
            logger.error("Cannot replicate information to {} for action {}", node.getServiceUrl(), action.name(), t);
        }
    }
相关文章
|
1月前
|
监控 负载均衡 Java
深入理解Spring Cloud中的服务网关
深入理解Spring Cloud中的服务网关
|
1月前
|
Java 开发工具 git
实现基于Spring Cloud的配置中心
实现基于Spring Cloud的配置中心
|
1月前
|
设计模式 监控 Java
解析Spring Cloud中的断路器模式原理
解析Spring Cloud中的断路器模式原理
|
23天前
|
负载均衡 Java Spring
Spring cloud gateway 如何在路由时进行负载均衡
Spring cloud gateway 如何在路由时进行负载均衡
150 15
|
29天前
|
资源调度 Java 调度
Spring Cloud Alibaba 集成分布式定时任务调度功能
Spring Cloud Alibaba 发布了 Scheduling 任务调度模块 [#3732]提供了一套开源、轻量级、高可用的定时任务解决方案,帮助您快速开发微服务体系下的分布式定时任务。
14232 20
|
23天前
|
Java Spring
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
42 3
|
1月前
|
消息中间件 Java 开发者
Spring Cloud微服务框架:构建高可用、分布式系统的现代架构
Spring Cloud是一个开源的微服务框架,旨在帮助开发者快速构建在分布式系统环境中运行的服务。它提供了一系列工具,用于在分布式系统中配置、服务发现、断路器、智能路由、微代理、控制总线、一次性令牌、全局锁、领导选举、分布式会话、集群状态等领域的支持。
119 5
|
1月前
|
Java API 开发工具
Spring Boot与Spring Cloud Config的集成
Spring Boot与Spring Cloud Config的集成
|
1月前
|
存储 安全 Java
实现基于Spring Cloud的分布式配置管理
实现基于Spring Cloud的分布式配置管理