zoukankan      html  css  js  c++  java
  • spring-cloud(一)

    1.SpringCloud概述和搭建Eureka服务注册中心

    Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用Spring Boot的开发风格做到一键启动和部署。Spring并没有重复制造轮子,它只是将目前各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包

    spring cloud的组成

    Spring Cloud的子项目,大致可分成两类,一类是对现有成熟框架”Spring Boot化”的封装和抽象,也是数量最多的项目;第二类是开发了一部分分布式系统的基础设施的实现,如Spring Cloud Stream扮演的就是kafka, ActiveMQ这样的角色。对于我们想快速实践 微服务的开发者来说,第一类子项目就已经足够使用,如:

    Spring Cloud Netflix 
      是对Netflix开发的一套分布式服务框架的封装,包括服务的发现和注册,负载均衡、断路器、REST客户端、请求路由等。

    Spring Cloud Config 
      将配置信息中央化保存, 配置Spring Cloud Bus可以实现动态修改配置文件

    Spring Cloud Bus 
      分布式消息队列,是对Kafka, MQ的封装

    Spring Cloud Security 
      对Spring Security的封装,并能配合Netflix使用

    Spring Cloud Zookeeper 
      对Zookeeper的封装,使之能配置其它Spring Cloud的子项目使用

    spring cloud前景

    Spring Cloud对于中小型互联网公司来说是一种福音,因为这类公司往往没有实力或者没有足够的资金投入去开发自己的分布式系统基础设施,使用Spring Cloud一站式解决方案能在从容应对业务发展的同时大大减少开发成本。同时,随着近几年微服务架构和 Docker容器概念的火爆,也会让Spring Cloud在未来越来越“云”化的软件开发风格中立有一席之地,尤其是在目前五花八门的分布式解决方案中提供了标准化的、全站式的技术方案,意义可能会堪比当前Servlet规范的诞生,有效推进服务端软件系统技术水平的进

      1.创建一个quickstrat项目名称为:springcloud_eureke_server

        2.导入依赖

    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.cloud</groupId>
      <artifactId>springcloud_eureke_server</artifactId>
      <version>1.0-SNAPSHOT</version>
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
      </parent>
    
      <name>springcloud_eureke_server</name>
      <!-- FIXME change it to the project's website -->
      <url>http://www.example.com</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
    
        <!--eureka依赖-->
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
      </dependencies>
    
      <dependencyManagement>
        <dependencies>
          <!--springCloud依赖-->
          <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
        </dependencies>
      </dependencyManagement>
    
      <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
          <plugins>
            <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
            <plugin>
              <artifactId>maven-clean-plugin</artifactId>
              <version>3.1.0</version>
            </plugin>
            <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
            <plugin>
              <artifactId>maven-resources-plugin</artifactId>
              <version>3.0.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.8.0</version>
            </plugin>
            <plugin>
              <artifactId>maven-surefire-plugin</artifactId>
              <version>2.22.1</version>
            </plugin>
            <plugin>
              <artifactId>maven-jar-plugin</artifactId>
              <version>3.0.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-install-plugin</artifactId>
              <version>2.5.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-deploy-plugin</artifactId>
              <version>2.8.2</version>
            </plugin>
            <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
            <plugin>
              <artifactId>maven-site-plugin</artifactId>
              <version>3.7.1</version>
            </plugin>
            <plugin>
              <artifactId>maven-project-info-reports-plugin</artifactId>
              <version>3.0.0</version>
            </plugin>
          </plugins>
        </pluginManagement>
      </build>
    </project>

      3.创建resources文件夹编写application.yml文件

    ##应用名称
    spring:
      application:
        name: eureke_server
    ##声明当前eurekeserver的端口号
    server:
      port: 8001
    ##配置eureka
    eureka:
      client:
        ##代表是否将自己注册到注册中心当中去
        register-with-eureka: false
        ##表明自己是注册中心
        fetch-registry: false
        ##配置地址
        service-url:
          defaultZone: http://localhost:8001/eureka

      4.编写开始StarEurekaServer类

    @SpringBootApplication
    @EnableEurekaServer    //eureka服务注册中心
    public class StarEurekaServer {
        public static void main(String[] args) {
            SpringApplication.run(StarEurekaServer.class,args);
        }
    }

      5.启动

    直接访问http://localhost:8001/

     2.Eureka生产者

      1.创建quickstrat项目spring_eureka_server_Provider

      

        2.引入依赖

    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.cloud</groupId>
      <artifactId>spring_eureka_server_Provider</artifactId>
      <version>1.0-SNAPSHOT</version>
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
      </parent>
      <name>spring_eureka_server_Provider</name>
      <!-- FIXME change it to the project's website -->
      <url>http://www.example.com</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
    
        <!--eureka依赖-->
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
      </dependencies>
      <dependencyManagement>
        <dependencies>
          <!--springCloud依赖-->
          <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
        </dependencies>
      </dependencyManagement>
    
      <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
          <plugins>
            <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
            <plugin>
              <artifactId>maven-clean-plugin</artifactId>
              <version>3.1.0</version>
            </plugin>
            <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
            <plugin>
              <artifactId>maven-resources-plugin</artifactId>
              <version>3.0.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.8.0</version>
            </plugin>
            <plugin>
              <artifactId>maven-surefire-plugin</artifactId>
              <version>2.22.1</version>
            </plugin>
            <plugin>
              <artifactId>maven-jar-plugin</artifactId>
              <version>3.0.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-install-plugin</artifactId>
              <version>2.5.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-deploy-plugin</artifactId>
              <version>2.8.2</version>
            </plugin>
            <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
            <plugin>
              <artifactId>maven-site-plugin</artifactId>
              <version>3.7.1</version>
            </plugin>
            <plugin>
              <artifactId>maven-project-info-reports-plugin</artifactId>
              <version>3.0.0</version>
            </plugin>
          </plugins>
        </pluginManagement>
      </build>
    </project>

      3.创建resources文件夹编写application.yml

    ##配置应用名称
    spring:
      application:
        name: eureka-provider
    ##配置当前服务的端口号
    server :
      port: 9002
    ##找到注册中心地址
    eureka:
      client :
        service-url:
          defaultZone: http://localhost:8001/eureka

      4.创建serviec层编写IDoSomeService

        IDoSomeService
    public interface IDoSomeService {
        String doSome();
    }
        IDoSomeServiceImpl
    @RestController
    public class IDoSomeServiceImpl implements IDoSomeService {
        @RequestMapping(value = "/doSome")
        @Override
        public String doSome() {
            System.out.println("服务提供者");
            return "eureka";
        }
    }

      5.启动

     3.Eureka消费者

      1.创建qustark项目spring_eureka_server_Consumers

        

       2.引入依赖

    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.cloud</groupId>
      <artifactId>spring_eureka_server_Consumers</artifactId>
      <version>1.0-SNAPSHOT</version>
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
      </parent>
      <name>spring_eureka_server_Consumers</name>
      <!-- FIXME change it to the project's website -->
      <url>http://www.example.com</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
        <!--eureka依赖-->
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    
      </dependencies>
    
      <dependencyManagement>
        <dependencies>
          <!--springCloud依赖-->
          <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
        </dependencies>
      </dependencyManagement>
    
      <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
          <plugins>
            <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
            <plugin>
              <artifactId>maven-clean-plugin</artifactId>
              <version>3.1.0</version>
            </plugin>
            <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
            <plugin>
              <artifactId>maven-resources-plugin</artifactId>
              <version>3.0.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.8.0</version>
            </plugin>
            <plugin>
              <artifactId>maven-surefire-plugin</artifactId>
              <version>2.22.1</version>
            </plugin>
            <plugin>
              <artifactId>maven-jar-plugin</artifactId>
              <version>3.0.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-install-plugin</artifactId>
              <version>2.5.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-deploy-plugin</artifactId>
              <version>2.8.2</version>
            </plugin>
            <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
            <plugin>
              <artifactId>maven-site-plugin</artifactId>
              <version>3.7.1</version>
            </plugin>
            <plugin>
              <artifactId>maven-project-info-reports-plugin</artifactId>
              <version>3.0.0</version>
            </plugin>
          </plugins>
        </pluginManagement>
      </build>
    </project>

      3.创建resources文件编写application.yml

    ##配置应用名称
    spring:
      application:
        name: eureka_Consumers
    ##配置当前服务的端口号
    server :
      port: 8899
    ##找到注册中心地址
    eureka:
      client :
        service-url:
          defaultZone: http://localhost:8001/eureka

      4.创建controller

    @RestController
    public class Consumerscontroller {
        @Resource
        private RestTemplate restTemplate;
    
        @Resource
        private IDoSomeService iDoSomeService;
    
        @RequestMapping(value = "/doSome")
        public String doSome(){
            System.out.println("Consumerscontroller");
            return restTemplate.getForObject("http://eureka-provider/doSome",String.class);
        }
    }

      5.编写stratProvider

    @SpringBootApplication
    @EnableEurekaClient
    @EnableFeignClients
    public class StratProvider {
        public static void main(String[] args) {
            SpringApplication.run(StratProvider.class,args);
        }
        }
        @Bean
        RestTemplate restTemplate(){
            return new RestTemplate();
        }
    }

      6.运行

     4.Ribbon负载均衡

       1.修改消费者的代码controller

    package com.cloud.controller;
    
    import com.cloud.service.IDoSomeService;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    import javax.annotation.Resource;
    
    @RestController
    public class Consumerscontroller {
        @Resource
        private RestTemplate restTemplate;
    
        @Resource
        private IDoSomeService iDoSomeService;
    
        @RequestMapping(value = "/doSome")
        public String doSome(){
            return iDoSomeService.doSome();
    
            /*System.out.println("Consumerscontroller");
            return restTemplate.getForObject("http://eureka-provider/doSome",String.class);*/
        }
    }

      注:http协议后面不能再写http://localhost:8899/doSome得写"http://eureka-provider/doSome"

      2.运行看效果

      

     

     5.Feign的使用以及负载均衡策略

      1.引入依赖

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

      2.创建service层

    @FeignClient("eureka-provider")
    public interface IDoSomeService {
        @RequestMapping(value = "/doSome")
        public String doSome();
    }

      3.编写controller

    @RestController
    public class Consumerscontroller {
        @Resource
        private RestTemplate restTemplate;
    
        @Resource
        private IDoSomeService iDoSomeService;
    
        @RequestMapping(value = "/doSome")
        public String doSome(){
            return iDoSomeService.doSome();
    
            /*System.out.println("Consumerscontroller");
            return restTemplate.getForObject("http://eureka-provider/doSome",String.class);*/
        }
    }

    可以看到依旧可以访问

  • 相关阅读:
    Aizu 0525 Osenbei 搜索 A
    PAT 1088 三人行 模拟,坑 C
    POJ1862 Stripies 贪心 B
    ZOJ 4109 Welcome Party 并查集+优先队列+bfs
    POJ 3685 Matrix
    POJ 3579 Median 二分加判断
    Educational Codeforces Round 63 D. Beautiful Array
    Codeforces Round #553 (Div. 2) C
    HDU 5289
    Codeforces 552 E. Two Teams
  • 原文地址:https://www.cnblogs.com/ws1149939228/p/12054556.html
Copyright © 2011-2022 走看看