javascript
学习Spring-Cloud –编写微服务
繼續(xù)我的Spring-Cloud學(xué)習(xí)歷程, 之前我已經(jīng)介紹了如何編寫(xiě)典型的基于Spring-Cloud和Netflix OSS的微服務(wù)環(huán)境的基礎(chǔ)架構(gòu)組件–在此特定實(shí)例中,有兩個(gè)關(guān)鍵組件,用于注冊(cè)和發(fā)現(xiàn)服務(wù)的Eureka和Spring Cloud用于維護(hù)服務(wù)配置集中式配置庫(kù)的配置。 在這里,我將展示如何開(kāi)發(fā)兩個(gè)虛擬微服務(wù),一個(gè)是簡(jiǎn)單的“ pong”服務(wù),另一個(gè)是使用“ pong”服務(wù)的“ ping”服務(wù)。
Sample-Pong微服務(wù)
處理“ ping”請(qǐng)求的端點(diǎn)是典型的基于Spring MVC的端點(diǎn):
@RestController public class PongController {@Value("${reply.message}")private String message;@RequestMapping(value = "/message", method = RequestMethod.POST)public Resource<MessageAcknowledgement> pongMessage(@RequestBody Message input) {return new Resource<>(new MessageAcknowledgement(input.getId(), input.getPayload(), message));}}它收到一條消息并以確認(rèn)響應(yīng)。 在這里,該服務(wù)利用配置服務(wù)器來(lái)獲取“ reply.message”屬性。 因此,“ pong”服務(wù)如何找到配置服務(wù)器,可能有兩種方式-直接通過(guò)指定配置服務(wù)器的位置,或通過(guò)Eureka查找配置服務(wù)器。 我習(xí)慣了將Eureka視為事實(shí)來(lái)源的方法,因此本著這種精神,我正在使用Eureka查找配置服務(wù)器。 Spring Cloud使整個(gè)流程變得非常簡(jiǎn)單,它所需要的只是一個(gè)“ bootstrap.yml”屬性文件,其內(nèi)容如下:
--- spring:application:name: sample-pongcloud:config:discovery:enabled: trueserviceId: SAMPLE-CONFIGeureka:instance:nonSecurePort: ${server.port:8082}client:serviceUrl:defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/通過(guò)“ eureka.client.serviceUrl”屬性指定Eureka的位置,并將“ spring.cloud.config.discovery.enabled”設(shè)置為“ true”以指定通過(guò)指定的Eureka服務(wù)器發(fā)現(xiàn)配置服務(wù)器。
僅需注意 ,這意味著Eureka和Configuration Server必須先完全啟動(dòng),然后才能?chē)L試提供實(shí)際服務(wù),這是先決條件,并且基本假設(shè)是在應(yīng)用程序啟動(dòng)時(shí)可以使用Infrastructure組件。
配置服務(wù)器具有“ sample-pong”服務(wù)的屬性,可以使用Config-servers端點(diǎn)進(jìn)行驗(yàn)證-http:// localhost:8888 / sample-pong / default,8888是我為之指定的端口服務(wù)器端點(diǎn),并應(yīng)按照以下內(nèi)容響應(yīng)內(nèi)容:
"name": "sample-pong","profiles": ["default"],"label": "master","propertySources": [{"name": "classpath:/config/sample-pong.yml","source": {"reply.message": "Pong"}}] }可以看出,該中央配置服務(wù)器的“ reply.message”屬性將被pong服務(wù)用作確認(rèn)消息。
現(xiàn)在要將此端點(diǎn)設(shè)置為服務(wù),所需要做的就是沿著這些行基于Spring-boot的入口點(diǎn):
@SpringBootApplication @EnableDiscoveryClient public class PongApplication {public static void main(String[] args) {SpringApplication.run(PongApplication.class, args);} }這樣就完成了“ pong”服務(wù)的代碼。
抽樣微服務(wù)
因此,現(xiàn)在轉(zhuǎn)到“乒乓”微服務(wù)的消費(fèi)者上,該消費(fèi)者非常富想象力地稱(chēng)為“乒乓”微服務(wù)。 Spring-Cloud和Netflix OSS提供了許多選項(xiàng)來(lái)調(diào)用Eureka注冊(cè)服務(wù)上的端點(diǎn),以總結(jié)我擁有的選項(xiàng):
我和費(fèi)恩一起去。 所需要的只是一個(gè)接口,該接口顯示了調(diào)用服務(wù)的合同:
package org.bk.consumer.feign;import org.bk.consumer.domain.Message; import org.bk.consumer.domain.MessageAcknowledgement; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody;@FeignClient("samplepong") public interface PongClient {@RequestMapping(method = RequestMethod.POST, value = "/message",produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)@ResponseBodyMessageAcknowledgement sendMessage(@RequestBody Message message); }注釋@FeignClient(“ samplepong”)內(nèi)部指向功能區(qū)“命名”客戶端,稱(chēng)為“ samplepong”。 這意味著該命名客戶端的屬性文件中必須有一個(gè)條目,就我而言,我的application.yml文件中包含以下條目:
samplepong:ribbon:DeploymentContextBasedVipAddresses: sample-pongNIWSServerListClassName: com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerListReadTimeout: 5000MaxAutoRetries: 2這里最重要的條目是“ samplepong.ribbon.DeploymentContextBasedVipAddresses”,它指向“ pong”服務(wù)Eureka注冊(cè)地址,Ribbon將使用該地址注冊(cè)服務(wù)實(shí)例。
該應(yīng)用程序的其余部分是一個(gè)常規(guī)的Spring Boot應(yīng)用程序。 我已經(jīng)在Hystrix后面公開(kāi)了此服務(wù)調(diào)用,該服務(wù)可以防止服務(wù)調(diào)用失敗,并且基本上可以包裝此FeignClient:
package org.bk.consumer.service;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.bk.consumer.domain.Message; import org.bk.consumer.domain.MessageAcknowledgement; import org.bk.consumer.feign.PongClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service;@Service("hystrixPongClient") public class HystrixWrappedPongClient implements PongClient {@Autowired@Qualifier("pongClient")private PongClient feignPongClient;@Override@HystrixCommand(fallbackMethod = "fallBackCall")public MessageAcknowledgement sendMessage(Message message) {return this.feignPongClient.sendMessage(message);}public MessageAcknowledgement fallBackCall(Message message) {MessageAcknowledgement fallback = new MessageAcknowledgement(message.getId(), message.getPayload(), "FAILED SERVICE CALL! - FALLING BACK");return fallback;} }“啟動(dòng)”起來(lái)
我已經(jīng)對(duì)整個(gè)設(shè)置進(jìn)行了docker化,因此啟動(dòng)應(yīng)用程序集的最簡(jiǎn)單方法是首先通過(guò)以下方式為所有工件構(gòu)建docker映像:
mvn clean package docker:build -DskipTests并使用以下命令將其全部調(diào)出,假設(shè)docker和docker-compose都在本地可用:
docker-compose up假設(shè)一切正常,Eureka應(yīng)該顯示所有已注冊(cè)的服務(wù),網(wǎng)址為http:// dockerhost:8761 url –
ping應(yīng)用程序的用戶界面應(yīng)位于http:// dockerhost:8080 url –
此外,Hystrix儀表板應(yīng)可用于監(jiān)視對(duì)此URL http:// dockerhost:8989 / hystrix / monitor?stream = http%3A%2F%2Fsampleping%3A8080%2Fhystrix.stream的“ pong”應(yīng)用程序的請(qǐng)求:
參考文獻(xiàn)
翻譯自: https://www.javacodegeeks.com/2015/07/learning-spring-cloud-writing-a-microservice.html
總結(jié)
以上是生活随笔為你收集整理的学习Spring-Cloud –编写微服务的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 怎样编写测试类测试分支_测试技巧–不编写
- 下一篇: 网站企业代备案(网站代备案淘宝)