二、SpringCloud之Eureka服务注册与发现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_29479041/article/details/83651655

一、Eureka简介

1、基于Netflix Eureka做了二次封装

2、两个组件组成:

  • Eureka Server 注册中心
  • Eureka Client 服务注册

3、Eureka总结:

  • @EnableEurekaServer   @EnableEurekaClient
  • 心跳检测、健康检查、负载均衡等功能
  • Eureka的高可用,生产上建议至少两台以上

二、Eureka Server 注册中心

1、步骤

  • pom.xml
    <properties>
        <spring-cloud.version>Greenwich.M1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </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
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
    fetch-registry: false
  server:
    enable-self-preservation: false #关闭自我保护模式 开发环境关闭,生产环境不要这样设置
spring:
  application:
    name: eureka
server:
  port: 8761
  • EurekaApplication
@EnableEurekaServer  //启用注册中心的功能
@SpringBootApplication
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

2、测试(http://localhost:8761/

3、后台启动(这样就不用每次都去启动它)

  • 1.将euraka打成jar包 ,将jar包放进一个文件夹内 

  • 2.在终端写启动命令
Amy:~ Amy$ nohup java -jar /Users/Amy/Downloads/service/eureka-0.0.1-SNAPSHOT.jar > /dev/null 2>&1 &
[1] 5628
Amy:~ Amy$ ps -ef |grep eureka    3.http://localhost:8761/

#如果要停止进程
Amy:~ Amy$ kill -9 5628

三、Eureka Client 服务注册

1、步骤

  • pom.xml(版本要与Eureka Server保持一致)
    <properties>
        <spring-cloud.version>Greenwich.M1</spring-cloud.version>
    </properties>    
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </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
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    hostname: clientName #自定义连接
spring:
  application:
    name: client
  • ClientApplication
@EnableDiscoveryClient //开启发现服务功能
@SpringBootApplication
public class ClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }
}

2、测试

猜你喜欢

转载自blog.csdn.net/qq_29479041/article/details/83651655