学习Spring-Cloud –编写微服务

继续我的Spring-Cloud学习历程, 之前我已经介绍了如何编写典型的基于Spring-CloudNetflix 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-pong
  cloud:
    config:
      discovery:
        enabled: true
        serviceId: SAMPLE-CONFIG

eureka:
  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注册服务上的端点,以总结我拥有的选项:

  1. 使用原始的Eureka DiscoveryClient查找托管服务的实例,并使用Spring的RestTemplate进行调用。
  2. 使用Ribbon客户端负载均衡解决方案,可以使用Eureka查找服务实例
  3. 使用Feign ,它提供了一种声明性的方式来调用服务调用。 内部使用功能区。

我和费恩一起去。 所需要的只是一个接口,该接口显示了调用服务的合同:

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)
    @ResponseBody
    MessageAcknowledgement sendMessage(@RequestBody Message message);
}

注释@FeignClient(“ samplepong”)内部指向功能区“命名”客户端,称为“ samplepong”。 这意味着该命名客户端的属性文件中必须有一个条目,就我而言,我的application.yml文件中包含以下条目:

samplepong:
  ribbon:
    DeploymentContextBasedVipAddresses: sample-pong
    NIWSServerListClassName: com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList
    ReadTimeout: 5000
    MaxAutoRetries: 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 –

SampleSpringCloudView

此外,Hystrix仪表板应可用于监视对此URL http:// dockerhost:8989 / hystrix / monitor?stream = http%3A%2F%2Fsampleping%3A8080%2Fhystrix.stream的“ pong”应用程序的请求:

SpringCloud-Hystrix

参考文献

  1. 该代码位于我的github位置 – https://github.com/bijukunjummen/spring-cloud-ping-pong-sample
  2. 大多数代码都是从spring-cloud-samples存储库大量借用 – https://github.com/spring-cloud-samples

翻译自: https://www.javacodegeeks.com/2015/07/learning-spring-cloud-writing-a-microservice.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值