zoukankan      html  css  js  c++  java
  • SpringCloud快速搭建

    1.SpringCloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、负载均衡、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,可以在开发人员的电脑上跑。另外说明spring cloud是基于Springboot的,所以需要开发中对Springboot有一定的了解。

    2.服务提供者与消费关系

      就是我我们常说的消费者和生产者

      生产者:提供服务给消费者调用

      消费者:调用生产者提供的服务,从而实现自身的功能模块

    3.服务注册中心Eureka

      与duboo类似,duboo我们一般选择zookeper做服务的注册中心,而springcloud是使用Eureka做服务注册中心的。他的作用是就是服务注册与服务发现。以下是比较官方的说明

      

      官方的介绍在这里Eureka wiki。Eureka是Netflix开源的一个RESTful服务,主要用于服务的注册发现。Eureka由两个组件组成:Eureka服务器和Eureka客户端。Eureka服务器用作服务注册服务器。Eureka客户端是一个java客                                   户端,用来简化与服务器的交互、作为轮询负载均衡器,并提供服务的故障切换支持。Netflix在其生产环境中使用的是另外的客户端,它提供基于流量、资源利用率以及出错状态的加权负载均衡。
           在我看来,Eureka的吸引力来源于以下几点:

      开源:大家可以对实现一探究竟,甚至修改源码。

      可靠:经过Netflix多年的生产环境考验,使用应该比较靠谱省心

      功能齐全:不但提供了完整的注册发现服务,还有Ribbon等可以配合使用的服务。

      基于Java:对于Java程序员来说,使用起来,心里比较有底。

      spring cloud可以使用Spring Cloud, 与Eureka进行了很好的集成,使用起来非常方便。

    4.服务注册

      1.那么我们首先创建一个空的maven项目,就叫springcloud

      

    创建完之后,将src目录删除,我们需要的只是这个外层的一个框架。

    2.创建模块springboot工程

      

    这里我的注册中心的项目名是:eureka-server

    3.application.yml配置

    server:
      port: 8888
    eureka:
      instance:
        hostname: localhost
      client:
        register-with-eureka: false
        fetch-registry: false
        serviceUrl:
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    application.yml

    4.springboot的核心配置类

      

    1 @SpringBootApplication
    2 @EnableEurekaServer
    3 public class EurekaServerApplication {
    4 
    5     public static void main(String[] args) {
    6         SpringApplication.run(EurekaServerApplication.class, args);
    7     }
    8 
    9 }
    EurekaServerApplication.java

    @EnableEurekaServer:开启EurekaServer的注解支持

    5.启动项目,在浏览器上输入http://localhost:8888/当看到以下页面表示成功搭建注册中心

    5.生产者producer

      项目创建方式与服务注册中心的创建方式一样

      1.引入pom依赖

      

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    spring-boot-starter-web

      2.核心配置文件

    1 eureka:
    2   client:
    3     serviceUrl:
    4       defaultZone: http://localhost:8888/eureka/
    5 server:
    6   port: 8763
    7 spring:
    8   application:
    9     name: service-producer
    application.yml

      3.生产者类方法

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.ArrayList;
    import java.util.List;
    
    @RestController
    public class ProducerController {
        @RequestMapping("getUser")
        public List<String>getUser(){
            List<String> lists = new ArrayList<>();
            lists.add("zhangsan");
            lists.add("lisi");
            lists.add("wangwu");
            return lists;
        }
    }
    ProducerController

      4.核心配置类

     1 import org.springframework.boot.SpringApplication;
     2 import org.springframework.boot.autoconfigure.SpringBootApplication;
     3 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
     4 
     5 @SpringBootApplication
     6 @EnableEurekaClient
     7 public class ProducerApplication {
     8     public static void main(String[] args) {
     9         SpringApplication.run(ProducerApplication.class, args);
    10     }
    11 }
    ProducerApplication
    @EnableEurekaClient:可以理解为将该项目注册到Eureka中

     5.启动项目,刷新http://localhost:8888/可以看到该服务注册到Eurika中

     6.消费者consumer

      项目搭建与上诉生产者一样的,没什么变化

      1.pom依赖,与producer使用的一样(我们现在使用的是rest方式传递信息)

      2.application.yml配置文件

     
    1 eureka:
    2   client:
    3     serviceUrl:
    4       defaultZone: http://localhost:8888/eureka/
    5 server:
    6   port: 8764
    7 spring:
    8   application:
    9     name: service-consumer
    application.yml(1)

      3.创建service包和controller包

        service包下的类:调用服务

     1 import org.springframework.beans.factory.annotation.Autowired;
     2 import org.springframework.stereotype.Service;
     3 import org.springframework.web.client.RestTemplate;
     4 
     5 import java.util.List;
     6 
     7 @Service
     8 public class ConsumerService {
     9     @Autowired
    10     private RestTemplate restTemplate;
    11     public List<String> getUser(){
    12         List forObject = restTemplate.getForObject("http://service-producer/getUser", List.class);
    13         return forObject;
    14     }
    15 }
    ConsumerService

        RestTemplate:该类是使用rest方式传递信息的工具类,可以获取到注册到Eurika上的服务

        controller包下的类:获取信息

        List forObject = restTemplate.getForObject("http://service-producer/getUser", List.class);

          第一个参数是请求生产者的url连接,service-producer:生产者在配置文件中配置的名字;第二个是返回值的类型

     1 import com.zy.consumer.service.ConsumerService;
     2 import org.springframework.beans.factory.annotation.Autowired;
     3 import org.springframework.web.bind.annotation.RequestMapping;
     4 import org.springframework.web.bind.annotation.RestController;
     5 
     6 import java.util.List;
     7 
     8 @RestController
     9 public class ConsumerController {
    10     @Autowired
    11     private ConsumerService service;
    12     @RequestMapping("/getUser")
    13     public List<String> getUser(){
    14         List<String> users = service.getUser();
    15         return users;
    16     }
    17 }
    ConsumerController

        与正常mvc的调用顺序是一样的,没有变化

      4.核心配置类

     1 import org.springframework.boot.SpringApplication;
     2 import org.springframework.boot.autoconfigure.SpringBootApplication;
     3 import org.springframework.cloud.client.loadbalancer.LoadBalanced;
     4 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
     5 import org.springframework.context.annotation.Bean;
     6 import org.springframework.web.client.RestTemplate;
     7 
     8 @SpringBootApplication
     9 @EnableEurekaClient
    10 public class ConsumerApplication {
    11 
    12     public static void main(String[] args) {
    13         SpringApplication.run(ConsumerApplication.class, args);
    14     }
    15     @Bean
    16     @LoadBalanced
    17     RestTemplate restTemplate() {
    18         return new RestTemplate();
    19     }
    20 
    21 }
    ConsumerApplication

      RestTemplate:该类没有做自动配置,需要我们手动注入到spring容器当中去

      @LoadBalanced:表示该项目支持负载均衡

      5.启动服务(启动顺序:注册中心>生产者>消费者)

      使用浏览器请求http://localhost:8764/getUser 可以看到在生产者中创建的集合信息,消费者可以获取到

      师承cmy 0.0

  • 相关阅读:
    linux 删除已输入的命令行
    LAMP编译参数查看
    mysql忘记密码的重置方法
    cmd大全_练习
    Parallel WebDriver executions using TestNG
    Internet Explorer for Mac the Easy Way: Run IE 7, IE8, & IE9 Free in a Virtual Machine
    linux delete files older than 3 days
    5 commands to check memory usage on Linux
    Base64 Encoding / Decoding in Node.js
    10 Useful du (Disk Usage) Commands to Find Disk Usage of Files and Directories
  • 原文地址:https://www.cnblogs.com/Tiandaochouqin1/p/10802923.html
Copyright © 2011-2022 走看看