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
测试
成功!
SpringCloud:SpringBoot整合SpringCloud项目的更多相关文章
- SpringBoot整合SpringCloud搭建分布式应用
什么是SpringCloud? SpringCloud是一个分布式的整体解决方案.SpringCloud为开发者提供了在分布式系统中快速构建的工具,使用SpringCloud可以快速的启动服务或构建应 ...
- springboot 整合 web 项目找不到 jsp 文件
今天遇到一个问题,就是使用springboot整合web项目的时候,怎么都访问不到 \webapp\WEB-INF\jsp\index.jsp 页面.这个问题搞了半天,试了各种方式.最后是因为在启动的 ...
- springBoot 整合 mybatis 项目实战
二.springBoot 整合 mybatis 项目实战 前言 上一篇文章开始了我们的springboot序篇,我们配置了mysql数据库,但是我们sql语句直接写在controller中并且使用 ...
- 实践丨SpringBoot整合Mybatis-Plus项目存在Mapper时报错
摘要:在SpringBoot运行测试Mybatis-Plus测试的时候报错的问题分析与修复 本文分享自华为云社区<SpringBoot整合MybatisPlus项目存在Mapper时运行报错的问 ...
- 使用SpringBoot整合ssm项目
SpringBoot是什么? Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程. Spring Boot 现在已经成为Java ...
- 33、springboot整合springcloud
Spring Cloud Spring Cloud是一个分布式的整体解决方案.Spring Cloud 为开发者提供了在分布式系统 (配置管理,服务发现,熔断,路由,微代理,控制总线,一次性token ...
- 二、springBoot 整合 mybatis 项目实战
前言 上一篇文章开始了我们的springboot序篇,我们配置了mysql数据库,但是我们sql语句直接写在controller中并且使用的是jdbcTemplate.项目中肯定不会这样使用,上篇文章 ...
- SpringBoot 整合 MybatisPlus (项目的创建及简单的单表查询)
添加依赖 <!--mybatis-plus的springboot支持--> <dependency> <groupId>com.baomidou</group ...
- Java-SpringBoot整合SpringCloud
SpringBoot整合SpringCloud 1. SpringCloud特点 SpringCloud专注于为典型的用例和扩展机制提供良好的开箱即用体验,以涵盖其他情况: 分布式/版本化配置 服务注 ...
随机推荐
- KVM虚拟化存储管理(4)
一.KVM存储虚拟化介绍 KVM 的存储虚拟化是通过存储池(Storage Pool)和卷(Volume)来管理的. Storage Pool 是宿主机上可以看到的一片存储空间,可以是多种型: Vol ...
- docker,docker-compose,harbor安装
安装docker-ce 下载docker-ce.repo: wget -O /etc/yum.repos.d/docker-ce.repo https://download.docker.com/li ...
- STM32独立看门狗(IWDG)
造成程序跑飞,只是程序的正常运行状态被打断而进入死循环,从而使单片机控制的系统无法正常工作.看门狗就是一种专门用于检测单片机程序运行状态的硬件结构. STM32内部自带了两个看门狗,独立看门狗(IWD ...
- Assignment Problem的若干思考
最近受到南京一个同学的push,又开始了博客园写作之旅.欢迎大家联系我做代码实现工作,QQ:1198552514.权当赚点生活费~ 我的研究也经常用的Assignment problem,而且很多 ...
- WEB页面下载内容导出excel
internal class DownloadHandler : IDownloadHandler { public DownloadHandler() { ...
- System Verilog MCDF(一)
- [论文阅读笔记] Are Meta-Paths Necessary, Revisiting Heterogeneous Graph Embeddings
[论文阅读笔记] Are Meta-Paths Necessary? Revisiting Heterogeneous Graph Embeddings 本文结构 解决问题 主要贡献 算法原理 参考文 ...
- 在gitlab网页上合并分支
在gitlab网页上合并分支 使用gitlab网页将代码合并分 下面将dev分支代码合并至master 1.点击request merge 2.源分支为当前分支,目标分支默认为master,确认无误, ...
- python批量向kafka塞数据
python批量向kafka塞数据 from kafka import KafkaClient from kafka.producer import SimpleProducer from kafka ...
- Spring Cloud系列(七):消息总线
在上一篇中,当一个配置中心的客户端启动之后,它所引用的值就无法改变了,但是Spring Cloud 提供了一种手段去解决了这个问题--Spring Cloud Bus. 一.Spring Cloud ...