Spring Boot搭建简易spring clound框架 (一)

1.搭建之前先了解微服务

微服务架构介绍

微服务架构(Microservice Architecture)是一种架构概念,旨在通过将功能分解到各个离散的服务中以实现对解决方案的解耦。你可以将其看作是在架构层次而非获取服务的
类上应用很多SOLID原则。微服务架构是个很有趣的概念,它的主要作用是将功能分解到离散的各个服务当中,从而降低系统的耦合性,并提供更加灵活的服务支持。
概念:把一个大型的单个应用程序和服务拆分为数个甚至数十个的支持微服务,它可扩展单个组件而不是整个的应用程序堆栈,从而满足服务等级协议。
定义:围绕业务领域组件来创建应用,这些应用可独立地进行开发、管理和迭代。在分散的组件中使用云架构和平台式部署、管理和服务功能,使产品交付变得更加简单。
本质:用一些功能比较明确、业务比较精练的服务去解决更大、更实际的问题。

概念是这样,下面这个图帮大家理解一下,每个业务都独立出来

现在主流的微服务架构有spring cloud和Dubbo,下面对比一下两个架构

Spring Cloud抛弃了Dubbo的RPC通信,采用的是基于HTTP的REST方式。严格来说,这两种方式各有优劣。虽然从一定程度上来说,后者牺牲了服务调用的性能,但也避免了上面提到的原生RPC带来的问题。而且REST相比RPC更为灵活,服务提供方和调用方的依赖只依靠一纸契约,不存在代码级别的强依赖,这在强调快速演化的微服务环境下,显得更加合适。

很明显,Spring Cloud的功能比DUBBO更加强大,涵盖面更广,而且作为Spring的拳头项目,它也能够与Spring Framework、Spring Boot、Spring Data、Spring Batch等其他Spring项目完美融合,这些对于微服务而言是至关重要的。前面提到,微服务背后一个重要的理念就是持续集成、快速交付,而在服务内部使用一个统一的技术框架,显然比把分散的技术组合到一起更有效率。更重要的是,相比于Dubbo,它是一个正在持续维护的、社区更加火热的开源项目,这就保证使用它构建的系统,可以持续地得到开源力量的支持。

介绍就就到这了,其余大家网上搜搜,要详细了解可以看下这篇介绍链接
https://blog.csdn.net/itjiandingshi/article/details/79756133

2.搭建spring cloud项目

官方文档,里面有详细介绍spring cloud中的插件 https://springcloud.cc/
这边主要介绍以下几个
Eureka:各个服务启动时,Eureka Client都会将服务注册到Eureka Server,并且Eureka Client还可以反过来从Eureka Server拉取注册表,从而知道其他服务在哪里
Ribbon:服务间发起请求的时候,基于Ribbon做负载均衡,从一个服务的多台机器中选择一台
Feign:基于Feign的动态代理机制,根据注解和选择的机器,拼接请求URL地址,发起请求
Hystrix:发起请求是通过Hystrix的线程池来走的,不同的服务走不同的线程池,实现了不同服务调用的隔离,避免了服务雪崩的问题
Zuul:如果前端、移动端要调用后端系统,统一从Zuul网关进入,由Zuul网关转发请求给对应的服务

这篇博客主要先采用Eureka搭建服务中心,并采用Ribbon做通信

1)搭建Eureka服务中心

这边采用spring boot 2.1.x版本(gradle版本)
首先先创建spring boot项目(自行创建)
然后在修改build.gradle

plugins {id 'org.springframework.boot' version '2.1.3.RELEASE'id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'lin29'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {mavenCentral()maven { url 'https://repo.spring.io/milestone' }
}
ext {set('springCloudVersion', 'Greenwich.SR1')
}
dependencies {implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'testImplementation 'org.springframework.boot:spring-boot-starter-test'compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')
}
dependencyManagement {imports {mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"}
}

修改后重新加载jar包,并修改启动类

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {public static void main(String[] args) {SpringApplication.run(EurekaApplication.class, args);}
}

修改application.yml文件
,Eureka会将自己也作为客户端尝试注册,所以在单机模式下,我们需要禁止该行为registerWithEureka,fetchRegistry需要设置为false

  server:port: 8761                    # 指定该Eureka实例的端口eureka:instance:hostname: discovery         # 指定该Eureka实例的主机名client:registerWithEureka: false   fetchRegistry: falseserviceUrl:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

到这,配置完成,启动项目,访问localhost:8761

2) 创建业务模块

