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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 微信支付与支付宝钱包的竞争分析

    微信支付与支付宝钱包的竞争分析微信支付与支付宝钱包的竞争分析NO1:2013年8月,微信5.0上线,其中附加了一个功能叫做微信支付,当时的微信用户已经超过了4亿,活跃用户1.94亿,估计不少人在看微信支付同支付老大哥支付包的大战。说起微信支付和支付宝的大战,先来说说他们背景,微信支付是社交软件巨头腾讯公司旗下的微信中的附加功能,而支付宝是电商巨头阿里巴巴旗下的支付理财软件。两家都有超过2万的顶级互联网员工,兵强马壮…

  • 如何在win10上安装ubuntu虚拟机-图文详细教程「建议收藏」

    前言(和我一样的小白快来看看~)本文将在win10上安装ubuntu虚拟机的步骤一步步记录了下来~希望对大家有帮助方法大概是先装一个虚拟化软件(virtualbox),然后在这个软件上新增一台虚拟电脑机,这样我们就等于有了一台没有装系统的电脑。然后再将我们已经下载好的iso结尾的镜像文件(ubuntu)作为启动盘,给这个虚拟电脑装上系统,就完事了~安装包和镜像文件准备virtu…

  • 页面加载时给的子元素的第一个元素加class

    页面加载时给的子元素的第一个元素加class

  • linux crontab 每隔10秒执行一次[通俗易懂]

    linux crontab 每隔10秒执行一次[通俗易懂]linux下定时执行任务的方法在LINUX中你应该先输入crontab-e,然后就会有个vi编辑界面,再输入03**1/clearigame2内容到里面:wq保存退出。在LINUX中,周期执行的任务一般由cron这个守护进程来处理[ps-ef|grepcron]。cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间。cron的配置文件称为“…

    2022年10月21日
  • 如何解决 VirtualBox 在安装 VB Guest additions(安装增强功能)时遇到的问题[通俗易懂]

    如何解决 VirtualBox 在安装 VB Guest additions(安装增强功能)时遇到的问题[通俗易懂]问题1遇到:ValueError:Filecontextfor/opt/VBoxGuestAdditions-6.1.6/other/mount.vboxsfalreadydefine

  • 字符串的匹配算法_多字符串匹配

    字符串的匹配算法_多字符串匹配文章目录BF算法RK算法编辑器中的全局替换方法:BM算法坏字符好后缀规则代码实现KMP算法一说到字符串匹配算法,不知道会有多少小伙伴不由自主的想起那个kmp算法呢?想到是很正常的,谁让它那么优秀呢。BF算法不要被事物的表面现象所迷惑,这个算法全称:BruteForce,有个拉风的中文名:暴力匹配算法。能想明白了吧。如果模式串长度为m,主串长度为n,那在主串中,就会有n-m+1个长度为m的子串,我们只需要暴力地对比这n-m+1个子串与模式串,就可以找出主串与模式串匹配的子串。.

发表回复

您的电子邮箱地址不会被公开。

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