Spring Cloud构建分布式微服务云架构基础
2017-12-11 09:00
796 查看
使用LoadBalancerClient
在Spring Cloud Commons中提供了大量的与服务治理相关的抽象接口,包括DiscoveryClient、这里我们即将介绍的
LoadBalancerClient等。对于这些接口的定义我们在上一篇介绍服务注册与发现时已经说过,Spring
Cloud做这一层抽象,很好的解耦了服务治理体系,使得我们可以轻易的替换不同的服务治理设施。
从
LoadBalancerClient接口的命名中,我们就知道这是一个负载均衡客户端的抽象定义,下面我们就看看如何使用Spring
Cloud提供的负载均衡器客户端接口来实现服务的消费。
下面的例子,我们将利用上一篇中构建的eureka-server作为服务注册中心、eureka-client作为服务提供者作为基础。
我们先来创建一个服务消费者工程,命名为:
eureka-consumer。并在
pom.xml中引入依赖(这里省略了parent和dependencyManagement的配置):
<<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">dependencies> <<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">dependency> <<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">groupId>org.springframework.cloud</<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">groupId> <<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">artifactId>spring-cloud-starter-eureka</<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">artifactId> </<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">dependency> <<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">dependency> <<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">groupId>org.springframework.boot</<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">groupId> <<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">artifactId>spring-boot-starter-web</<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">artifactId> </<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">dependency> <<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">dependency> <<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">groupId>org.springframework.boot</<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">groupId> <<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">artifactId>spring-boot-starter-actuator</<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">artifactId> </<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">dependency> </<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">dependencies> |
application.properties,指定eureka注册中心的地址:
spring.application.name=eureka-consumer server.port=2101 eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/ |
RestTemplate,用来真正发起REST请求。
@EnableDiscoveryClient注解用来将当前应用加入到服务治理体系中。
@EnableDiscoveryClient @SpringBootApplication public class Application { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } } |
@RestController public class DcController { @Autowired LoadBalancerClient loadBalancerClient; @Autowired RestTemplate restTemplate; @GetMapping("/consumer") public String dc() { ServiceInstance serviceInstance = loadBalancerClient.choose("eureka-client"); String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/dc"; System.out.println(url); return restTemplate.getForObject(url, String.class); } } |
LoadBalancerClient和
RestTemplate,并在
/consumer接口的实现中,先通过
loadBalancerClient的
choose函数来负载均衡的选出一个
eureka-client的服务实例,这个服务实例的基本信息存储在
ServiceInstance中,然后通过这些对象中的信息拼接出访问
/dc接口的详细地址,最后再利用
RestTemplate对象实现对服务提供者接口的调用。
从现在开始,我这边会将近期研发的springcloud微服务云架构的搭建过程和精髓记录下来,帮助更多有兴趣研发spring
cloud框架的朋友,希望可以帮助更多的好学者。大家来一起探讨spring
cloud架构的搭建过程及如何运用于企业项目。源码来源
相关文章推荐
- 整合spring cloud云服务架构 - 企业分布式微服务云架构构建
- Spring Cloud--鸿鹄Cloud分布式微服务云系统—云架构代码结构构建
- Spring Cloud Spring Boot mybatis分布式微服务云架构(五)构建RESTful API
- Spring Cloud Spring Boot mybatis分布式微服务云架构(二)使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程
- Spring Cloud Spring Boot mybatis分布式微服务云架构(九)使用Swagger2构建强大的RESTful API文档(1)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(五)构建RESTful API
- 整合企业Spring Cloud分布式微服务云架构构建
- Spring Cloud--鸿鹄Cloud分布式微服务云系统—云架构代码结构构建
- Spring Cloud Spring Boot mybatis分布式微服务云架构(九)使用Swagger2构建强大的RESTful API文档(1)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十)使用Swagger2构建强大的RESTful API文档(2)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(九)使用Swagger2构建强大的RESTful API文档(1)
- Spring Cloud 分布式微服务架构 企业级实战 :一、项目构建
- Spring Cloud--鸿鹄Cloud分布式微服务云系统—云架构代码结构构建
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十)使用Swagger2构建强大的RESTful API文档(2)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(二)使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程
- Spring Cloud云服务架构 - 企业分布式微服务云架构构建
- Spring Cloud Spring Boot mybatis分布式微服务云架构(二)使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程
- Spring Cloud Spring Boot mybatis分布式微服务云架构(五)构建RESTful API
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十八)使用Spring-data-jpa(2)
- Spring Cloud分布式微服务云架构服务组件