【SpringCloud】Register-Server-注册中心(二)

文章目录

  • Note
  • GitHub
  • Finchley分支(Eureka)
    • Code
    • UI
  • Greenwich分支(Consul)
    • 官网下载&安装
    • 启动Consul服务
    • Consul-Client-Code
    • UI
  • Author

Note

对应两条分支
springcloud-greenwich集成eureka + springboot2.1.x
springcloud-finchley集成consul + springboot2.0.x

注册中心可以说是微服务架构中的”通讯录“,它记录了服务和服务地址的映射关系。在分布式架构中,服务会注册到这里,当服务需要调用其它服务时,就到这里找到服务的地址,进行调用

GitHub

地址:https://github.com/ithuhui/hui-base-springcloud
分支:finchley | greenwich
模块:【hui-base-springcloud-config】
finchley分支配置中心配置存放在git-> https://github.com/ithuhui/hui-base-springcloud-config-repo

Finchley分支(Eureka)

Code

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
        
        <springcloud.version>Finchley.RELEASEspringcloud.version>
    properties>
<dependencyManagement>
    <dependencies>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-dependenciesartifactId>
            <version>${springcloud.version}version>
            <type>pomtype>
            <scope>importscope>
        dependency>
    dependencies>
dependencyManagement>

  <dependencies>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
        dependency>
    dependencies>
server:
  port: 28761
eureka:
  instance:
    prefer-ip-address: true
    hostname: localhost
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}
  client:
    service-url:
      defaultZone: http://localhost:28761/eureka/
    register-with-eureka: false
#    开发的时候关闭自我保护提醒
  server:
    enable-self-preservation: false
spring:
  application:
    name: eureka-server
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * EurekaApplication
 * 

* Description: *

* Creation Time: 2018/11/12 23:56. * * @author Hu Weihui */ @EnableEurekaServer @SpringBootApplication public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }

UI

【SpringCloud】Register-Server-注册中心(二)_第1张图片

Greenwich分支(Consul)

官网下载&安装

https://www.consul.io/

这里我开发环境在windows本机运行

在windows环境变量配置Path

D:\software\nodejs\;D:\dev\consul_1.5.2_windows_amd64;

启动Consul服务

默认端口为8500

consul agent -dev

Consul-Client-Code

        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-consul-discoveryartifactId>
        dependency>
		
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>

server:
  port: 29090
  servlet:
    context-path: /service-client
spring:
  application:
    name: service-client
  profiles:
    active: dev
  ## consul 配置
  cloud:
    consul:
      # consul服务器地址
      host: localhost
      # consul服务端口
      port: 8500
      # 服务发现配置
      discovery:
        # 服务名称
        service-name: ${spring.application.name}
        # 启用服务发现
        enabled: true
        # 启用服务注册
        register: true
        # 服务停止时取消注册
        deregister: true
        # 表示注册时使用IP而不是hostname
        prefer-ip-address: true
        # 执行监控检查的频率
        health-check-interval: 30s
        # 设置健康检查失败多长时间后,取消注册
        health-check-critical-timeout: 30s
        # 健康检查的路径
        health-check-path: /${spring.application.name}/actuator/info
        # 服务注册标识,格式为:应用名称+服务器IP+端口
        instance-id: ${spring.application.name}
        #instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}

package com.hui.base.springcloud.discovery;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * EurekaApplication
 * 

* Description 服务发现CLIENT的DEMO。 * 注意与EUREKA不一致 *

* Creation Time: 2019/2/25 16:05. * * @author Hu-Weihui * @since hui-base-springcloud ${PROJECT_VERSION} */ @SpringBootApplication @EnableDiscoveryClient public class DiscoveryApplication { public static void main(String[] args) { SpringApplication.run(DiscoveryApplication.class, args); } }

UI

访问 localhost:8500
【SpringCloud】Register-Server-注册中心(二)_第2张图片

Author

 作者:HuHui
 转载:欢迎一起讨论web和大数据问题,转载请注明作者和原文链接,感谢

你可能感兴趣的:(SpringCloud)