前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springcloud(一)-集成Eureka 服务注册与发现(慕课网廖师兄SpringCloud微服务实战)

springcloud(一)-集成Eureka 服务注册与发现(慕课网廖师兄SpringCloud微服务实战)

作者头像
Meet相识
发布2018-09-12 16:33:17
2.6K0
发布2018-09-12 16:33:17
举报
文章被收录于专栏:技术专栏技术专栏

image.png

1.服务中心

  • 核心依赖
代码语言:javascript
复制
<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>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
  • 启用Eureka Server

在启动类或者配置类上添加@EnableEurekaServer 注解

代码语言:javascript
复制
/**
 * Eureka 启动类
 * @author gaowenfeng
 *
 * @EnableEurekaServer  启用Eureka Server
 */
@SpringBootApplication
@EnableEurekaServer
public class MicroWeatherEurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(MicroWeatherEurekaServerApplication.class, args);
    }
}
  • 配置
代码语言:javascript
复制
server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    # 静止注册成客户端
    register-with-eureka: false
    fetch-registry: false
    # 服务的url
    service-url:
      defaultZone: http://{eureka.instance.hostname}:{server.port}/eureka
  • 检查

启动项目后访问 http://{host}:{serverport}

image.png

2. 服务注册

  • 核心依赖
代码语言:javascript
复制
<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>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
  • 注册服务相关代码实现

启动类或者配置类添加@EnableDiscoveryClient注解

代码语言:javascript
复制
/**
 * 启动类
 * @author gaowenfeng
 */
@SpringBootApplication
@EnableDiscoveryClient
public class MsaWeatherCityServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(MsaWeatherCityServerApplication.class, args);
    }
}
  • 相关配置
代码语言:javascript
复制
spring:
  application:
    name: msa-weather-city-server

eureka:
    # 服务中心的url
    service-url:
      # 这里可以是一个数组,也就是可以注册多多个服务端,用逗号隔开
      defaultZone: http://localhost:8761/eureka/
      instance:
           # 自定义服务的域名
           hostname: clientName
server:
  port: 9991
  • 检查

启动实例,即可在服务中心界面以及控制台看到相关注册信息

image.png

  • Eureka高可用

image.png

让多个Eureka服务端两两注册,即一个Eureka做为另一个Eureka的客户端,然后让Client注册到每一个Eureka服务端上,这样,当一个服务端挂掉以后,在其他的Eureka服务端也可以发现注册的Client

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.02.21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.服务中心
  • 2. 服务注册
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档