javascript
学习Spring-Cloud –编写微服务
繼續我的Spring-Cloud學習歷程, 之前我已經介紹了如何編寫典型的基于Spring-Cloud和Netflix OSS的微服務環境的基礎架構組件–在此特定實例中,有兩個關鍵組件,用于注冊和發現服務的Eureka和Spring Cloud用于維護服務配置集中式配置庫的配置。 在這里,我將展示如何開發兩個虛擬微服務,一個是簡單的“ pong”服務,另一個是使用“ pong”服務的“ ping”服務。
Sample-Pong微服務
處理“ ping”請求的端點是典型的基于Spring MVC的端點:
@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));}}它收到一條消息并以確認響應。 在這里,該服務利用配置服務器來獲取“ reply.message”屬性。 因此,“ pong”服務如何找到配置服務器,可能有兩種方式-直接通過指定配置服務器的位置,或通過Eureka查找配置服務器。 我習慣了將Eureka視為事實來源的方法,因此本著這種精神,我正在使用Eureka查找配置服務器。 Spring Cloud使整個流程變得非常簡單,它所需要的只是一個“ bootstrap.yml”屬性文件,其內容如下:
--- 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/通過“ eureka.client.serviceUrl”屬性指定Eureka的位置,并將“ spring.cloud.config.discovery.enabled”設置為“ true”以指定通過指定的Eureka服務器發現配置服務器。
僅需注意 ,這意味著Eureka和Configuration Server必須先完全啟動,然后才能嘗試提供實際服務,這是先決條件,并且基本假設是在應用程序啟動時可以使用Infrastructure組件。
配置服務器具有“ sample-pong”服務的屬性,可以使用Config-servers端點進行驗證-http:// localhost:8888 / sample-pong / default,8888是我為之指定的端口服務器端點,并應按照以下內容響應內容:
"name": "sample-pong","profiles": ["default"],"label": "master","propertySources": [{"name": "classpath:/config/sample-pong.yml","source": {"reply.message": "Pong"}}] }可以看出,該中央配置服務器的“ reply.message”屬性將被pong服務用作確認消息。
現在要將此端點設置為服務,所需要做的就是沿著這些行基于Spring-boot的入口點:
@SpringBootApplication @EnableDiscoveryClient public class PongApplication {public static void main(String[] args) {SpringApplication.run(PongApplication.class, args);} }這樣就完成了“ pong”服務的代碼。
抽樣微服務
因此,現在轉到“乒乓”微服務的消費者上,該消費者非常富想象力地稱為“乒乓”微服務。 Spring-Cloud和Netflix OSS提供了許多選項來調用Eureka注冊服務上的端點,以總結我擁有的選項:
我和費恩一起去。 所需要的只是一個接口,該接口顯示了調用服務的合同:
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”)內部指向功能區“命名”客戶端,稱為“ samplepong”。 這意味著該命名客戶端的屬性文件中必須有一個條目,就我而言,我的application.yml文件中包含以下條目:
samplepong:ribbon:DeploymentContextBasedVipAddresses: sample-pongNIWSServerListClassName: com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerListReadTimeout: 5000MaxAutoRetries: 2這里最重要的條目是“ samplepong.ribbon.DeploymentContextBasedVipAddresses”,它指向“ pong”服務Eureka注冊地址,Ribbon將使用該地址注冊服務實例。
該應用程序的其余部分是一個常規的Spring Boot應用程序。 我已經在Hystrix后面公開了此服務調用,該服務可以防止服務調用失敗,并且基本上可以包裝此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;} }“啟動”起來
我已經對整個設置進行了docker化,因此啟動應用程序集的最簡單方法是首先通過以下方式為所有工件構建docker映像:
mvn clean package docker:build -DskipTests并使用以下命令將其全部調出,假設docker和docker-compose都在本地可用:
docker-compose up假設一切正常,Eureka應該顯示所有已注冊的服務,網址為http:// dockerhost:8761 url –
ping應用程序的用戶界面應位于http:// dockerhost:8080 url –
此外,Hystrix儀表板應可用于監視對此URL http:// dockerhost:8989 / hystrix / monitor?stream = http%3A%2F%2Fsampleping%3A8080%2Fhystrix.stream的“ pong”應用程序的請求:
參考文獻
翻譯自: https://www.javacodegeeks.com/2015/07/learning-spring-cloud-writing-a-microservice.html
總結
以上是生活随笔為你收集整理的学习Spring-Cloud –编写微服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎样编写测试类测试分支_测试技巧–不编写
- 下一篇: Java 9抢先体验:与JShell的动