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

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-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        
        <springcloud.version>Finchley.RELEASE</springcloud.version>
    </properties>
<dependencyManagement>
    <dependencies>
        <!--Spring Cloud dependencies-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${springcloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

  <dependencies>
        <!--eureka-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </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;

/**
 * <b><code>EurekaApplication</code></b>
 * <p/>
 * Description:
 * <p/>
 * <b>Creation Time:</b> 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

在这里插入图片描述

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.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
		<!--健康检查-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </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;

/**
 * <b><code>EurekaApplication</code></b>
 * <p/>
 * Description 服务发现CLIENT的DEMO。
 * 注意与EUREKA不一致
 * <p/>
 * <b>Creation Time:</b> 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
在这里插入图片描述

Author

 作者:HuHui
 转载:欢迎一起讨论web和大数据问题,转载请注明作者和原文链接,感谢
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值