zoukankan      html  css  js  c++  java
  • SpringBoot分布式篇Ⅷ --- 整合SpringCloud

    SpringCloud是一个分布式的整体解决方案。Spring Cloud为开发者提供了在分布式系统(配置管理,服务发现,熔断,路由,微代理,控制总线,一次性token,全局锁,leader选举.分布式session,集群状态)中快速构建的工具,使用SpringCloud的开发者可以快速的启动服务或构建应用、同时能够快速和云平台资源进行对接。本篇只讲述SpringBoot与SpringCloud的整合以及简单使用,若要详细学习SpringCloud请参考其他文章或官方文档。

    一.Spring Cloud分布式开发五大常用组件

    1.服务发现 --- Netfix Eureka

    2.客户端负载均衡 --- Netfix Ribbon

    3.断路器 --- Netfix Hystrix

    4.服务网关 --- Netfix Zuul

    5.分布式配置 --- Spring Cloud Config

    二.创建Eureka注册中心
    创建一个空的项目,在该项目下创建一个eureka-server模块。

    eureka-server所需要的依赖:

     <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
                <version>1.4.4.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>

    2.1 配置eureka相关信息

    server:
      port: 8761
    
    eureka:
      instance:
        hostname: eureka-server #eureka实例的主机名
      client:
        register-with-eureka: false #不把自己注册到eureka上
        fetch-registry: false #不从eureka上来获取服务的注册信息
        service-url:
          defaultZon: http://localhost:8761/eureka/

    2.2 开启注册中心功能 @EnableEurekaServer

    @EnableEurekaServer
    @SpringBootApplication
    public class EurekaServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaServerApplication.class, args);
        }
    
    }

    启动时eureka-server,若启动报错:java.lang.NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBuilder.<init>([Ljava,则更换SpringBoot版本为1.5.12.RELEASE

    三.服务提供者注册服务

    3.1 创建应用provider-ticket

    导入eureka依赖:

     <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
                <version>1.4.4.RELEASE</version>
     </dependency>
    @RestController
    public class TicketController {
        @Autowired
        TicketService ticketService;
    
        @GetMapping("/ticket")
        public String getTicket(){
            System.out.println("8002端口应用");
            return ticketService.getTicket();
        }
    }
    @Service
    public class TicketService {
        public String getTicket(){
            return "《海王》";
        }
    }

    3.2 配置服务

    server:
      port: 8002
    spring:
      application:
        name: provider-ticket
    
    eureka:
      instance:
        prefer-ip-address: true #注册服务的时候使用服务的ip地址
      client:
        service-url:
          defaultZon: http://localhost:8761/eureka/

    启动时若报错则跟换SpringBoot的版本为1.5.12.RELEASE版本。

    3.3 注册多个服务

    首先将服务用maven工具打包成jar,打包的时候遇到了一些问题。是SpringBoot与SpringCloud的版本不匹配导致的。下面是两者的版本对应明细

    如果还是报错,看一下eureka-server项目是否成功启动了,在服务注册期间eureka-server要保持运行状态,打包成功后,修改provider-ticket的端口号:

    server:
      port: 8002

    再次打包,然后使用java -jar provider-ticket-0.0.1-SNAPSHOT.jar命令分别启动两个jar包项目。查看http://localhost:8761/

    上述状态说明两个服务已经成功地在Eureka上注册了。

    四.发现消费服务

    4.1 引入依赖

     <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      </dependency>

    4.2 在启动类开启发现服务功能,并配置RestTemplate

    @EnableDiscoveryClient //开启发现服务功能
    @SpringBootApplication
    public class ConsumerUserApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConsumerUserApplication.class, args);
        }
        @LoadBalanced //使用负载均衡机制
        @Bean
        public RestTemplate restTemplate(){
            return new RestTemplate();
        }
    }

    4.3 配置发现服务信息

    spring:
      application:
        name: consumer-user
    server:
      port: 8200
    
    eureka:
      instance:
        prefer-ip-address: true #注册服务的时候使用服务的ip地址
      client:
        service-url:
          defaultZon: http://localhost:8761/eureka/

    4.4 调用服务

    @RestController
    public class UserController {
        @Autowired
        RestTemplate restTemplate;
        @GetMapping("/")
        @ResponseBody
        public String buyTicket(){
            String str = restTemplate.getForObject("http://PROVIDER-TICKET/ticket", String.class);
            return str;
        }
    }

    4.5 测试

    启动consumer-user应用,访问http://localhost:8200/,查看是否有返回值,刷新页面看是否启用了负载均衡,将eureka中的两个服务都调用了。

  • 相关阅读:
    Linux记录-普通用户下执行sudo xxx 找不到命令解决方案
    Hadoop记录-MRv2(Yarn)运行机制
    The difference between Severity and Priority
    1.3 Seven Testing Principles
    [转]ISTQB FL初级认证考试资料(中文)
    ISTQB测试人员认证 初级(基础级)大纲
    自定义控件VS用户控件
    进程、线程与应用程序域
    [转]软件测试演义——中高级系列(序)
    [转]在做自动化测试之前你需要知道的
  • 原文地址:https://www.cnblogs.com/wangxiayun/p/10320126.html
Copyright © 2011-2022 走看看