创建springboot项目修改build.gradle文件

plugins {id 'org.springframework.boot' version '2.1.3.RELEASE'id 'java'
}apply plugin: 'io.spring.dependency-management'group = 'lin29'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'repositories {mavenCentral()
}ext {set('springCloudVersion', 'Greenwich.SR1')
}dependencies {implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.0'runtimeOnly 'mysql:mysql-connector-java:8.0.15'testImplementation 'org.springframework.boot:spring-boot-starter-test'implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')compile("org.springframework.boot:spring-boot-starter-web")compile("org.springframework.boot:spring-boot-starter-actuator")compile("org.springframework.boot:spring-boot-starter-jdbc")}dependencyManagement {imports {mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"}
}

修改启动类

package com;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication
@EnableDiscoveryClient
public class BusApplication {public static void main(String[] args) {SpringApplication.run(BusApplication.class, args);}
}

修改application.yml文件

server:port: 8000
spring:application:name: user    # 项目名称尽量用小写jpa:generate-ddl: falseshow-sql: truehibernate:ddl-auto: nonedatasource:                           # 指定数据源(mysql)url: jdbc:mysql://127.0.0.1:3306/demousername: rootpassword: 123456driver-class-name: com.mysql.jdbc.Driver
logging:level:root: INFOorg.hibernate: INFOorg.hibernate.type.descriptor.sql.BasicBinder: TRACEorg.hibernate.type.descriptor.sql.BasicExtractor: TRACEcom.itmuch.youran.persistence: ERROR
eureka:client:serviceUrl:defaultZone: http://${eureka.instance.hostname}:8761/eureka/    # 指定注册中心的地址instance:hostname: 127.0.0.1preferIpAddress: true

创建测试类

package com.controller;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class TestController {@RequestMapping("/test")public String test() {return "hello world";}
}

启动项目,访问localhost:8000/test

3)创建客户端

创建springboot项目,修改build.gradle文件

