Spring Cloud(一):服务注册与发现

Spring Cloud是什么

Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。Spring并没有重复制造轮子,它只是将目前各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。

微服务是可以独立部署、水平扩展、独立访问(或者有独立的数据库)的服务单元,springcloud就是这些微服务的大管家,采用了微服务这种架构之后,项目的数量会非常多,springcloud做为大管家需要管理好这些微服务,自然需要很多小弟来帮忙。

和Spring Boot 是什么关系

Spring Boot 是 Spring 的一套快速配置脚手架,可以基于Spring Boot 快速开发单个微服务,Spring Cloud是一个基于Spring Boot实现的云应用开发工具;Spring Boot专注于快速、方便集成的单个个体,Spring Cloud是关注全局的服务治理框架;Spring Boot使用了默认大于配置的理念,很多集成方案已经帮你选择好了,能不配置就不配置,Spring Cloud很大的一部分是基于Spring Boot来实现,可以不基于Spring Boot吗?不可以。

Spring Boot可以离开Spring Cloud独立使用开发项目,但是Spring Cloud离不开Spring Boot,属于依赖的关系。

    spring -> spring booot > Spring Cloud 这样的关系。

Spring Cloud的优势

微服务的框架那么多比如:dubbo、Kubernetes,为什么就要使用Spring Cloud的呢?

  • 产出于spring大家族,spring在企业级开发框架中无人能敌,来头很大,可以保证后续的更新、完善。比如dubbo现在就差不多死了
  • 有Spring Boot 这个独立干将可以省很多事,大大小小的活Spring Boot都搞的挺不错。
  • 作为一个微服务治理的大家伙,考虑的很全面,几乎服务治理的方方面面都考虑到了,方便开发开箱即用。
  • Spring Cloud 活跃度很高,教程很丰富,遇到问题很容易找到解决方案
  • 轻轻松松几行代码就完成了熔断、均衡负责、服务中心的各种平台功能

Spring Cloud对于中小型互联网公司来说是一种福音,因为这类公司往往没有实力或者没有足够的资金投入去开发自己的分布式系统基础设施,使用Spring Cloud一站式解决方案能在从容应对业务发展的同时大大减少开发成本。同时,随着近几年微服务架构和Docker容器概念的火爆,也会让Spring Cloud在未来越来越“云”化的软件开发风格中立有一席之地,尤其是在目前五花八门的分布式解决方案中提供了标准化的、全站式的技术方案,意义可能会堪比当前Servlet规范的诞生,有效推进服务端软件系统技术水平的进步。

Spring Cloud Eureka

我们用Spring Cloud Eureka来实现服务治理。

创建“服务注册中心”

创建一个基础的Spring Boot工程,命名为springboot-register,并在pom.xml中引入需要的依赖内容:

 1 <dependencies>
 2         <dependency>
 3             <groupId>org.springframework.boot</groupId>
 4             <artifactId>spring-boot-starter-web</artifactId>
 5         </dependency>
 6 
 7         <dependency>
 8             <groupId>org.springframework.cloud</groupId>
 9             <artifactId>spring-cloud-starter-eureka-server</artifactId>
10         </dependency>
11         <dependency>
12             <groupId>org.springframework.boot</groupId>
13             <artifactId>spring-boot-starter-test</artifactId>
14             <scope>test</scope>
15         </dependency>
16     </dependencies>
17     <dependencyManagement>
18         <dependencies>
19             <dependency>
20                 <groupId>org.springframework.cloud</groupId>
21                 <artifactId>spring-cloud-dependencies</artifactId>
22                 <version>Brixton.SR5</version>
23                 <type>pom</type>
24                 <scope>import</scope>
25             </dependency>
26         </dependencies>
27     </dependencyManagement>

通过@EnableEurekaServer注解启动一个服务注册中心提供给其他应用进行对话:

@EnableEurekaServer
@SpringBootApplication
public class ApplicationRegister {

    public static void main(String[] args) {
        SpringApplication.run(ApplicationRegister.class, args);
    }
}

application.properties配置文件中增加如下信息:

server.port=1111
eureka.server.enable-self-preservation=false
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

访问:http://localhost:1111/ ,如图所示:

页面中还没有任何服务!

注册“服务提供方”

创建一个基本的Spring Boot应用。命名为spring boot-client,在pom.xml中,加入如下配置:

 1 <dependency>
 2             <groupId>org.springframework.boot</groupId>
 3             <artifactId>spring-boot-starter-web</artifactId>
 4         </dependency>
 5 
 6         <dependency>
 7             <groupId>org.springframework.cloud</groupId>
 8             <artifactId>spring-cloud-starter-eureka-server</artifactId>
 9         </dependency>
10         <dependency>
11             <groupId>org.springframework.boot</groupId>
12             <artifactId>spring-boot-starter-test</artifactId>
13             <scope>test</scope>
14         </dependency>
15     </dependencies>
16     <dependencyManagement>
17         <dependencies>
18             <dependency>
19                 <groupId>org.springframework.cloud</groupId>
20                 <artifactId>spring-cloud-dependencies</artifactId>
21                 <version>Brixton.SR5</version>
22                 <type>pom</type>
23                 <scope>import</scope>
24             </dependency>
25         </dependencies>
26     </dependencyManagement>

编写一个controller:

 1 @RestController
 2 public class HelloController {
 3     private final Logger logger = Logger.getLogger(getClass());
 4 
 5     @Autowired
 6     private DiscoveryClient client;
 7 
 8     @RequestMapping(value = "/hello", method = RequestMethod.GET)
 9     public String index() throws Exception{
10        
11         return "Hello World";
12     }
13 }

最后在应用主类中通过加上@EnableDiscoveryClient注解,实现Controller中对服务信息的输出:

1 @EnableDiscoveryClient
2 @SpringBootApplication
3 public class ApplicationClient {
4 
5     public static void main(String[] args) {
6         SpringApplication.run(ApplicationClient.class, args);
7     }
8 
9 }

在application.properties配置文件中增加如下信息:

server.port=2224

spring.application.name=hello-service
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

启动该工程后,再次访问:http://localhost:1111/ ,如图:

我们也可以通过直接访问spring boot-client服务提供的/hello接口,访问:http://localhost:2224/hello,得到如下输出:

这样我们的服务注册与发现就完成了。

猜你喜欢

转载自www.cnblogs.com/heqiyoujing/p/9212600.html