javascript
Spring Cloud简介–配置(第一部分)
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=demoMongoDB,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简介–配置(第一部分)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 雷柏 V300PRO 轻量化双模游戏鼠标
- 下一篇: 国货品牌,不能只靠哭穷