springCloud注册中心Eureka

springCloud注册中心Eureka

1 注册中心配置

1.1pom 文件

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.duran</groupId>
    <artifactId>eureka1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka1</name>
    <description>eureka1</description>

    <properties>
        <java.version>1.8</java.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-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR3</version><!-- eureka版本 -->
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

1.2yml文件

server:
  port: 8081

eureka:
  instance:
    hostname: localhost
    lease-renewal-interval-in-seconds: 10 #客户端向服务器(注册中心发送心跳的时间间隔)
    lease-expiration-duration-in-seconds: 120 #服务器(注册中心)租期到期的时间, 也就是说服务器在收到最后一次心跳的时间上线
  client:
     registerWithEureka: false   #自己不注册到注册中心上
     fetchRegistry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

1.3启动类

@SpringBootApplication
@EnableEurekaServer
public class Eureka1Application {

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

}

2服务端配置

2.1pom文件

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.duran</groupId>
    <artifactId>client1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>client1</name>
    <description>client1</description>

    <properties>
        <java.version>1.8</java.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-eureka</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR3</version><!-- eureka版本 -->
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

2.2yml文件

spring:
  application:
    name: consumer-service

server:
  context-path: /consumer
  port: 8083

eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      defaultZone: http://127.0.0.1:8081/eureka/

2.3启动类

@EnableEurekaClient
@SpringBootApplication
public class ClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }

}

双注册中心 高可用

注册中心1 yml配置

server:
  port: 8081

eureka:
  instance:
    hostname: localhost
    lease-renewal-interval-in-seconds: 10 #客户端向服务器(注册中心发送心跳的时间间隔)
    lease-expiration-duration-in-seconds: 120 #服务器(注册中心)租期到期的时间, 也就是说服务器在收到最后一次心跳的时间上线
  client:
    service-url:
      defaultZone: http://127.0.0.1:8082/eureka/  #互相注册到对方--高可用服务发现

注册中心2 yml配置

server:
  port: 8082

eureka:
  instance:
    hostname: localhost
    lease-renewal-interval-in-seconds: 10 #客户端向服务器(注册中心发送心跳的时间间隔)
    lease-expiration-duration-in-seconds: 120 #服务器(注册中心)租期到期的时间, 也就是说服务器在收到最后一次心跳的时间上线
  client:
    service-url:
      defaultZone: http://127.0.0.1:8081/eureka/  #互相注册到对方--高可用服务发现

服务端yml配置文件

spring:
  application:
    name: consumer-service
server:
  context-path: /consumer
  port: 8083

eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      defaultZone: http://localhost:8081/eureka/,http://localhost:8082/eureka/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值