设为首页 加入收藏

TOP

服务治理: Spring Cloud Eureka(二)
2019-09-03 01:23:53 】 浏览:61
Tags:服务 治理 Spring Cloud Eureka
upId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
  • 在SpringBoot启动类,也就是@SpringBootApplication修饰的主方法中加入如下注解@EnableEurekaServer
    
        @EnableEurekaServer
    @SpringBootApplication
    public class EurekaServerApplication {

        public static void main(String[] args) {
            SpringApplication.run(EurekaServerApplication.class, args);
        }

    }

加入这个注解也就标识着这是一个Eureka的服务端,可以启动服务了,但是启动服务会报错,因为你没有添加注册中心的相关配置。

  • application.properties文件中加入如下内容
server.port=8000

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

server.port 就代表着注册中心的端口号

eureka.client.service-url.defaultZone :eureka客户端默认服务url

eureka.client.register-with-eureka : 表示注册中心是否向其他注册中心注册自己,单节点注册中心不需要,设置为false

eureka.client.fetch-registry: 表示注册中心是否主动去检索服务,并不需要检索服务,设置为false

其他配置:

# 项目contextPath,一般在正式发布版本中,我们不配置
# 避免加上更目录:Cannot execute request on any known server
# 加上根目录也需要在注册地址上加入根
 server.context-path=/eureka81
# 错误页,指定发生错误时,跳转的URL。请查看BasicErrorController源码便知
 server.error.path=/error
# 通过spring.application.name属性,我们可以指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问。
 spring.application.name=eureka-server
# eureka是默认使用hostname进行注册,可通过一下项自动获取注册服务IP或者直接通过eureka.instance.ip-address指定IP
# eureka.instance.prefer-ip-address=true
# SpringBoot 在启动的时候会读配置文件,会把prefer-ip-address 默认转换为preferIpAddress驼峰命名
eureka.instance.preferIpAddress=true

# 设为false,关闭自我保护
eureka.server.enable-self-preservation=false
# 清理间隔(单位毫秒,默认是60*1000
eureka.server.eviction-interval-timer-in-ms=6000

# 开启健康检查(需要spring-boot-starter-actuator依赖)
eureka.client.healthcheck.enabled=false
# 续约更新时间间隔(默认30秒)
eureka.instance.lease-renewal-interval-in-seconds=10    
# 续约到期时间(默认90秒)
eureka.instance.lease-expiration-duration-in-seconds=30

没有加入 eureka.instance.preferIpAddress=true 之前,默认本地为注册中心

加入 eureka.instance.preferIpAddress=true 之后,圈出来的ip即为eureka.client.service-url.defaultZone指定的 ip。

在完成了上述配置之后,应用程序启动并访问http://localhost:1111/ 可以看到如下图所示的信息版,其中Instances curently registered with Eureka 是空的,表明还没有任何服务提供者提供服务。

注册服务提供者

在完成了上述搭建之后,接下来我们尝试将一个既有的SpringBoot应用加入Eureka服务治理体系去。

使用上一小节的快速入门工程进行改造,将其作为一个微服务应用向服务注册中心发布注册自己

pom.xml配置如下:

        <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframewo
首页 上一页 1 2 3 4 5 6 下一页 尾页 2/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇if语句的嵌套使用之获取三个数据.. 下一篇笔记-迎难而上之Java基础进阶7

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目