基于SpringCloud Eureka 搭建服务注册中心

新建一个springboot项目:eureka-server

添加Maven依赖信息 :

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>xxxx.com.cn</groupId>
    <artifactId>eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
<!-- 注册中心核心依赖 -->
        <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>
    </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>

application.yml配置:

spring:
  application:
    name: eureka
server:
  port: 8761
eureka:
  client:
    service-url:
      defaultzone: http://127.0.0.1:8762/eureka/
      #eureka必须实现高可用至少2个 2个eureka是互相注册 3个eureka则是两两互相注册
      #defaultzone: http://127.0.0.1:8761/eureka/ 即1注册到2 2注册到1
      #defaultzone: http://127.0.0.1:8761/eureka/,http://127.0.0.1:8762/eureka/
      #即 启动1时 将1注册到2和3 启动2时 将2注册到1和3  启动3时 将3注册到1和2
    register-with-eureka: false #本身就是一个注册中心所以不需要自己注册进eureka
  server:
    enable-self-preservation: false #eureka server 的自我保护 开发环境关闭 生产环境打开

在启动类中进行配置@EnableEurekaServer配置

@SpringBootApplication
@EnableEurekaServer //开启eurekaServer
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

 浏览器输入:http://127.0.0.1:8761/

 

posted @ 2019-07-10 23:25  骷髅党政委  阅读(1312)  评论(0编辑  收藏  举报