网站建设php培训,53货源网下载app,东莞做公众号的网站,长沙seo优化外包公司1.概述 Spring Cloud为开发人员提供了工具#xff0c;以快速构建分布式系统中的某些常见模式#xff08;例如#xff0c;配置管理#xff0c;服务发现#xff0c;断路器#xff0c;智能路由#xff0c;微代理#xff0c;控制总线#xff0c;一次性令牌#xff0c;全局… 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.databasedemo MongoDBWeb和安全性所需的所有bean将自动创建。 5.数据库 我们将在示例中使用MongoDB和一个简单的POJO。 将自动创建一个PersonRepository bean。 Data
NoArgsConstructor
Document
public class Person {Id private String id;private String name;
}public interface PersonRespository extends ReactiveMongoRepositoryPerson, String {FluxPerson findByName(String name);
}6. Web API 我们将为Person创建REST端点。 Spring 5增加了对在功能上创建路由的支持同时仍然支持基于注释的传统创建方式。 让我们在示例的帮助下看看它们两个。 基于注释 这是创建端点的传统方式。 RestController
RequestMapping(/person)
public class PersonController {Autowiredprivate PersonRespository personRespository;GetMappingpublic FluxPerson 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