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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 集成学习-Voting

    集成学习-Voting一、什么是集成学习集成学习是使用一系列学习器进行学习,并使用某种规则把各个学习结果进行整合从而获得比单个学习器更好的学习效果的一种机器学习方法。一般情况下,集成学习中的多个学习器都是同质的”弱学习器”。上面的描述来自百度百科,看定义的话知道是基于‘弱学习器’的,很多讲集成学习的教程都会先讲决策树,然后讲到随机森林和GBDT,也就是bagging和boosting,…

    2025年6月19日
    2
  • goland激活码最新-激活码分享

    (goland激活码最新)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlTR0LFTT656-eyJsa…

    2022年3月22日
    57
  • Marsaglia XORshift随机数算法「建议收藏」

    Marsaglia XORshift随机数算法「建议收藏」引理:二进制矩阵相乘中加法为异或。给定非空向量beta和n阶矩阵T,序列beta*T,beta*T^2,beta*T^3,…的秩为2^n-1的充要条件是矩阵T为非奇异矩阵.L是左移位操作,y=y^(y>b)表示为y=y*(E+R^b),令T=(E+L^a)(E+R^b),n=32或64,找不到这样的非奇异矩阵。但是令T=(E+L^a)*(E+R^b)*(E+L^c)能找

    2022年7月26日
    9
  • 《大白AI周报》精华内容整理汇总「建议收藏」

    《大白AI周报》精华内容整理汇总「建议收藏」在人工智能学习中,大家或多或少都会关注一些公众号,但随着每天信息量的暴增,碎片化的内容让大家应接不暇。如何挖掘有价值的内容,如何快速查阅自己需要的内容,是一个头疼的问题。因此大白每周都会将人工智能领域,几十个公众号每周发布的精华内容汇总起来。同时进行系统的分类,让大家对于人工智能行业每周的技术动态**可以一目了然,希望对大家有帮助。《大白AI周报》每周精华文章链接:每周日报的内容还是有点多,大白将其中的更加系统性,或者对项目来说,更有针对性的文章,整理到本文中,方便大家更好的查看。整理汇总:江大白

    2022年8月31日
    6
  • 简易网页音乐播放器

    简易网页音乐播放器简易网页音乐播放器开发工具与关键技术:DW2021jQueryHTML5撰写时间:2021年5月28日简介与要点:在网页上制作一个音乐播放器我们仅需用到一个新的标签<audiosrc=”素材音乐”controls></audio>;因为我们这个音乐播放不是单曲循环的使用还要…

    2022年6月25日
    30
  • 质量工具因果图_质量管理因果图例题

    质量工具因果图_质量管理因果图例题前言在项目中,我们经常需要用到不同的工具对项目质量进行评审。使用不同的质量工具可能得到的结果不太一样。下面简单说下项目中常用到的质量分析工具因果图。释义:什么是因果图因果图又称为石川图、Ishikawa或鱼骨图,它把影响质量诸因素之间的关系以树状图的方式表示出来,使人一目了然,便于分析原因并采取相应的措施。它是一种在问题发生后,寻找根本原因的一种方法。它取名石川图是因为它是由日

    2022年8月14日
    8

发表回复

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

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