SpringCloud服务注册中心

SpringCloud服务注册中心

Spring Cloud 是一系列框架的有序集合,如服务注册发现、配置中心、消息总线、负载均衡、断路器等,都可以用 Spring Boot 的开发风格做到一键启动和部署。

下面我们介绍 Spring Cloud 组件之一:服务注册中心Eureka的搭建。

注册中心搭建

创建项目

创建一个普通的Spring Boot项目,并将其命名为eureka-server

添加依赖

eureka-serverpom.xml中添加如下依赖

<dependency> 
  <groupId>org.springframework.cloud</groupId> 
  <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 
</dependency> 

目前Spring Cloud的最新版本是Hoxton.SR3

eureka-server服务的启动类EurekaServerApplication
添加注解@EnableEurekaServer表示这个服务是一个Eureka服务。

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

进行服务的配置

#配置注册中心的端口
server:
  port: 8761
eureka:
  # 设置Eureka的hostname
  instance:
    hostname: locahost
  # 设置不检索其他服务
  client:
    fetch-registry: false
  # 不注册自己
    register-with-eureka: false
  # 服务注册地址
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

1、由于我的Euerka服务是在本地,所以hostname设置的是locahost,大家可以根据自己的需求灵活设置自己的。

2、fetch-registry服务注册中心本身的职责是维护其他服务的服务信息列表,因此它本身不需要去检索其他服务

3、register-with-eureka默认情况下,会向注册中心注册自己,由于本身就是一个服务注册中心,不需要自己注册自己。

注册中心验证

以上的所有工作完成以后,只需要浏览器输入http://localhost:8761如果出现如下页面:

小结

好啦,小伙伴们,服务注册中心,搭建完了,持续关注北漂码农有话说,更多精彩敬请期待!

原文地址:https://www.cnblogs.com/triumph-wyp-com/p/13363761.html