javascript
Spring Cloud简介–配置(第一部分)
1.概述
Spring Cloud為開(kāi)發(fā)人員提供了工具,以快速構(gòu)建分布式系統(tǒng)中的某些常見(jiàn)模式(例如,配置管理,服務(wù)發(fā)現(xiàn),斷路器,智能路由,微代理,控制總線,一次性令牌,全局鎖,領(lǐng)導(dǎo)選舉,分布式會(huì)話,群集狀態(tài))。
它有助于管理構(gòu)建分布式系統(tǒng)所涉及的復(fù)雜性。
2.微服務(wù)
微服務(wù)是一種軟件開(kāi)發(fā)體系結(jié)構(gòu)樣式,它將應(yīng)用程序分解為一組松散耦合的服務(wù)。
它提高了模塊性,從而使應(yīng)用程序更易于開(kāi)發(fā),測(cè)試和部署。
通過(guò)使小型團(tuán)隊(duì)并行處理不同的服務(wù),這也使開(kāi)發(fā)過(guò)程更加高效。
在微服務(wù)架構(gòu)中,服務(wù)之間的通信,管理配置等也存在各種困難。
應(yīng)該通過(guò)“ 十二要素應(yīng)用宣言”來(lái)解決微服務(wù)體系結(jié)構(gòu)所引起的許多問(wèn)題。
3. Spring Cloud Config
Spring Cloud Config為分布式系統(tǒng)中的外部化配置提供服務(wù)器和客戶端支持。
它具有兩個(gè)組件,即配置服務(wù)器和配置客戶端。
Config Server是在所有環(huán)境中管理應(yīng)用程序外部屬性的中心位置。 我們還可以使用Git對(duì)配置文件進(jìn)行版本控制。 它公開(kāi)了REST API,供客戶端連接并獲取所需的配置。 我們還可以利用Spring Profiles為不同的Profile(環(huán)境)管理不同的配置文件。
3.依存關(guān)系
我們將使用Gradle構(gòu)建我們的項(xiàng)目。 我建議使用Spring Initializr引導(dǎo)您的項(xiàng)目。
我們將使用:
- Spring靴2
- Spring Webflux
- Spring Reactive Data MongoDB
- Spring Security反應(yīng)式Webflux
- Lombok
并非所有的Spring庫(kù)都有穩(wěn)定的版本。
Lombok用于減少模型和POJO的樣板代碼。 它可以自動(dòng)生成setter / getter,默認(rèn)構(gòu)造函數(shù),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.自動(dòng)配置
我們將讓Spring Boot根據(jù)添加的依賴項(xiàng)自動(dòng)配置我們的應(yīng)用程序。
@SpringBootApplication @EnableReactiveMongoRepositories @EnableWebFluxSecurity public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);} }為了在應(yīng)用程序配置中使用非默認(rèn)值,我們可以將它們指定為屬性,Spring Boot會(huì)自動(dòng)使用它們來(lái)創(chuàng)建bean。
spring.data.mongodb.database=demoMongoDB,Web和安全性所需的所有bean將自動(dòng)創(chuàng)建。
5.數(shù)據(jù)庫(kù)
我們將在示例中使用MongoDB和一個(gè)簡(jiǎn)單的POJO。 將自動(dòng)創(chuàng)建一個(gè)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創(chuàng)建REST端點(diǎn)。
Spring 5增加了對(duì)在功能上創(chuàng)建路由的支持,同時(shí)仍然支持基于注釋的傳統(tǒng)創(chuàng)建方式。
讓我們?cè)谑纠膸椭驴纯此鼈儍蓚€(gè)。
基于注釋
這是創(chuàng)建端點(diǎn)的傳統(tǒng)方式。
@RestController @RequestMapping("/person") public class PersonController {@Autowiredprivate PersonRespository personRespository;@GetMappingpublic Flux<Person> index() {return personRespository.findAll();} }這將創(chuàng)建一個(gè)REST端點(diǎn)/ person ,它將以響應(yīng)方式返回所有Person記錄。
路由器功能
這是創(chuàng)建端點(diǎn)的一種新的簡(jiǎn)潔方法。
@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方法用于創(chuàng)建嵌套路由,其中??一組路由共享一個(gè)公共路徑(前綴),標(biāo)頭或其他RequestPredicate 。
因此,在本例中,所有相應(yīng)的路由都具有公共前綴/ person 。
在第一個(gè)途徑中,我們公開(kāi)了GET API / person / {id} ,它將檢索相應(yīng)的記錄并返回它。
在第二種方法中,我們公開(kāi)了一個(gè)POST API / person ,它將接收一個(gè)Person對(duì)象并將其保存在數(shù)據(jù)庫(kù)中。
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我們應(yīng)該在Spring配置文件中定義路由。
7.安全性
在示例中,我們將使用非常簡(jiǎn)單的基本身份驗(yàn)證機(jī)制。
@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); }我們?yōu)閼?yīng)用程序添加了一些用戶,并為其分配了不同的角色。
8.結(jié)論
我嘗試用一??個(gè)簡(jiǎn)單的示例解釋如何使用Spring Boot構(gòu)建一個(gè)簡(jiǎn)單的Reactive Web應(yīng)用程序。
您可以閱讀有關(guān)以下內(nèi)容的更多信息:
- 春云
- Spring數(shù)據(jù)反應(yīng)式
- Spring Functional Web框架
您可以在Github上找到Config Server & Library Service的完整示例。
翻譯自: https://www.javacodegeeks.com/2018/04/introduction-to-spring-cloud-config-part-i.html
總結(jié)
以上是生活随笔為你收集整理的Spring Cloud简介–配置(第一部分)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 雷柏 V300PRO 轻量化双模游戏鼠标
- 下一篇: 认识JSON绑定:概述系列