eureka是netflix开源的一款提供服务注册和发现的产品,它提供了完整的service registry(注册登记)和service discovery(发现)实现。也是springcloud体系中最重要最核心的组件之一。
服务中心:
服务中心又称注册中心,管理各种服务功能包括服务的注册、发现、熔断、负载、降级等。
有了服务中心调用关系会有什么变化,画几个简图来帮忙理解
项目a调用项目b
正常调用项目a请求项目b
有了服务中心之后,任何一个服务都不能直接去掉用,都需要通过服务中心来调用
项目a调用项目b,项目b在调用项目c
这时候调用的步骤就会为两步:第一步,项目a首先从服务中心请求项目b服务器,然后项目b在从服务中心请求项目c服务。
上面的项目只是两三个相互之间的简单调用,但是如果项目超过20个30个呢,画一张图来描述几十个项目之间的相互调用关系全是线条,任何其中的一个项目改动,就会牵连好几个项目跟着重启,巨麻烦而且容易出错。通过服务中心来获取服务你不需要关注你调用的项目ip地址,由几台服务器组成,每次直接去服务中心获取可以使用的服务去调用既可。
由于各种服务都注册到了服务中心,就有了去做很多高级功能条件。比如几台服务提供相同服务来做均衡负载;监控服务器调用成功率来做熔断,移除服务列表中的故障点;监控服务调用时间来对不同的服务器设置不同的权重等等。
eureka由两个组件组成:eureka服务器和eureka客户端。eureka服务器用作服务注册服务器。eureka客户端是一个java客户端,用来简化与服务器的交互、作为轮询负载均衡器,并提供服务的故障切换支持。netflix在其生产环境中使用的是另外的客户端,它提供基于流量、资源利用率以及出错状态的加权负载均衡。
上图简要描述了eureka的基本架构,由3个角色组成:
1、eureka server
2、service provider
3、service consumer
创建一个简单的maven springboot项目
pom里面添加如下依赖:
1 <parent> 2 <groupid>org.springframework.boot</groupid> 3 <artifactid>spring-boot-starter-parent</artifactid> 4 <version>1.3.5.release</version> 5 <relativepath/> <!-- lookup parent from repository --> 6 </parent> 7 8 <dependencies> 9 <dependency> 10 <groupid>org.springframework.boot</groupid> 11 <artifactid>spring-boot-starter-test</artifactid> 12 <scope>test</scope> 13 </dependency> 14 15 <dependency> 16 <groupid>org.springframework.cloud</groupid> 17 <artifactid>spring-cloud-starter-eureka-server</artifactid> 18 </dependency> 19 </dependencies> 20 21 <dependencymanagement> 22 <dependencies> 23 <dependency> 24 <groupid>org.springframework.cloud</groupid> 25 <artifactid>spring-cloud-dependencies</artifactid> 26 <version>brixton.release</version> 27 <type>pom</type> 28 <scope>import</scope> 29 </dependency> 30 </dependencies> 31 </dependencymanagement>
通过@enableeurekaserver
注解启动一个服务注册中心提供给其他应用进行对话。这一步非常的简单,只需要在一个普通的spring boot应用中添加这个注解就能开启此功能,比如下面的例子:
1 package com; 2 3 import org.springframework.boot.autoconfigure.springbootapplication; 4 import org.springframework.boot.builder.springapplicationbuilder; 5 import org.springframework.cloud.netflix.eureka.server.enableeurekaserver; 6 7 @enableeurekaserver//服务注册中心开启注解 8 @springbootapplication 9 public class application { 10 11 public static void main(string[] args) { 12 new springapplicationbuilder(application.class).web(true).run(args); 13 } 14 15 }
在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,只需要在application.properties
中问增加如下配置:
1 #启动端口 2 server.port=1111 3 4 #关闭掉自己往服务中心注册的机制 5 eureka.client.register-with-eureka=false 6 #是否检索服务 7 eureka.client.fetch-registry=false 8 #服务中心地址 9 eureka.client.serviceurl.defaultzone=http://localhost:${server.port}/eureka/
项目结构:
启动项目访问
界面如下
下面我们创建提供服务的客户端,并向服务注册中心注册自己。
假设我们有一个提供计算功能的微服务模块,我们实现一个restful api,通过传入两个参数a和b,最后返回a + b的结果。
首先,创建一个基本的spring boot应用,在pom.xml
中,加入如下配置:
1 <parent> 2 <groupid>org.springframework.boot</groupid> 3 <artifactid>spring-boot-starter-parent</artifactid> 4 <version>1.3.5.release</version> 5 <relativepath/> <!-- lookup parent from repository --> 6 </parent> 7 8 <dependencies> 9 <dependency> 10 <groupid>org.springframework.boot</groupid> 11 <artifactid>spring-boot-starter-test</artifactid> 12 <scope>test</scope> 13 </dependency> 14 15 <dependency> 16 <groupid>org.springframework.cloud</groupid> 17 <artifactid>spring-cloud-starter-eureka</artifactid> 18 </dependency> 19 </dependencies> 20 21 <dependencymanagement> 22 <dependencies> 23 <dependency> 24 <groupid>org.springframework.cloud</groupid> 25 <artifactid>spring-cloud-dependencies</artifactid> 26 <version>brixton.release</version> 27 <type>pom</type> 28 <scope>import</scope> 29 </dependency> 30 </dependencies> 31 </dependencymanagement>
创建我们的业务访问控制器
1 package com; 2 3 import org.apache.log4j.logger; 4 import org.springframework.beans.factory.annotation.autowired; 5 import org.springframework.cloud.client.serviceinstance; 6 import org.springframework.cloud.client.discovery.discoveryclient; 7 import org.springframework.web.bind.annotation.requestmapping; 8 import org.springframework.web.bind.annotation.requestmethod; 9 import org.springframework.web.bind.annotation.requestparam; 10 import org.springframework.web.bind.annotation.restcontroller; 11 12 @restcontroller 13 public class computecontroller { 14 15 private final logger logger = logger.getlogger(getclass()); 16 17 @autowired 18 private discoveryclient client; 19 20 @requestmapping(value = "/add" ,method = requestmethod.get) 21 public integer add(@requestparam integer a, @requestparam integer b) { 22 serviceinstance instance = client.getlocalserviceinstance(); 23 integer r = a + b; 24 logger.info("/add, host:" + instance.gethost() + ", service_id:" + instance.getserviceid() + ", result:" + r); 25 return r; 26 } 27 28 }
创建启动器
enablediscoveryclient
注解,该注解能激活eureka中的discoveryclient
实现,才能实现controller中对服务信息的输出。
package com; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.boot.builder.springapplicationbuilder; import org.springframework.cloud.client.discovery.enablediscoveryclient; @enablediscoveryclient//标识客户端,并扫描控制器里面的discoveryclient @springbootapplication public class computeserviceapplication { public static void main(string[] args) { new springapplicationbuilder(computeserviceapplication.class).web(true).run(args); } }
application.properties
1 #指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问。 2 spring.application.name=compute-service 3 server.port=2222 4 #属性对应服务注册中心的配置内容,指定服务注册中心的位置。 5 eureka.client.serviceurl.defaultzone=http://localhost:1111/eureka/
启动本项目,然后再访问就可以看到服务已经注册到服务中心
使用ribbon实现负载均衡的消费者,构建一个基本spring boot项目,并在pom.xml中加入如下内容:
1 <parent> 2 <groupid>org.springframework.boot</groupid> 3 <artifactid>spring-boot-starter-parent</artifactid> 4 <version>1.3.5.release</version> 5 <relativepath/> <!-- lookup parent from repository --> 6 </parent> 7 8 <dependencies> 9 <dependency> 10 <groupid>org.springframework.cloud</groupid> 11 <artifactid>spring-cloud-starter-ribbon</artifactid> 12 </dependency> 13 <dependency> 14 <groupid>org.springframework.cloud</groupid> 15 <artifactid>spring-cloud-starter-eureka</artifactid> 16 </dependency> 17 <dependency> 18 <groupid>org.springframework.boot</groupid> 19 <artifactid>spring-boot-starter-web</artifactid> 20 </dependency> 21 <dependency> 22 <groupid>org.springframework.boot</groupid> 23 <artifactid>spring-boot-starter-test</artifactid> 24 <scope>test</scope> 25 </dependency> 26 </dependencies> 27 28 <dependencymanagement> 29 <dependencies> 30 <dependency> 31 <groupid>org.springframework.cloud</groupid> 32 <artifactid>spring-cloud-dependencies</artifactid> 33 <version>brixton.release</version> 34 <type>pom</type> 35 <scope>import</scope> 36 </dependency> 37 </dependencies> 38 </dependencymanagement>
通过@loadbalanced注解实现负载均衡的开启
1 package com; 2 3 import org.springframework.boot.springapplication; 4 import org.springframework.boot.autoconfigure.springbootapplication; 5 import org.springframework.cloud.client.discovery.enablediscoveryclient; 6 import org.springframework.cloud.client.loadbalancer.loadbalanced; 7 import org.springframework.context.annotation.bean; 8 import org.springframework.web.client.resttemplate; 9 10 @springbootapplication 11 @enablediscoveryclient 12 public class ribbonapplication { 13 14 @bean 15 @loadbalanced//负载均衡的开启 16 resttemplate resttemplate() { 17 return new resttemplate(); 18 } 19 20 public static void main(string[] args) { 21 springapplication.run(ribbonapplication.class, args); 22 } 23 24 }
创建consumercontroller
来消费compute-service
的add服务。通过直接resttemplate来调用服务,计算10 + 20的值。
1 package com; 2 3 import org.springframework.beans.factory.annotation.autowired; 4 import org.springframework.web.bind.annotation.requestmapping; 5 import org.springframework.web.bind.annotation.requestmethod; 6 import org.springframework.web.bind.annotation.restcontroller; 7 import org.springframework.web.client.resttemplate; 8 9 @restcontroller 10 public class consumercontroller { 11 12 @autowired 13 resttemplate resttemplate; 14 15 @requestmapping(value = "/add", method = requestmethod.get) 16 public string add() { 17 return resttemplate.getforentity("http://compute-service/add?a=10&b=20", string.class).getbody(); 18 } 19 // 20 }
application.properties
中配置eureka服务注册中心
1 spring.application.name=ribbon-consumer 2 server.port=3333 3 eureka.client.serviceurl.defaultzone=http://www.lhsxpumps.com/_localhost:1111/eureka/
这个时候修改服务提供方把端口改为2223,再启动一个服务提供
可一看到服务中心已经被注册了两个服务。
启动服务消费方,并访问五次:
然后,打开compute-service的两个服务提供方,分别输出了类似下面的日志内容:
端口2222的
端口2223的
可以看到,之前启动的两个compute-service服务端分别被调用了三次,两次。到这里,我们已经通过ribbon在客户端已经实现了对服务调用的均衡负载。
项目源码 1--spingcloud_服务注册与发现eureka
参考:http://www.ityouknow.com/springcloud/2017/05/10/springcloud-eureka.html
如对本文有疑问, 点击进行留言回复!!
Postgresql结合postgis使用java的JDBC连接
网友评论