SpringCloud:SpringBoot整合SpringCloud项目

划分模块

这里我划分了四个模块

 

Common: 存放bean和Dao模块

Consumer: 消费者模块,提供对外暴露接口服务

EurekaServer: Eureka注册中心模块,主要用于启动注册中心

Provider: 提供者模块,提供业务实现给消费者调用

 

依赖jar包

整合boot+cloud项目Maven依赖jar包,由于所有的bean都在Common模块内,所以我把jar包依赖全部放在此模块内,其他模块只需要引用即可。

<?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">
    <artifactId>Common</artifactId>
    <groupId>com.boot</groupId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <modelVersion>4.0.0</modelVersion>
    <name>Common</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <mybatis-spring-boot>1.3.2</mybatis-spring-boot>
    </properties>

    <!-- SpringCloud-dependencies版本管理 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-spring-boot}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.5</version>
        </dependency>
        <!-- Springboot-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Springboot-aop -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <!-- Springboot-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Springboot-jdbc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- Springboot-activemq监控包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
        </dependency>
        <!-- SpringCloud-eureka注册中心 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <!-- SpringCloud-eureka注册中心 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <!-- SpringCloud-feign服务调用 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <!-- SpringCloud -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
            <version>1.3.5.RELEASE</version>
        </dependency>

    </dependencies>

</project>

 博主在整合生产版本时,遇到很多问题,故整理如下jar包依赖pom文件,供大家参考。

 <dependencies>

        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-spring-boot}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.5</version>
        </dependency>

        <!-- Springboot-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Springboot-aop -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

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

        <!-- Springboot-jdbc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!-- SpringCloud-eureka注册中心 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.3.4.RELEASE</version>
        </dependency>

        <!-- SpringCloud-netflix 核心包 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-core</artifactId>
            <version>1.4.4.RELEASE</version>
        </dependency>

        <!-- SpringCloud-feign服务调用 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

        <!-- SpringCloud-feign服务调用 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
            <version>1.3.4.RELEASE</version>
        </dependency>

        <!-- SpringCloud -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
            <version>1.3.5.RELEASE</version>
        </dependency>


    </dependencies>        

 

 

创建注册中心启动类

引用Common依赖jar包

<dependencies>
        <dependency>
            <groupId>com.boot</groupId>
            <artifactId>Common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

编写启动类

package com.boot.eurekaServer;

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


@EnableEurekaServer
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringBootApplication
public class EurekaServerApplication {

    public static void main(String[] args){
        System.out.println(">>>>>>>>>>>>>>> 启动EurekaServer <<<<<<<<<<<<<<<");
        SpringApplication.run(EurekaServerApplication.class, args);
        System.out.println(">>>>>>>>>>>>>>> 运行EurekaServer成功 <<<<<<<<<<<<<<<");
    }
}

@EnableEurekaServer表示用来激活Eureka Server相关的配置,启动注册中心

application.properties配置文件:

#服务端口号
server.port=8001
#服务名称
spring.application.name=eureka-server
#服务地址
eureka.instance.hostname=localhost
#禁用eureka注册自己
#Eureka是为注册中心,是否需要将自己注册到注册中心上(默认为true),
#本次位单机部署,不需要设置为true;但是注册中心集群时候必须为true;因为集群时,其他Eureka需要从此Eureka中的节点上获取数据;
eureka.client.register-with-eureka=false
#Erueka是为注册中心,不需要检索服务信息;
#(表示是否从Eureka Server获取注册信息,默认为true 如果这是一个单点的 Eureka Server,不需要同步其他节点的数据,可以设为false)
eureka.client.fetch-registry=false
#关闭保护机制
eureka.server.enable-self-preservation=false
#标注其它服务注册的目标地址
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

重点: 

