1.概述

Spring Cloud为开发人员提供了工具,以快速构建分布式系统中的某些常见模式(例如,配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁,领导选举,分布式会话,群集状态)。

它有助于管理构建分布式系统所涉及的复杂性。

2.微服务

微服务是一种软件开发体系结构样式,它将应用程序分解为一组松散耦合的服务。

它提高了模块性,从而使应用程序更易于开发,测试和部署。

通过使小型团队并行处理不同的服务,这也使开发过程更加高效。

在微服务架构中,服务之间的通信,管理配置等也存在各种困难。

应该通过“ 十二要素应用宣言”来解决微服务体系结构所引起的许多问题。

3. Spring Cloud Config

Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。

它具有两个组件,即配置服务器和配置客户端。

Config Server是在所有环境中管理应用程序外部属性的中心位置。 我们还可以使用Git对配置文件进行版本控制。 它公开了REST API,供客户端连接并获取所需的配置。 我们还可以利用Spring Profiles为不同的Profile(环境)管理不同的配置文件。

3.依存关系

我们将使用Gradle构建我们的项目。 我建议使用Spring Initializr引导您的项目。

我们将使用:

  • Spring靴2
  • Spring Webflux
  • Spring Reactive Data MongoDB
  • Spring Security反应式Webflux
  • Lombok

并非所有的Spring库都有稳定的版本。

Lombok用于减少模型和POJO的样板代码。 它可以自动生成setter / getter,默认构造函数,toString等方法。

buildscript {ext {springBootVersion = '2.0.0.M2'}
...
}dependencies {compile('org.springframework.boot:spring-boot-starter-data-mongodb-reactive')compile('org.springframework.boot:spring-boot-starter-webflux')compile('org.springframework.security:spring-security-core')compile('org.springframework.security:spring-security-config')compile('org.springframework.security:spring-security-webflux')compileOnly('org.projectlombok:lombok')
...
}

4.自动配置

我们将让Spring Boot根据添加的依赖项自动配置我们的应用程序。

@SpringBootApplication
@EnableReactiveMongoRepositories
@EnableWebFluxSecurity
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}

为了在应用程序配置中使用非默认值,我们可以将它们指定为属性,Spring Boot会自动使用它们来创建bean。

spring.data.mongodb.database=demo

MongoDB,Web和安全性所需的所有bean将自动创建。

5.数据库

我们将在示例中使用MongoDB和一个简单的POJO。 将自动创建一个PersonRepository bean。

@Data
@NoArgsConstructor
@Document
public class Person {@Id private String id;private String name;
}public interface PersonRespository extends ReactiveMongoRepository<Person, String> {Flux<Person> findByName(String name);
}

6. Web API

我们将为Person创建REST端点。

Spring 5增加了对在功能上创建路由的支持,同时仍然支持基于注释的传统创建方式。

让我们在示例的帮助下看看它们两个。

基于注释

这是创建端点的传统方式。

@RestController
@RequestMapping("/person")
public class PersonController {@Autowiredprivate PersonRespository personRespository;@GetMappingpublic Flux<Person> index() {return personRespository.findAll();}
}

这将创建一个REST端点/ person ,它将以响应方式返回所有Person记录。

路由器功能

这是创建端点的一种新的简洁方法。

@Bean
RouterFunction<?> routes(PersonRespository personRespository) {return nest(path("/person"),route(RequestPredicates.GET("/{id}"),request -> ok().body(personRespository.findById(request.pathVariable("id")), Person.class)).andRoute(method(HttpMethod.POST),request -> {personRespository.insert(request.bodyToMono(Person.class)).subscribe();return ok().build();}));
}

nest方法用于创建嵌套路由,其中​​一组路由共享一个公共路径(前缀),标头或其他RequestPredicate

因此,在本例中,所有相应的路由都具有公共前缀/ person

在第一个途径中,我们公开了GET API / person / {id} ,它将检索相应的记录并返回它。

在第二种方法中,我们公开了一个POST API / person ,它将接收一个Person对象并将其保存在数据库中。

cURL命令相同:

curl http://localhost:8080/person -v -u tom:password
curl http://localhost:8080/person/{id} -v -u tom:password
curl http://localhost:8080/person -X POST -d '{"name":"John Doe","age":20}' -H "Content-Type: application/json" -v -u tom:password

我们应该在Spring配置文件中定义路由。

7.安全性

在示例中,我们将使用非常简单的基本身份验证机制。

@Bean
UserDetailsRepository userDetailsRepository() {UserDetails tom = withUsername("tom").password("password").roles("USER").build();UserDetails harry = withUsername("harry").password("password").roles("USER", "ADMIN").build();return new MapUserDetailsRepository(tom, harry);
}

我们为应用程序添加了一些用户,并为其分配了不同的角色。

8.结论

