springcloud干货之服务注册与发现(尤里卡)

  

<强>使用尤里卡实现服务治理

  

作用:实现服务治理(服务注册与发现)

  

简介:春天云尤里卡是春云Netflix项目下的服务治理模块,而春天云Netflix项目是春云的子项目之一,主要内容是对Netflix公司一系列开源产品的包装,它为春天引导应用提供了自配置的Netflix OSS整合。通过一些简单的注解,开发者就可以快速的在应用中配置一下常用模块并构建庞大的分布式系统。它主要提供的模块包括:服务发现(尤里卡),断路器(Hystrix)、智能路由(Zuul),客户端负载均衡(丝带)等。

  

<强>项目实战:

  

服务注册中心:eureka-server

  

作用:服务注册中心提供服务注册功能

  

服务提供方:eureka-client

  

作用:注册服务到服务注册中心

  

<>强服务注册中心:eureka-server

  

新建一个springboot项目:eureka-server,其pom.xml配置如下:

        & lt; properties>   & lt; project.build.sourceEncoding> UTF-8   & lt; project.reporting.outputEncoding> UTF-8   & lt; java.version> 1.8 & lt;/java.version>   & lt;/properties>   & lt; dependencies>   & lt; dependency>   & lt; groupId> org.springframework.cloud   & lt; artifactId> spring-cloud-starter-eureka-server   & lt;/dependency>   & lt;/dependencies>   & lt; dependencyManagement>   & lt; dependencies>   & lt; dependency>   & lt; groupId> org.springframework.cloud   & lt; artifactId> spring-cloud-dependencies   & lt; version> Dalston.SR1   & lt; type> pom   & lt; scope> import   & lt;/dependency>   & lt;/dependencies>   & lt;/dependencyManagement>      

想要实现一个服务注册中心的功能非常简单,只需要在项目的启动类EurekaServerApplication上使用@EnableEurekaServer注解即可

        @EnableEurekaServer   @SpringBootApplication   公开课EurekaServerApplication {      公共静态void main (String [] args) {   新SpringApplicationBuilder (EurekaServerApplication.class)   .web(真正的).run (args);   }   }   之前      

默认情况下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,只需要在application.properties配置文件中增加如下信息:

        spring.application.name=eureka-server   server.port=1001   eureka.instance.hostname=localhost   eureka.client.register-with-eureka=false   eureka.client.fetch-registry=false      

启动EurekaServerApplication,访问http://localhost: 9001/可以看到尤里卡的页面,从红框的位置可以看到没有任务服务实例注册到当前的服务注册中心

  

 springcloud干货之服务注册与发现(尤里卡),

  

<>强服务提供方:eureka-client

  

每一个实例注册之后需要向注册中心发送心跳,当客户端向服务器注册时,它会提供一些元数据,例如主机和端口,URL,主页等.Eureka服务器从每个客户实例接收心跳消息。如果心跳超时,则通常将该实例从注册服务器中删除。

  

新建一个springboot项目:eureka-client,其pom.xml配置如下:

  

        & lt; properties>   & lt; project.build.sourceEncoding> UTF-8   & lt; project.reporting.outputEncoding> UTF-8   & lt; java.version> 1.8 & lt;/java.version>   & lt;/properties>   & lt; dependencies>   & lt; dependency>   & lt; groupId> org.springframework.cloud   & lt; artifactId> spring-cloud-starter-eureka   & lt;/dependency>   & lt; dependency>   & lt; groupId> org.springframework.boot   & lt; artifactId> spring-boot-starter-web   & lt;/dependency>   & lt;/dependencies>   & lt; dependencyManagement>   & lt; dependencies>   & lt; dependency>   & lt; groupId> org.springframework.cloud   & lt; artifactId> spring-cloud-dependencies   & lt; version> Dalston.SR1   & lt; type> pom   & lt; scope> import   & lt;/dependency>   & lt;/dependencies>   & lt;/dependencyManagement>      

想要实现一个服务提供方也很简单,只要在项目的启动类EurekaClientApplication上使用@EnableEurekaClient注解即可

springcloud干货之服务注册与发现(尤里卡)