美文网首页
StringCloud之快速上手Eureka

StringCloud之快速上手Eureka

作者: fanbuer | 来源:发表于2019-04-15 10:24 被阅读0次

    Eureka服务端开发

    1.创建eureka模块
    2.引入依赖 父工程pom.xml定义SpringCloud版本

    <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Finchley.M9</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
    </dependencyManagement>
    

    tensquare_eureka模块pom.xml引入eureka-server

    <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
    </dependencies>
    

    3.添加application.yml

    server:
        port: 6868 #服务端口 
    eureka:
        client:
            registerWithEureka: false #是否将自己注册到Eureka服务中,本身就是所有无需 注册 
            fetchRegistry: false #是否从Eureka中获取注册信息 
            serviceUrl: #Eureka客户端与Eureka服务端进行交互的地址 
                defaultZone: http://127.0.0.1:${server.port}/eureka/
    

    4.编写启动类

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

    5.测试
    启动运行启动类,然后在浏览器地址栏输入 http://localhost:6868/ 运行效果如 下:

    image.png
    主界面中
    system status为系统信息
    General Info为一般信息
    Instances currently registered with Eureka为注册到的所有微服务列表

    服务注册

    1.将其他微服务模块添加依赖

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

    2.修改每个微服务的application.yml,添加注册eureka服务的配置

    eureka:
        client:
            service‐url:
                defaultZone: http://localhost:6868/eureka 
        instance:
            prefer‐ip‐address: true
    

    3.修改每个服务类的启动类,添加注解

    @SpringBootApplication
    @EnableEurekaClient//必须的
    public class UserApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(UserApplication.class, args);
        }
    }
    

    4.启动测试

    • 将每个微服务启动起来,会发现eureka的注册列表中可以看到这些微服务了

    保护模式

    • 如果在Eureka Server的首页看到以下这段提示,则说明Eureka已经进入了保护模式:


      image.png
    • Eureka Server在运行期间,会统计心跳失败的比例在15分钟之内是否低于85%,如果出 现低于的情况(在单机调试的时候很容易满足,实际在生产环境上通常是由于网络不稳定导致),Eureka Server会将当前的实例注册信息保护起来,同时提示这个警告。保护 模式主要用于一组客户端和Eureka Server之间存在网络分区场景下的保护。一旦进入保 护模式,Eureka Server将会尝试保护其服务注册表中的信息,不再删除服务注册表中的 数据(也就是不会注销任何微服务)。

    相关文章

      网友评论

          本文标题:StringCloud之快速上手Eureka

          本文链接:https://www.haomeiwen.com/subject/alpywqtx.html