(01) SpringCloud: Eureka Server 搭建

SpringCloud的Eureka Server模块

1. Eureka Server: 注册中心, 提供服务的注册和发现;

2. Service Provider: 服务提供方, 将自身服务注册到Eureka Server;

3. Service Consumer: 服务消费方,从 Eureka Server 获取注册列表, 从而能够调用相关服务;

脑补一下画面,Eureka Server 就是那个居间桥梁,当然 Eureka在实践中要使用集群,实现多活。

样例步骤:

0. 新建一个SpringBoot项目;

1. pom.xml 引入SpringCloud 的 Eureka 组件依赖;


<!-- 引入SpringCloud依赖管理 -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>


<!-- 引入SpringCloud Eureka依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

 

2. 启动入口类增加 @EnableEurekaServer 注解

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

3. 配置文件如下:

修改hosts文件 /etc/hosts, 增加如下配置

127.0.0.1	eureka-server-01
127.0.0.1	eureka-server-02
127.0.0.1	eureka-server-03

单台: application-dev.properties

spring.application.name=xts-eureka-server
server.port=9001
##
## 开发环境 Eureka-Server为单实例: 单服务中心
## 是否将自己注册到EurekaServer上
eureka.client.register-with-eureka=false
## 是否从Eureka Server获取注册信息
eureka.client.fetch-registry=false
## 主机hostname
eureka.instance.hostname=eureka-server-01
## 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址
eureka.client.service-url.defaultZone=http://eureka-server-02:9001/eureka/

启动: 

java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev

DashBoard查看: http://eureka-server-01:9001/

 

多节点(3台):

application-test1.properties 

spring.application.name=xts-eureka-server
server.port=9001
eureka.instance.hostname=eureka-server-01
eureka.client.service-url.defaultZone=http://eureka-server-02:9002/eureka/,http://eureka-server-03:9003/eureka/

application-test2.properties 

spring.application.name=xts-eureka-server
server.port=9002
eureka.instance.hostname=eureka-server-02
eureka.client.service-url.defaultZone=http://eureka-server-01:9001/eureka/,http://eureka-server-03:9003/eureka/

application-test3.properties

spring.application.name=xts-eureka-server
server.port=9003
eureka.instance.hostname=eureka-server-03
eureka.client.service-url.defaultZone=http://eureka-server-01:9001/eureka/,http://eureka-server-02:9002/eureka/

 

4. 启动

打好jar包,然后分别执行:

java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=test1

java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=test2

java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=test3

 

5.  DashBoard 查看地址

http://eureka-server-01:9001/

http://eureka-server-02:9002/

http://eureka-server-03:9003/

b5c91ec5cfbab535a5ade232ad76f3c4215.jpg

 

转载于:https://my.oschina.net/jrrx/blog/1840090

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值