Spring Cloud Eureka-服务注册

一、注册中心

1、新建maven项目eureka-server,引入对应jar包:

<parent>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-parent</artifactId>
    <version>Brixton.SR4</version>
    <relativePath/>
 </parent>

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

2、EnableEurekaServer注册Application

package com.tan.self;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * Author: Mr.tan
 * Date: 2017/9/9
 */
@EnableEurekaServer   // 开启服务注册服务
@SpringBootApplication
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

3、application.properties配置

server.port= 1111

eureka.instance.hostname= localhost
# 不需要向注册中心注册自己
eureka.client.register-with-eureka=false
# 不需要去检索服务
eureka.client.fetch-registry=false
#对外地址
eureka.client.serviceUrl.defaultZone= http://${eureka.instance.hostname}:${server.port}/eureka

4、启动EurekaServerApplication

访问http://localhost:1111/ 可以看到如下服务注册页面

这里写图片描述

说明服务注册中心搭建完成


二、高可用注册中心

生产环境中为避免单机故障,我们需要集群和高可用部署。Eureka Server 的高可用实际上就是将自己作为服务想其它服务注册中心注册自己,这样就形成了一组互相注册的服务中心,以实现服务清单 的互相同步,达到高可用的效果。

–接着上面搭建的eureka-server改造–

1、两个注册中心配置文件

application-peer1.properties : peer1中心,将serviceUrl 指向 peer2

spring.application.name=eureka-server
server.port=1111
eureka.instance.hostname=peer1
eureka.client.serviceUrl.defaultZone=http://peer2:1112/eureka/

application-peer2.properties : peer2中心,将serviceUrl 指向 peer1

spring.application.name=eureka-server
server.port=1112
eureka.instance.hostname=peer2

2、启动

本地HOST添加配置,让peer1和peer2能够被访问到(推荐SwitchHosts工具,一键切换hosts,作者官网:http://oldj.net/article/switchhosts/

127.0.0.1 peer1  
127.0.0.1 peer2

通过JAVA命令启动两个注册中心

java -jar eureka-server-1.0.jar --spring.profiles.active=peer1
java -jar eureka-server-1.0.jar --spring.profiles.active=peer2

3、查看效果

通过访问http://peer1:1111/http://peer2:1112/查看配置成功:

这里写图片描述

这里写图片描述


*完整项目代码在:*

https://github.com/hanmo9/spring-cloud-learn

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值