zoukankan      html  css  js  c++  java
  • springcloud 学习

    参考:

    spring cloud 入门系列一:初识spring cloud

    http://blog.didispace.com/Spring-Cloud%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B/

    这里总结一些内容

    一,spring cloud 入门系列一:初识spring cloud

    了解spring cloud,不赘述

    二,spring cloud 入门系列二:使用Eureka 进行服务治理

    功能:创建服务注册和发现中心

    首先,创建一个总的maven项目,然后里面其他的都创建 maven-module

    关于pom文件,

    springcloudtest总的pom文件

    <?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.mytest</groupId>
        <artifactId>springcloudtest</artifactId>
        <packaging>pom</packaging>
        <version>1.0-SNAPSHOT</version>
        <modules>
            <module>eureak</module>
            <module>helloservice</module>
            <module>helloconsumer</module>
        </modules>
    
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.1.RELEASE</version>
        </parent>
    
        <properties>
            <javaVersion>1.8</javaVersion>
        </properties>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Camden.SR6</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
    
        </dependencyManagement>
    
    
    
    </project>

    eureak  pom文件:

    <?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">
        <parent>
            <artifactId>springcloudtest</artifactId>
            <groupId>com.mytest</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>eureak</artifactId>
    
    
        <dependencies>
            <!-- 引入eureka server依赖 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka-server</artifactId>
            </dependency>
    
        </dependencies>
    </project>

    consumer  pom文件:

    <?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">
        <parent>
            <artifactId>springcloudtest</artifactId>
            <groupId>com.mytest</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>hello-consumer</artifactId>
    
        <dependencies>
            <!-- 引入eureka 客户端依赖 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
            <!-- 引入ribbon 依赖 ,用来实现负载均衡,我们这里只是使用,先不作其他介绍-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-ribbon</artifactId>
            </dependency>
    
        </dependencies>
    </project>

    service  pom文件:

    <?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">
        <parent>
            <artifactId>springcloudtest</artifactId>
            <groupId>com.mytest</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>hello-service</artifactId>
    
        <dependencies>
            <!-- 引入eureka 客户端依赖 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
    
        </dependencies>
    
    </project>

    关于同一个service启动两个不同端口:

    1  先启动service,然后选择 edit configuration,配置如下:

    2  修改application.properties中的port端口

    3  启动就可以了。

    结果如下:

    访问页面:

    PS:

    如果controller和action都设置了@RequestMapping("/hello")

    地址是:http://localhost:9090/hello/hello

    如果只有action设置了@RequestMapping("/hello")

    地址是:http://localhost:9090/hello

    ConsumerApp中,

    如果controller和action都设置了@RequestMapping("/hello")

    restTemplate.getForObject("http://hello-service/hello/hello", String.class);

    如果只有action设置了@RequestMapping("/hello")

    restTemplate.getForObject("http://hello-service/hello", String.class);

    三,spring cloud 入门系列三:使用Eureka 搭建高可用服务注册中心

    功能:分布式

    这个目前没测试。

    四,spring cloud 入门系列四:使用Hystrix 实现断路器进行服务容错保护

    功能:熔断措施

    跟着教程配置service层,然后写入对应代码,重点是下面的注解。

    @SpringCloudApplication = @EnableDiscoveryClient +@SpringBootApplication+@EnableCircuitBreaker
    @HystrixCommand(fallbackMethod = "errorMsg")

    值得注意的是运行顺序:

    ①  eureka-->helloservice1-->helloservice2-->consumer

    ②单独关闭helloservice2

    ③多次刷新页面,会出现  error!!!  字符串

    五,spring cloud 入门系列五:使用Feign 实现声明式服务调用

    功能:分布式和熔断措施

    照着文章来 ,没什么问题。

    不过这里要注意几点:

    先看一下model结构:

    1  首先我创建了一个 model层。所以每个运用到model的项目都要引入pom文件。

    下面这个是我的feignconsumer的pom,其中model就是我创建的

    <?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">
        <parent>
            <artifactId>springcloudtest</artifactId>
            <groupId>com.mytest</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>feign-consumer</artifactId>
    
        <dependencies>
            <!--引入model-->
            <dependency>
                <groupId>com.mytest</groupId>
                <artifactId>model</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
    
            <!-- 引入eureka 客户端依赖 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
            <!-- 引入feign 依赖 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-feign</artifactId>
            </dependency>
    
        </dependencies>
    </project>

    2  路由的controller问题

    如果前面我们在hello-service中的controller和action上设定了

    @RequestMapping("/hello")

    那么在feignConsumerService中,记得要带上controller的路由

    我的是如下:@RequestMapping("/hello/hello")

    package com.mytest.service;
    
    import com.mytest.User;
    import org.springframework.cloud.netflix.feign.FeignClient;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * Created by Tyler on 2018/8/27
     */
    @FeignClient(name="hello-service")
    public interface FeignConsumerService{
    
        @RequestMapping("/hello/hello")
        public void hello();
    
        @RequestMapping("/hello/hello1")
        public void hello1(@RequestBody User user);
    }

    3  我尝试了使用重载,既

    @RequestMapping("/hello/hello")
    public void hello();
    
    @RequestMapping("/hello/hello")
    public void hello(@RequestBody User user);
    

    可是这样是错误的。

    现阶段项目下载地址:(运行--eurake--helloservice--helloservice1--feignconsumer)

    https://pan.baidu.com/s/1vjNM_Ud0D53U7LJEScidXw

    六,spring cloud 入门系列六:使用Zuul 实现API网关服务

    功能:设置路由规则

    1,构建网关,配置路由

    没什么问题,照着做就好了。

    启动顺序:eureka---helloservice---feignconsumer--apigetway

    访问地址:http://localhost:5555/api-a/feign-consumer

    2,面向服务的路由

    我的hello-service中设置了controller的@RequestMapping("/hello/hello"),所以地址如下

    访问地址:http://localhost:5555/api-a/hello/hello

    3,服务路由的默认规则

    这里要访问:hello-service和feign-consumer的路径分别如下

    http://localhost:5555/api-a/hello/hello

    http://localhost:5555/api-b/feign-consumer

    配置如下:

    server.port=5555
    spring.application.name=api-gateway
    
    zuul.routes.api-a.path=/api-a/**
    #这里用serviceId代替url,用服务名代替ip+端口号
    zuul.routes.api-a.serviceId=hello-service
    
    zuul.routes.api-b.path=/api-b/**
    #这里用serviceId代替url,用服务名代替ip+端口号
    zuul.routes.api-b.serviceId=feign-consumer
    eureka.client.service-url.defaultZone=http://localhost:1111/eureka

    现阶段项目下载地址:

    https://pan.baidu.com/s/1D-ui1dMEHCAGsHWIrb94mQ

    七,spring cloud 入门系列七:基于Git存储的分布式配置中心--Spring Cloud Config

    首先,需要学习Git -->  Git学习

  • 相关阅读:
    爬虫案例
    伪静态
    HTTP0.9、HTTP1.0、HTTP1.1、HTTP2的区别
    正向代理和反向代理
    数据结构继承
    APP 爬虫
    算法基础
    matplotlib
    Java类加载机制及自定义加载器
    SpringBoot war包部署到Tomcat服务器
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/9779698.html
Copyright © 2011-2022 走看看