plugins {id 'org.springframework.boot' version '2.1.3.RELEASE'id 'java'
}apply plugin: 'io.spring.dependency-management'group = 'lin29'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'repositories {mavenCentral()
}ext {set('springCloudVersion', 'Greenwich.SR1')
}dependencies {implementation 'org.springframework.boot:spring-boot-starter-web'testImplementation 'org.springframework.boot:spring-boot-starter-test'testImplementation 'org.springframework.security:spring-security-test'implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')compile('org.springframework.cloud:spring-cloud-starter-netflix-ribbon')compile("org.springframework.boot:spring-boot-starter-actuator")
}dependencyManagement {imports {mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"}
}

修改启动类

package com;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;@SpringBootApplication
@EnableDiscoveryClient
public class WebApplication {/*** 实例化RestTemplate,通过@LoadBalanced注解开启均衡负载能力.* @return restTemplate*/@Bean@LoadBalancedpublic RestTemplate restTemplate() {return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(WebApplication.class, args);}}

配置application.yml文件

server:port: 8710
spring:application:name: web
eureka:client:serviceUrl:defaultZone: http://${eureka.instance.hostname}:8761/eureka/instance:hostname: 127.0.0.1preferIpAddress: true

写测试类RibbonController.java

package com.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import com.service.RibbonService;@RestController
public class RibbonController {@Autowiredprivate RibbonService service;@RequestMapping("/ribbon/test")public String ribbonTest() {return service.ribbonTest();}
}

RibbonService.java类

package com.service;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;@Service
public class RibbonService {@Autowiredprivate RestTemplate restTemplate;public String ribbonTest() {return this.restTemplate.getForObject("http://user/test", String.class);}
}

启动服务,查看localhost:8710/ribbon/test

至此,采用ribbon通信成功,这边没做负载均衡测试,可自行测试

后续博客也会陆续更新spring cloud其他相关组件的使用以及整合一些常用框架,redis,mybatis等,欢迎关注哈哈哈

Spring Boot搭建简易spring clound框架 (一)相关推荐

  1. 【Spring Boot】使用Spring Boot来搭建Java web项目以及开发过程

    [Spring Boot]使用Spring Boot来搭建Java web项目以及开发过程 一.Spring Boot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来 ...

  2. move_uploaded_file返回false但实际成功_023 Spring Boot 搭建实际项目开发框架

    前面的课程中,我主要给大家讲解了 Spring Boot 中常用的一些技术点,这些技术点在实际项目中可能不会全部用得到,因为不同的项目可能使用的技术不同,但是希望大家都能掌握如何使用,并能自己根据实际 ...

  3. Spring boot 搭建个人博客系统(二)——登录注册功能

    Spring boot 搭建个人博客系统(二)--登录注册功能 一直想用Spring boot 搭建一个属于自己的博客系统,刚好前段时间学习了叶神的牛客项目课受益匪浅,乘热打铁也主要是学习,好让自己熟 ...

  4. spring boot 搭建 和 全局异常处理

    spring boot 搭建: java -jar -Dserver.port=10000 -Dlogging.path=/var/logs xxx.jar &   -- 默认在/var/lo ...

  5. 在Spring Boot项目中使用Spock框架

    转载:https://www.jianshu.com/p/f1e354d382cd Spock框架是基于Groovy语言的测试框架,Groovy与Java具备良好的互操作性,因此可以在Spring B ...

  6. 使用 Spring Boot 快速构建 Spring 框架应用

    https://www.ibm.com/developerworks/cn/java/j-lo-spring-boot/index.html Spring 框架对于很多 Java 开发人员来说都不陌生 ...

  7. 使用 Spring Boot 快速构建 Spring 框架应用--转

    原文地址:https://www.ibm.com/developerworks/cn/java/j-lo-spring-boot/ Spring 框架对于很多 Java 开发人员来说都不陌生.自从 2 ...

  8. maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目

    项目的简单介绍: 项目采用maven聚合工程 用spring boot 搭建 spring cloud的微服务 模块式开发 项目的截图: 搭建开始: 能上图 我少打字 1.首先搭建maven的聚合工程 ...

  9. Spring Boot 搭建应用实现登陆实例,页面使用bootstrap

    2019独角兽企业重金招聘Python工程师标准>>> Spring boot 搭建web应用集成了thymeleaf模板实现登陆  下面是pom.xml的配置 <?xml v ...

最新文章

  1. ubuntu下Qt Creator使用valgrind检测内存泄漏
  2. poj1182 and 携程预赛2第一题 带权并查集
  3. android 一个字符串分两行显示_【Android】DataBindinglt;中gt;
  4. 系统间通信1:阻塞与非阻塞式通信A
  5. sql判断邮箱是否合法_分享一个oracle身份证校验函数,判断你的身份证是否合法...
  6. 微生物 研究_微生物监测如何工作,为何如此重要
  7. 删除数据清理oracle表空间,oracle数据库删除无用表空间及数据文件过程
  8. docker file 示例
  9. java war包更新 部署_关于Linux系统下基于Tomcat部署和升级war包的详细过程
  10. pycharm添加python_Pycharm 复制代码出现空格的解决方式
  11. 【CCCC】L2-028 秀恩爱分得快 (25分),模拟题
  12. Java出现The import javax.servlet cannot be resolved 的解决方法
  13. 全科初高中智能学习机器人_智能学习机器人推荐,阿尔法蛋大蛋2.0学习内容智能推荐...
  14. 网站开发流程(附图)
  15. 怎么查看笔记本内存条型号_笔记本如何加内存条之如何查看笔记本内存品牌和型号...
  16. [转] 宝宝出生第一年妈妈最应关心的问题
  17. 什么是哥德尔不完备定理?
  18. 关于数据库的操作语句
  19. 苹果手机 iTunes 资料备份到另一手机
  20. 2018年中国500强排行榜

热门文章

  1. 清华领军计划计算机试题,清华大学领军计划测试物理试题含答案
  2. 采集数据零点漂移问题解析
  3. 大学图书馆图书借阅管理系统
  4. html5常见使用的属性,HTML5常见五种常规全局属性
  5. IP地址、端口Port
  6. babylon.js 学习
  7. 按头安利 好看又实用的公文包 文件包3d模型素材看这里
  8. 计算机桌面上的公文包怎么加密,关于公文包同步的使用问题
  9. 无人机航拍高度与地面采样距离
  10. 企业与员工间的相互认同