spring-cloud 注册中心eureka环境搭建

来源:互联网 发布:java源码下载 编辑:程序博客网 时间:2024/05/22 00:54

开发工具idea

1、新建一个maven项目


2、在初学spring boot时,官方示例中,都是让我们继承一个spring的 spring-boot-starter-parent 这个parent

    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.2.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>
3、加入依赖文件

  <dependencies>    <!--eureka server -->    <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-starter-eureka-server</artifactId>    </dependency>        <!-- spring boot test-->    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-test</artifactId>        <scope>test</scope>    </dependency>  </dependencies>

4、加入版本管理器:

     1:如果dependencies里的dependency自己没有声明version元素,那么maven就

会倒dependencyManagement里面去找有没有对该artifactId和groupId进行过版本声明,如果有,就继承它,如果

没有就会报错,告诉你必须为dependency声明一个version

    2:如果dependencies中的dependency声明了version,那么无论dependencyManagement中有无对该jar的version声明,都以dependency里的version为准。

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

5、指定仓库

<repositories>    <repository>        <id>spring-milestones</id>        <name>Spring Milestones</name>        <url>http://repo.spring.io/milestone</url>        <snapshots>            <enabled>false</enabled>        </snapshots>    </repository></repositories>
6、构建

<build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build>
7、新建配置文件

bootstrap.yml  和application.yml  都可以用来配置参数

bootstrap.yml可以理解成系统级别的一些参数配置,这些参数一般是不会变动的

application.yml 可以用来定义应用级别的,如果搭配spring-cloud-config使用 application.yml里面定义的文件可以实现动态替换

server:  port: 8001eureka:  instance:    hostname: localhost  client:    registerWithEureka: false    fetchRegistry: false    serviceUrl:      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

registerWithEureka表示是否注册自身到eureka服务器,因为当前这个应用就是eureka服务器,没必要注册自身,所以这里是false,fetchRegistry表示是否从eureka服务器获取注册信息,同上,这里不需要。defaultZone就比较重要了,是设置eureka服务器所在的地址,查询服务和注册服务都需要依赖这个地址。

8、新建启动服务类

package com.jinzhu.server;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;/** * Created by rick on 2017/12/2. */@EnableEurekaServer@SpringBootApplicationpublic class Server {    public static void main(String []args){        SpringApplication.run(                Server.class,args        );    }}

直接启动





浏览器能访问成功则说明搭建成功


资源下载:http://download.csdn.net/download/zhangchangbin123/10141726