【Spring Boot】27.spring cloud

简介

Spring Cloud是一个分布式的总体解决方案。Spring Cloud 为开发者提供了在分布式系统(配置管理,服务发现,熔断,路由,微代理,控制总线,一次性token,全局琐,leader选举,分布式session,集群状态)中快速构建的工具,使用Spring Cloud的开发者能够快速的启动服务或构建应用、同时可以快速和云平台资源进行对接。java

SpringCloud分布式开发五大经常使用组件

  • 服务发现——Netflix Eureka
  • 客服端负载均衡——Netflix Ribbon
  • 断路器——Netflix Hystrix
  • 服务网关——Netflix Zuul
  • 分布式配置——Spring Cloud Config

快速体验

搭建项目环境

  1. 建立空项目
  2. 建立注册中心模块:eureka-server,模块选择Eureka-Server
  3. 建立服务提供者模块: provider-ticket,模块选择Eureka-Discovery、web模块。(服务提供者是从服务中心注册本身)
  4. 建立服务消费者模块:consumer-ticket,模块选择Eureka-Discovery、web模块。(服务提供者是从服务中心中获取提供者)

配置注册中心模块

  1. 修改配置文件application.yml:
server:
  port: 8761 # eureka server defort port
eureka:
  instance:
    hostname: eureka-server # eureka instance host name
  client:
    register-with-eureka: false # don't set myself to eureka, default is true
    fetch-registry: false # don't get register info to eureka server, because we will set this item to be server
    service-url:
      defaultZone: http://localhost:8761/eureka/ # register center server url, default value is http://localhost:8761/eureka/

注意我修改properties为yml,这是比较容易阅读的语言,层次更加的清晰。web

  1. 启动类处启用euraka server注解
package com.zhaoyi.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

// 启用注册中心功能(enable eureka server)
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}
  1. 运行项目,访问: localhost:8761 便可查看注册中心网站。接下来,咱们配置服务提供者,将服务注册到注册中心。

配置服务提供者模块

  1. 配置文件application.yml
spring:
  application:
    name: provider-ticket
server:
  port: 8086 # provider server port
eureka:
  instance:
    prefer-ip-address: true # register server use ip style.
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/ # register center url.

该配置中:spring

  1. 编写service,和咱们以前分布式的测试例子同样,是个买票的简单程序
// 接口类
package com.zhaoyi.providerticket.service;
public interface TicketService {
    public String buy();
}
// 接口实现类
package com.zhaoyi.providerticket.service;

import org.springframework.stereotype.Service;

@Service
public class TicketServiceImpl implements TicketService {
    @Override
    public String buy() {
        return "《Grimgar of Fantasy and Ash》";
    }
}
  1. 编写controller,用于显示服务运行端口以及使用买票服务
package com.zhaoyi.providerticket.controller;

import com.zhaoyi.providerticket.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TicketController {

    @Autowired
    TicketService ticketService;

    @Value("${server.port}")
    private Integer serverPort;

    @GetMapping("/ticket")
    public String ticket(){

        return "server:" + serverPort + " and buy a ticket:" + ticketService.buy() ;
    }
}

启动提供模块的时候,请保证注册中心模块处于运行状态。session

为了往服务中心多注册几个服务提供者,咱们修改不一样端口8086以及8087,并经过maven发布多2个jar包,将其复制到外部文件夹中,而后一一使用java -jar启动起来。注意:经过启动参数修改端口号没有意义的,由于咱们就是想体验返回内容的不一样,因此仍是改改端口,发不一样的jar包好些—— 模拟多个provider服务。app

多发布几个provider,同时运行,咱们能够看到,注册中心注册了咱们的多个provider。负载均衡

这时候访问IP:8761,就能够看到咱们的两个提供者实例都注册上了:maven

Application AMIs Availability Zones Status
PROVIDER-TICKET n/a(2) (2) UP (2)-DESKTOP-Q1PPPMM:provider-ticket:8087, DESKTOP-Q1PPPMM:provider-ticket:8086

配置服务消费模块

  1. 配置文件配置
spring:
  application:
    name: consumer-ticket
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
  instance:
    prefer-ip-address: false
server:
  port: 8899

一样的,咱们开启8899端口,服务名设置为consumer-ticket,注册中心地址注册为http://localhost:8761/eureka,使用ip方式进行注册。分布式

  1. 主启动类开启发现服务功能而且添加RestTmeplate组件
package com.zhaoyi.consumerticket;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerTicketApplication {

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

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplateBuilder().build();
    }
}

咱们开启了发现服务注解,而且在容器中添加了一个类型为RestTemplate的Bean,经过其为咱们调用注册中心的具体实现。ide

@LoadBalanced注解能够实现对调用服务的负载均衡访问。工具

  1. 编写controller,消费注册中心为咱们提供的服务
package com.zhaoyi.consumerticket.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class IndexController {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/buy")
    public String buy(){
        return "恭喜您,购买了" + restTemplate.getForObject("http://PROVIDER-TICKET/ticket", String.class
        );
    }
}

PROVIDER-TICKET是咱们注册的服务的名字,能够从你的服务中心首页的注册项的application栏目查找到;

ticket是提供服务者监听的服务地址,即至关于咱们访问服务IP:8086/ticket同样的效果。

运行效果,咱们访问消费者ip:8899/buy,获得以下输出:

恭喜您,购买了server:8086 and buy a ticket:《Grimgar of Fantasy and Ash》

同时,多访问几回,你会看到8086 8087的控制台会分别相应各请求,也能够经过打印的端口号看出具体是哪一个端口上的服务响应的请求,这取决于负载均衡的流量导向。

结束语

spring cloud算是目前分布式开发的一个很好的解决方案,还有不少内容须要咱们去了解,但愿本文可以为您开启一个初始的印象,迎头而上,在springcloud的使用道路上一路凯歌。

相关文章
相关标签/搜索