美文网首页学习
Spring Cloud微服务框架升级

Spring Cloud微服务框架升级

作者: questionuncle | 来源:发表于2018-03-02 16:47 被阅读119次

在《Spring Cloud实现文件上传》、《基于fastdfs实现文件上传微服务》两篇文章中,作者已对Spring Cloud有所接触也有点想法,想将搭建起整个生态框架,麻雀虽小,五脏得全。

1.总体框架

在脑海里酝酿了许久,也观摩多个例子,结合自己的实际情况,总算把完整的一套框架搭建起来。以下是架构图:


image.png

从架构图上,可以看出有6个服务,其中Eureka Server、uploadpicture service服务已经开发完成。其余服务需求逐渐添加进来。

2.Gateway Service

网关服务是新增的第一个服务,其采用Zuul实现服务的代理。

2.1为什么需要它

我们的业务服务有很多个,每个服务或许都有自己独立的部署环境,其还会伸缩服务数量,自然调用地址、端口、数量等都会改变。如果没有个统一的对外出口,让前端应用怎么办。

2.2如何应用

添加相关的依赖,配置相关端口及路由

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
spring:
  application:
    name: api-gateway-microservice
zuul:
  routes:
      uploadpicture-consumer-microservice:
      path: /uploadpicture/**
      url: http://localhost:8044
      stripPrefix: false
      sensitiveHeaders:

3.Center Service

中心服务作为图片上传服务消费者,同时承担各个服务的连接中心,通过Feign对图片上传服务进行调用。

3.1Feign

Feign是声明式Web服务客户端,所谓声明式,指的是在代码中,将要调用的其它内部微服务提供的Restful API声明为自己的接口,该接口使用FeignClient注解。声明后,在本微服务中就可以通过接口注入的方式消费其它微服务的提供的Restful API了。
参考:利用Spring Cloud实现微服务(七)- 内部调用

3.2具体实现

Spring Cloud实现文件上传

4.Ribbon Service

Ribbon Service作为负载均衡器,为后端服务提供负载均衡。

4.1为什么需要它

后端的业务服务可以起多个,并注册到服务注册中心去,从网关服务直接做条路由到业务服务岂不很好,为什么还要增加一个Ribbon Service。
我们可以看看网关服务中路由是怎么配的。

routes:
  uploadpicture-consumer-microservice:
      path: /uploadpicture/**
      url: http://localhost:8044
      stripPrefix: false
      sensitiveHeaders:

可以看出路由配置时已经绑定好服务地址了,这如何做负载?显然做不了。

4.2如何应用

添加依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>

开启Ribbon的负载均衡能力

@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class RibbonMicroserviceApplication {

    public static void main(String[] args) {
        SpringApplication.run(RibbonMicroserviceApplication.class, args);
    }

    @LoadBalanced
    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

利用RestTemplate调用

@Service
public class CenterService {
    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "fallback")
    public Result findUser(String userName, String passWord) {
        String uri = String.format("http://center-microservice/center/users/author?username=%s&password=%s", userName, passWord);
        return this.restTemplate.getForObject(uri, Result.class);
    }

    public Result fallback(String userName, String password) {
        return new Result(1, "用户名密码错误");
    }
}

5.Config Service

配置服务主要是管理服务群各服务的配置信息。

5.1为什么需要它

各个服务配置在各自工程下挺好,为啥要把它拿出来,放到配置服务中加以管理。这就要提一下现在流行的容器技术了,试想一下,如果各个业务服务均已打包成镜像,现在需要对其配置作修改,我们该怎么办。那就需要拿出源码,修改相应的配置,然后再打包。如果我们在用户现场没有源码在手呢,估计得请老巢内同事帮忙了。而配置服务可以解决这个问题,将配置放置在该服务内,修改完后让业务服务重新拉取一下配置即可。

5.2如何应用

在Config Server和Client中分别添加依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>

相关配置

server:
  port: 8888

spring:
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/shared
  profiles:
     active: native

注意,这里也可以配置git或svn库的方式,来加载各服务的相关配置文件。

测试

image.png

相关文章

网友评论

    本文标题:Spring Cloud微服务框架升级

    本文链接:https://www.haomeiwen.com/subject/aqpaxftx.html