1.2.服务注册中心的搭建(Eureka,Consul)

一. Eureka注册中心搭建

  1. 官方文档:http://cloud.spring.io/spring-cloud-netflix/single/spring-cloud-netflix.html#spring-cloud-eureka-server

  2. 步骤:

    1. 第一步:创建项目

    2. 第二步:添加注解 @EnableEurekaServer

    3. 第三步:增加配置application.yml

    4. 第四步:访问注册中心页面

  3. 另:maven镜像仓库地址: https://www.cnblogs.com/sword-successful/p/6408281.html

 

 

项目中的pom文件:

<parent>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-parent</artifactId>

    <version>1.5.4.RELEASE</version>

    <relativePath/>

</parent>

<dependencies>

    <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-eureka-server</artifactId>

    </dependency>

</dependencies>

<dependencyManagement>

    <dependencies>

        <dependency>

           <groupId>org.springframework.cloud</groupId>

           <artifactId>spring-cloud-dependencies</artifactId>

           <version>Dalston.SR1</version>

           <type>pom</type>

           <scope>import</scope>

        </dependency>

    </dependencies>

</dependencyManagement>

 

 

Application启动类:

@EnableEurekaServer

@SpringBootApplication

public class Application {

    public static void main(String[] args) {

        new SpringApplicationBuilder(Application.class)

                    .web(true).run(args);

    }

}

 

application.yml:

server:

  port: 8761

 

eureka:

  instance:

    hostname: localhost

  client:

    #声明自己是个服务端

    registerWithEureka: false

    fetchRegistry: false

    serviceUrl:

      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

 

application.properties

spring.application.name=eureka-server

server.port=1001

 

eureka.instance.hostname=localhost

eureka.client.register-with-eureka=false

eureka.client.fetch-registry=false

 


二. 创建“服务提供方”

  1. 步骤:

    1. 新版本

      1. 添加服务注册和发现依赖

      2. 添加配置文件applicationNew.yml

    2. 老版本

      1. 添加服务注册和发现依赖

      2. 添加注解 @EnableDiscoveryClient

      3. 添加配置文件applicationOld.yml

 

pom.xml:

<parent>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-parent</artifactId>

    <version>1.5.4.RELEASE</version>

    <relativePath/> <!-- lookup parent from repository -->

</parent>

 

<dependencies>

    <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-eureka</artifactId>

    </dependency>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>

    </dependency>

</dependencies>

 

<dependencyManagement>

    <dependencies>

        <dependency>

           <groupId>org.springframework.cloud</groupId>

           <artifactId>spring-cloud-dependencies</artifactId>

           <version>Dalston.SR1</version>

           <type>pom</type>

           <scope>import</scope>

        </dependency>

    </dependencies>

</dependencyManagement>

 

applicationOld.yml:

spring:

  application:

    name: zl-data-warning-center

  cloud:

    #指定配置中心的服务

    config:

      discovery:

        enabled: true

        serviceId: zl-configer-center

      profile: dev

server:

  port: 8082

eureka:

  server:

    address: 47.94.223.26

    port: 8761

  client:

    serviceUrl:

      defaultZone: http://${eureka.server.address}:${eureka.server.port}/eureka/

 

application.properties

spring.application.name=eureka-client

server.port=2001

eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

 

applicationNew.yml:

server:

  port: 8771

 

#指定注册中心地址

eureka:

  client:

    serviceUrl:

      defaultZone: http://localhost:8761/eureka/

 

#服务的名称

spring:

  application:

    name: product-service

 


三. Spring Cloud Consul

  1. 官方文档: https://www.consul.io/

  2. 简介:Consul是一个分布式高可用的系统,它包含多个组件,但是作为一个整体,在微服务架构中为我们的基础设施提供服务发现和服务配置的工具。

  3. 特性:

    1. 服务发现

    2. 健康检查

    3. Key/Value存储

    4. 多数据中心

  4. 使用:

    1. 将基于springBoot的微服务注册到Consul上

    2. 步骤:

      1. 将eureka-client的pom.xml中的eurka依赖改为consul-descovery依赖

      2. 修改application.properites,将consul配置加入

 

pom.xml

<dependency>

  <groupId>org.springframework.cloud</groupId>

  <artifactId>spring-cloud-starter-consul-discovery</artifactId>

</dependency>

 

application.properites(yml相同)

spring.cloud.consul.host=localhost

spring.cloud.consul.port=8500

 

  1. 注意:

    1. 不需要创建类似eureka-server的服务端吗?

      1. 由于Consul自身提供了服务端,所以我们不需要像之前实现Eureka的时候创建服务注册中心,直接通过下载consul的服务端程序就可以使用。

    2. 使用命令可以启动consul开发模式

 

命令可以启动consul开发模式:

$consul agent -dev

==> Starting Consul agent...

==> Starting Consul agent RPC...

==> Consul agent running!

           Version: 'v0.7.2'

         Node name: 'Lenovo-zhaiyc'

        Datacenter: 'dc1'

            Server: true (bootstrap: false)

       Client Addr: 127.0.0.1 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400)

      Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)

    Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false

             Atlas: <disabled>

 

 

==> Log data will now stream in as it occurs:

 

 

    2017/06/22 07:50:54 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:127.0.0.1:8300 Address:127.0.0.1:8300}]

    2017/06/22 07:50:54 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "")

    2017/06/22 07:50:54 [INFO] serf: EventMemberJoin: Lenovo-zhaiyc 127.0.0.1

    2017/06/22 07:50:54 [INFO] consul: Adding LAN server Lenovo-zhaiyc (Addr: tcp/127.0.0.1:8300) (DC: dc1)

    2017/06/22 07:50:54 [INFO] serf: EventMemberJoin: Lenovo-zhaiyc.dc1 127.0.0.1

    2017/06/22 07:50:54 [INFO] consul: Adding WAN server Lenovo-zhaiyc.dc1 (Addr: tcp/127.0.0.1:8300) (DC: dc1)

    2017/06/22 07:51:01 [ERR] agent: failed to sync remote state: No cluster leader

    2017/06/22 07:51:02 [WARN] raft: Heartbeat timeout from "" reached, starting election

    2017/06/22 07:51:02 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 2

    2017/06/22 07:51:02 [DEBUG] raft: Votes needed: 1

    2017/06/22 07:51:02 [DEBUG] raft: Vote granted from 127.0.0.1:8300 in term 2. Tally: 1

    2017/06/22 07:51:02 [INFO] raft: Election won. Tally: 1

    2017/06/22 07:51:02 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state

    2017/06/22 07:51:02 [INFO] consul: cluster leadership acquired

    2017/06/22 07:51:02 [INFO] consul: New leader elected: Lenovo-zhaiyc

    2017/06/22 07:51:02 [DEBUG] consul: reset tombstone GC to index 3

    2017/06/22 07:51:02 [INFO] consul: member 'Lenovo-zhaiyc' joined, marking health alive

    2017/06/22 07:51:02 [INFO] agent: Synced service 'consul'

    2017/06/22 07:51:02 [DEBUG] agent: Node info in sync

 


四. eureka和consul控制台

  1. eureka控制台

    1. 注意事项:

      1. eureka管理后台出现一串红色字体:是警告,说明有服务上线率低:EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.

      2. 为什么只加一个注册中心地址,就可以注册:By having spring-cloud-starter-netflix-eureka-client on the classpath, your application automatically registers with the Eureka Server. Configuration is required to locate the Eureka server, as shown in the following example:

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值