SpringCloud 服务注册与发现

2020-05-08  本文已影响0人  凡毓不凡
  • SpringCloud 版本Hoxton.SR1
  • SpringBoot 版本2.2.1.RELEASE
  • 关键字@EnableEurekaServer,@EnableEurekaClient

1. 搭建服务端

 <dependencyManagement>
    <dependencies>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-dependencies</artifactId>
             <version>Hoxton.SR1</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
     </dependencies>
 </dependencyManagement>
  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  </dependency> 

spring.application.name=eureka-server
server.port=7070 # 端口自定义
eureka.server.enable-self-preservation = false #不开启自我保护,将不可用的实例剔除
eureka.client.register-with-eureka=false #false表示自己就是服务端,不注册到Eureka中
eureka.client.fetch-registry=false #false表示自己就是服务端,不拉取服务注册表
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/ #覆盖默认的defaultZone

  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.boot.builder.SpringApplicationBuilder;
  import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  @SpringBootApplication
  @EnableEurekaServer
  public class EurekaServer {
      public static void main(String[] args) {
          new SpringApplicationBuilder(EurekaServer.class).run(args);
      }
  }

2. 搭建客户端

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

spring.application.name=eureka-client
server.port=7076 # 端口自定义
eureka.client.register-with-eureka= true #默认为true,注册到Eureka中
management.endpoints.web.exposure.include=* # expose endpoints
eureka.client.fetch-registry= true #默认为true,获取注册列表
eureka.client.serviceUrl.defaultZone=http://localhost:7070/eureka/

  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.boot.builder.SpringApplicationBuilder;
  import org.springframework.cloud.netflix.eureka.server.EnableEurekaClient;
  @SpringBootApplication
  @EnableEurekaClient
  public class EurekaClient {
      public static void main(String[] args) {
          new SpringApplicationBuilder(EurekaClient.class).run(args);
      }
  }

3. 总结

  1. ☛ 文章要是勘误或者知识点说的不正确,欢迎评论,毕竟这也是作者通过阅读源码获得的知识,难免会有疏忽!
  2. 要是感觉文章对你有所帮助,不妨点个关注,或者移驾看一下作者的其他文集,也都是干活多多哦,文章也在全力更新中。
  3. 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处!
上一篇下一篇

猜你喜欢

热点阅读