spring cloud eurake server「建议收藏」

spring cloud eurake server「建议收藏」1、概念:待补充2、入门2.1、启动eurekaserver此处示例是maven-module搭建,第一段为maven项目的dependency,后面的为module-springcloud-server的示例dependency<parent><groupId>org.springframewo…

大家好,又见面了,我是你们的朋友全栈君。

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);
        }
    }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/135136.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 关于G1收集器

    关于G1收集器G1(GarbageFirst)收集器是Oracle公司开发的一款主要面向服务端的拥有相对可靠的停顿预测模型的垃圾收集器。在垃圾收集器的历史上有着里程碑式的意义。与之前的收集器不同,G1不在基于固定的新生代与老年代的内存分配方式进行垃圾清理,而是使用了基于Region的内存分配的方式进行垃圾清理。这种方式使得G1在进行垃圾清理的时候不需要对整个新生代或老年代甚至整个Java堆进行垃圾清理,这样就极大的减少标记期间的停顿时间。设计思路:面向局部(单个或多个Region)收集内存布局:基于Regi

    2022年5月20日
    33
  • 一步到位分布式开发Zookeeper实现集群管理

    一步到位分布式开发Zookeeper实现集群管理

    2022年3月12日
    55
  • Linux LAMP架构介绍及配置「建议收藏」

    Linux LAMP架构介绍及配置「建议收藏」LinuxLAMP架构介绍及配置一、LAMPLAMP平台概述LAMP架构是目前成熟的企业网站应用模式之一,指的是协同工作的一整台系统和相关软件,能够提供动态web站点服务及其应用开发环境LAMP是一个缩写词,具体包括Linux操作系统,Apache网站服务器,MySQL数据库服务器,PHP(或perl,Python)网页编程语言LAMP各组件主要作用(平台)Linux:作为LAMP架构的基础,提供用于支撑Web站点的操作系统,能够与其他三个组件提供更好的稳定性,兼容性(AMP组件也

    2022年10月16日
    6
  • vue开发移动端app苹果手机的bug脱坑

    vue开发移动端app苹果手机的bug脱坑vue 开发移动端 app 网页打包苹果 app 的坑总结列表设置 overflow auto 后 滚动效果不流畅的问题 可以在列表设置 flex 1 overflow y auto webkit overflow scrolling touch 在 iOS 中出现滚动卡顿问题上诉解决方法还会出现一个问题 就是会导致在列表中如果有弹窗 position fixed 会导致弹窗被覆盖或者覆盖不完全的问题 为此需要将弹窗放在列表外 下面为代码例子

    2025年10月30日
    4
  • Linux 配置Maven环境

    Linux 配置Maven环境Linux配置Maven环境

    2022年5月14日
    34
  • flask中jsonify和json区别[通俗易懂]

    flask中jsonify和json区别[通俗易懂]JSON数据结构要把json与字典区分开来dumps(字典转换成Json)loads(Json转换成字典)参考:Python的字典是一种数据结构,JSON是一种数据格式。json就是一个根据某种约定格式编写的纯字符串,不具备任何数据结构的特征。而python的字典的字符串表现形式的规则看上去和json类似,但是字典本身是一个完整的数据结构,实现了一切自身该有的算法。Python的字典key可以是任意可hash对象,json只能是字符串。形式上有些相像,但JSO

    2022年5月23日
    51

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号