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专注于为典型的用例和扩展机制提供良好的开箱即用体验,以涵盖其他情况: 分布式/版本化配置 服务注 ...
随机推荐
- IT菜鸟之计算机硬件
现在的人们几乎无时无刻都会碰到计算机!不管是桌面计算机.笔记本电脑.平板计算机.智能型手机等等,这些东西都算计算机.虽然接触的怎么多,但是,我们一般很少会专门了解计算机内部的构成,下面就是自己在听课结 ...
- Centos7 网卡DHCP重新获取IP地址
问题:局域网内一台linux系统(Centos7.4)DHCP自动获取的IP地址和另一台手动配置的静态IP冲突了 解决方法:让DHCP自动获取的IP地址重新获取一个别的IP地址 DHCP重新获取IP ...
- tomcat---starup.bat点击窗口自动关闭
- Redis持久化锦囊在手,再也不会担心数据丢失了
大家好,我是小羽. Redis 的读写都是在内存中进行的,所以它的性能高.而当我们的服务器断开或者重启的时候,数据就会消失,那么我们该怎么解决这个问题呢? 其实 Redis 已经为我们提供了一种持久化 ...
- python异步编程之asyncio
python异步编程之asyncio 前言:python由于GIL(全局锁)的存在,不能发挥多核的优势,其性能一直饱受诟病.然而在IO密集型的网络编程里,异步处理比同步处理能提升成百上千倍的效率, ...
- 激光雷达Lidar Architecture and Lidar Design(上)
激光雷达Lidar Architecture and Lidar Design(上) 介绍 激光雷达结构: 基本条件 构型和基本布置 激光雷达设计: 基本思想和基本原则 总结 介绍 激光雷达结构是激光 ...
- Spring Cloud系列(七):消息总线
在上一篇中,当一个配置中心的客户端启动之后,它所引用的值就无法改变了,但是Spring Cloud 提供了一种手段去解决了这个问题--Spring Cloud Bus. 一.Spring Cloud ...
- 基于Android平台的图书管理系统的制作(4)
讲解完学生.职员.书籍这些基础层之后,我们可以来了解一些应用层的活动. 新书上架.借阅排行.黑名单.图书馆介绍.图书馆新闻. 新书上架是查询数据库里的Book表,将最近注册的五本书的基本信息(若图书馆 ...
- 【NX二次开发】Block UI 切换开关
属性说明 常规 类型 描述 BlockID String 控件ID Enable Logical 是否可操作 Group Logical ...
- GoLang:通过url将值从view层(.tpl)传递到controller层
beego框架 1.定义路由: beego.Router("/UpdateState/:statename/:id", &controllers.ContentContro ...