spring-cloud学习笔记基于spring-boot2.0.3--Eureka注册中心(一)

24 篇文章 0 订阅
10 篇文章 0 订阅

我这里发布的项目版本都是基于spring-boot2.0+的

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

spring-cloud的版本为Finchley.RELEASE版本,后续所有的内容都是基于该版本进行的。

       首先我们先建立一个父项目,主要的目的是为了导入公共的jar包。我这里新建一个maven项目,当然想偷懒的同学,可以直接使用ide自带的工具快速发布spring-boot项目,这个看个人习惯。

父项目的的名称为vts_sc,POM文件的内容如下:

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
	</properties>

	<dependencies>
	 	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<!-- druid数据库连接池 -->  
		<dependency>  
	       <groupId>com.alibaba</groupId>  
	       <artifactId>druid</artifactId>  
	       <version>1.1.8</version>  
		</dependency> 
		<!-- MySql数据库驱动 -->  
		<dependency>  
	       <groupId>mysql</groupId>  
	       <artifactId>mysql-connector-java</artifactId>  
		</dependency> 
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.47</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
		<dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib</artifactId>
			<classifier>jdk15</classifier><!--指定jdk版本 -->
			<version>2.4</version>
		</dependency>
			<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
		<dependency>
		    <groupId>org.apache.commons</groupId>
		    <artifactId>commons-lang3</artifactId>
		</dependency>
		<!-- https://mvnrepository.com/artifact/commons-configuration/commons-configuration -->
		<dependency>
		    <groupId>commons-configuration</groupId>
		    <artifactId>commons-configuration</artifactId>
		    <version>1.10</version>
		</dependency>
		<dependency>
		    <groupId>com.vipcode</groupId>
		    <artifactId>until</artifactId>
		    <version>2.0</version>
		</dependency>
		<!-- 配置中心的jar包 -->
		<dependency>
	        <groupId>com.ctrip.framework.apollo</groupId>
	        <artifactId>apollo-client</artifactId>
	        <version>1.0.0</version>
	    </dependency>
	</dependencies>

      <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

新建一个maven-module(或者说新建一个spring-boot项目),vts_sc_eureka

导入jar包内容

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

     新建启动类,Application。注意:因为spring-boot在扫描的时候只扫描Application所在类的下面的子包内容,所以建议将Application构建的包为二级到三级包

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

}

注意这里需要增加注解:@EnableEurekaServer注解启动一个服务注册中心提供给其他应用进行对话

      在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,只需要在 application.yml 配置文件中增加如下信息:

spring:
  application:
    name: eureka-server
server:
  port: 9001
  
eureka:
  instance:
    hostname: localhost
    # 使用IP注册
    preferIpAddress: true
    # 心跳间隔
    lease-renewal-interval-in-seconds: 3
    # 服务失效时间: 如果多久没有收到请求,则可以删除服务
    lease-expiration-duration-in-seconds: 7
  client:
    # 关闭eureka client
    # enabled: false
    # 注册自身到eureka服务器
    registerWithEureka: true
    # 表示是否从eureka服务器获取注册信息
    fetchRegistry: false
    # 客户端从Eureka Server集群里更新Eureka Server信息的频率
    eureka-service-url-poll-interval-seconds: 60
    # 定义从注册中心获取注册服务的信息
    registry-fetch-interval-seconds: 5
    # 设置eureka服务器所在的地址,查询服务和注册服务都需要依赖这个地址
    service-url:
      defaultZone: http://${eureka.instance.hostname}:9001/eureka/
  server:
     # renewal-percent-threshold: 0.1
     # 关闭自我保护模式
     enable-self-preservation: false
     # Eureka Server 自我保护系数,当enable-self-preservation=true时,启作用
     # renewal-percent-threshold:
     # 设置清理间隔,单位为毫秒,默认为0
     eviction-interval-timer-in-ms: 3000
     # 设置如果Eureka Server启动时无法从临近Eureka Server节点获取注册信息,它多久不对外提供注册服务
     wait-time-in-ms-when-sync-empty: 6000000
     # 集群之间相互更新节点信息的时间频率
     peer-eureka-nodes-update-interval-ms: 60000

启动应用:访问localhost:9001会出现如下界面:

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值