我尝试用一​​个简单的示例解释如何使用Spring Boot构建一个简单的Reactive Web应用程序。

您可以阅读有关以下内容的更多信息:

  • 春云
  • Spring数据反应式
  • Spring Functional Web框架

您可以在Github上找到Config Server & Library Service的完整示例。

翻译自: https://www.javacodegeeks.com/2018/04/introduction-to-spring-cloud-config-part-i.html

Spring Cloud简介–配置(第一部分)相关推荐

  1. Spring Cloud学习系列第一章:Eureka之服务注册与发现

    一.Spring Cloud简介 Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线).分布式系统的协调导致了样 ...

  2. 玩转Spring Cloud之配置中心(config server config client)

    玩转Spring Cloud之配置中心(config server &config client)  本文内容导航: 一.搭建配置服务中心(config server) 1.1.git方式 1 ...

  3. Spring cloud简介及Netflix组件介绍

    Spring cloud简介 Spring Cloud是基于Spring Boot的一整套实现微服务的框架.他提供了微服务开发所需的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策 ...

  4. 微服务框架-Spring Cloud简介(一)

    Spring Cloud是一个微服务框架,相比Dubbo等RPC框架, Spring Cloud提供的全套的分布式系统解决方案. Spring Cloud对微服务基础框架Netflix的多个开源组件进 ...

  5. 【夯实Spring Cloud】Spring Cloud分布式配置中心详解

    本文属于[夯实Spring Cloud]系列文章,该系列旨在用通俗易懂的语言,带大家了解和学习Spring Cloud技术,希望能给读者带来一些干货.系列目录如下: [夯实Spring Cloud]D ...

  6. Spring Cloud Gateway配置熔断CircuitBreaker

    我们使用的SpringCloud版本是Hoxton.SR9,小于G版本的是不支持CircuitBreaker的 CircuitBreaker原理 CircuitBreaker是由一个有限状态机实现的, ...

  7. spring cloud nacos 配置多环境打包

    spring cloud nacos 配置多环境打包 一.前言 因需要将项目打包给N多第三方,他们有各自的nacos地址,又需要实现代码无侵入(配置文件存在很多版本如 application-tocd ...

  8. (七)Alian 的 Spring Cloud Config 配置中心(客户端)

    目录 一.背景 二.maven依赖 三.配置文件 四.验证 一.背景   通过上一篇文章,我们已经搭建了配置中心了,接下里我们继续改造我们的订单服务了,之前我们的订单服务的数据库配置还是写在配置文件中 ...

  9. 【第一篇】Spring Cloud简介

    1.1 概述 Spring Cloud是目前微服务架构领域的翘楚,无数的书籍博客都在讲解这个技术.不过大多数讲解还停留在对Spring Cloud功能使用的层面,其底层的很多原理,很多人可能并不知晓. ...

最新文章

  1. 世界五百强世硕科技工作经历——05
  2. 网络爬虫-爬取微博热门话题前15个
  3. 需求的推动力-网线啥的
  4. 企业官网页面设计谨记三个要点!
  5. 【计算机网络】网络层 : IP 组播 ( IP 数据报传输方式 | 组播 IP 地址 | 组播 MAC 地址 | IGMP 协议 | 组播路由选择协议 )
  6. eclipse远程连接hadoop_1个文件,3个类,mapreduce就是这么简单,动手搭建Hadoop(8)...
  7. 数据中心里的应急关机技术
  8. Linux 写时复制机制原理
  9. 【牛客NOIP模拟】牛半仙的妹子序列【DP】【Segment Tree Beats】
  10. jee web_您基于JEE的Web项目的结构是什么?
  11. 论文浅尝 | 利用多语言 wordnet 上随机游走实现双语 embeddings
  12. ssm指的是什么_什么是RESTful?RESTfule风格又是啥?
  13. Lagrangian乘子法 对偶问题 KKT条件 Slater条件 与凸优化
  14. multisim安装
  15. AES MODE_GCM
  16. 原 《老路用得上的商学课》86-90学习笔记
  17. System.InvalidOperationException: Failed to deploy distro docker-desktop......
  18. dimens文件生成器
  19. 大数据服务模型设计:默默无闻的贤内助
  20. 【DL】第 1 章:神经网络相关核心概念

热门文章

  1. Java 并发总结——AQS
  2. 自然语言处理中的Attention Model:是什么以及为什么[一]
  3. MySQL cast()函数
  4. 线程间协作的两种方式:wait、notify、notifyAll和Condition
  5. Oracle入门(十四.20)之创建DML触发器:第一部分
  6. Java Map集合面试题汇总
  7. Hibernate框架之入门配置
  8. JDBC登录功能实现
  9. 阿卡姆疯人院需要java吗_蝙蝠侠阿甘疯人院 这个报错 怎么解决 哪位大神知道...
  10. 转:权限管理——用户认证和用户授权