该警告是触发了Eureka Server的自我保护机制。Eureka Server在运行期间,会统计心跳失败的比例在15分钟之内是否低于85%,如果低于,就会将当前实例注册信息保护起来,让实例不会过期,尽可能保护这些注册信息。但是如果在保护期间,实例出现问题,那么客户端很容易拿到实际已经不存在的服务实例,会出现调用失败。这个时候客户端的容错机制就很重要了。(重新请求,断路器)保护机制,可能会导致服务实例不能够被正确剔除,可以关掉保护机制。

 

 

创建提供者启动类

引用Common依赖jar包

<dependencies>
        <dependency>
            <groupId>com.boot</groupId>
            <artifactId>Common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

编写启动类

package com.boot.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;


/**
 * 
 * @EnableEurekaClient : 负责与Eureka Server 配合向外提供注册与发现服务接口
 */
@EnableEurekaClient
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringBootApplication
public class ProviderServerApplication {

    public static void main(String[] args){
        System.out.println(">>>>>>>>>>>>>>> 启动ProviderServer <<<<<<<<<<<<<<<");
        SpringApplication.run(ProviderServerApplication.class, args);
        System.out.println(">>>>>>>>>>>>>>> 运行ProviderServer成功 <<<<<<<<<<<<<<<");
    }
}

编写业务实现类

package com.boot.provider.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *Feign远程服务调用与正常暴露接口是一样的,所以我们只需要仿照接口暴露编写即可
 */
@RestController
@RequestMapping("/Provider")
public class ProviderServerController {

    @RequestMapping("/gotoAlgorithmServer")
    public String gotoAlgorithmServer(){
        return "调用Provider成功";
    }
}

application.properties配置文件:

#指定提供者名称(消费者通过此名称远程访问服务)
spring.application.name=eureka-provider
#端口号
server.port=8003
#服务注册地址(注册中心的服务注册地址)
eureka.client.serviceUrl.defaultZone = http://localhost:8001/eureka/

 

创建消费者启动类

引用Common依赖jar包

<dependencies>
        <dependency>
            <groupId>com.boot</groupId>
            <artifactId>Common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

编写启动类

package com.boot.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

/**
 * @EnableFeignClients 启动feign客户端
 * @EnableEurekaClient 负责与 Eureka Server 配合向外提供注册与发现服务接口
 */
@EnableFeignClients
@EnableEurekaClient
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringBootApplication
public class ConsumerServerApplication {

    public static void main(String[] args){
        System.out.println(">>>>>>>>>>>>>>> 启动ConsumerServer <<<<<<<<<<<<<<<");
        SpringApplication.run(ConsumerServerApplication.class, args);
        System.out.println(">>>>>>>>>>>>>>> 运行ConsumerServer成功 <<<<<<<<<<<<<<<");
    }
}

编写暴露接口服务类

package com.boot.consumer.controller;

import com.boot.consumer.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/Consumer")
public class ConsumerServerController {

    @Autowired
    public ConsumerService consumerService;

    @RequestMapping("/gotoAlgorithmServer")
    public String gotoAlgorithmServer(){
        return consumerService.gotoAlgorithmServer();
    }
}

编写Feign调用接口

package com.boot.consumer.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *@FeignClient : feign客户端
 * name属性 :提供者的服务名称
 */
@FeignClient(name = "eureka-provider")
public interface ConsumerService {

    @RequestMapping(method = RequestMethod.GET ,path = "/Provider/gotoAlgorithmServer")
    String gotoAlgorithmServer();
}

application.properties配置文件:

#消费者名称
spring.application.name=eureka-consumer
#端口
server.port=8002
#注册中心地址
eureka.client.serviceUrl.defaultZone = http://localhost:8001/eureka/
#设置连接超时
feign.client.config.default.connect-timeout = 10000
#设置读取超时
feign.client.config.default.read-timeout = 600000

 

测试

 

 

成功!

 

posted @ 2020-03-23 22:56  怒吼的萝卜  阅读(12034)  评论(0编辑  收藏  举报