微服务是现在比较流行的技术,对于程序猿而言,了解并搭建一个基本的微服务框架是很有必要滴。

微服务包含的内容非常多,一般小伙伴们可以根据自己的需求不断添加各种组件、框架。

一般情况下,基本的微服务框架包含:框架:注册中心、负载均衡、声明式服务(feign)、容错(hystrix)、网关(权限)gateway 和 配置(resource)

注册中心:现在比较常用的有eureka、nacos

负载均衡:包括feign、ribbon等技术,相关对比可以参考另一位老哥的博客:《负载均衡之feign与ribbon对比》

服务间调用:包括resttemplate、feign等等

熔断:hystrix

网关:gateway,(zuul已经逐渐被弃用)

配置中心:config server 、 config clent

本章主要采用 eureka+feign+hystrix+gateway+config 的组合搭建一个基础框架,环境采用idea。

注册中心

首先新建一个spring工程,spring-boot版本可以选择, 2.1.9.RELEASE。

在新建的project上右键,新建module,选择Spring Initializr

下一步,设置对应的包名,artifact名称,这些可以根据自己的项目需要自己命名。

next,

然后下一步,完成即可。这种方式是直接将Spring管网上对应的module下载到你的项目中,非常方便。

然后再application.yml文件中设置你对应的注册中心的端口,访问方式等信息。

server:
port: 8761 eureka:
instance: hostname: localhost
client:
#自己是注册中心,不需要注册自己
registerWithEureka: false
#自己是注册中心不需要发现服务
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

在启动类中加入对应注解:

@SpringBootApplication
//声明自己是注册中心
@EnableEurekaServer
public class HsddEurekaApplication { public static void main(String[] args) {
SpringApplication.run(HsddEurekaApplication.class, args);
} }

  

业务逻辑服务

业务逻辑服务可以按功能或逻辑进行分类,分成多个微服务来提供,保证整体的可用性。

在project中新建一个目录,比如,service目录。将业务逻辑相关的微服务都放到这里面。

新建spring boot modul,同上面一样,不过可以不选spring cloud配置项。

然后再main.java下面创建对应的各层目录,这里没啥好说的。

在application启动类中,我们需要添加对应注解,

package com.fencer.userdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication
@EnableEurekaClient  #注册到注册中心
@EnableFeignClients  #服务间负载均衡与声名式服务
public class UserDemoApplication { public static void main(String[] args) {
SpringApplication.run(UserDemoApplication.class, args);
} } 

在业务逻辑微服务中,需要配置application.yml,需要将你的服务注册到注册中心,才能被发现调用

#服务端口
server:
port: 8762 spring:
application:
name: user-demo #注册的服务名称
profiles:
active: dev
devtools:
restart:
enabled: true
trigger-file: devtools.tg #注册中心地址
eureka:
instance:
prefer-ip-address: true
instance-id: 127.0.0.1:${server.port}
hostname: localhost
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
register-with-eureka: true
fetch-registry: true

  

服务间声明式调用及熔断

服务间声明式调用及熔断比较简单,

新建一个业务逻辑服务,在启动类中加入feign、hystrix相关注解

package com.fencer.orderdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication
@EnableEurekaClient
@EnableFeignClients  
@EnableHystrix //熔断
public class OrderDemoApplication { public static void main(String[] args) {
SpringApplication.run(OrderDemoApplication.class, args);
} }

  创建一个调用对象的接口,接口与服务提供方,方法名、参数完全一致

package com.fencer.orderdemo.service;

import com.fencer.orderdemo.fallback.MyFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; /**
* @author :
* @version V1.0
* @Project: hsdd
* @Package com.fencer.orderdemo.service
* @Description: TODO
* @date Date : 2019年10月23日 19:49
*/
@FeignClient(value = "user-demo", fallback = MyFallback.class)
public interface UserDemoFeignService { @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
String getUser(@PathVariable("id") int id); @RequestMapping(value = "/hi", method = RequestMethod.GET)
String home(@RequestParam String name);
}

  添加统一降级处理方法,在服务提供方没有响应时进行处理。

package com.fencer.orderdemo.fallback;

import com.fencer.orderdemo.service.UserDemoFeignService;
import org.springframework.stereotype.Component; /**
* @author :
* @version V1.0
* @Project: hsdd
* @Package
* @Description: TODO
* @date Date : 2019年10月24日 10:46
*/
@Component
public class MyFallback implements UserDemoFeignService { // 添加服务降级处理方法
@Override
public String getUser(int id) {
return "error getUser";
} @Override
public String home(String name) {
return "error home Method";
}
}

  在application.yml中配置feign相关配置项

#服务启动端口号
server:
port: 8763
#服务名称
spring:
application:
name: order-demo #服务注册到eureka服务端地址
eureka:
instance:
prefer-ip-address: true
# instance-id: 127.0.0.1:${server.port}
# hostname: localhost
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
register-with-eureka: true
fetch-registry: true feign:
hystrix:
enabled: true #允许开启hystrix功能
compression:
request:
enabled: true #开启请求压缩
mime-types: text/html,application/xml,application/json # 设置压缩的数据类型
min-request-size: 2048 # 设置触发压缩的大小下限
response:
enabled: true #开启响应压缩
client:
config:
default:
connectTimeout: 10000 #连接超时时间(ms)
readTimeout: 10000 # 通信超时时间(ms) #ribbon:
# ConnectTimeout: 5000 # 连接超时时间(ms)
# ReadTimeout: 5000 # 通信超时时间(ms)
# OkToRetryOnAllOperations: true # 是否对所有操作重试
# MaxAutoRetriesNextServer: 1 # 同一服务不同实例的重试次数
# MaxAutoRetries: 1 # 同一实例的重试次数

 

网关gateway

新建一个网关服务

在pom中需要加入对应依赖

<!-- 引入gateway网关依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency> <!-- 添加eureka注册中心客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> <!-- 添加熔断依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency> <!-- 监控中心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

  网关的重点在yml配置文件中,

server:
port: 8081 spring:
application:
name: hsdd-gateway
cloud:
gateway:
discovery:
locator:
##gateway开启服务注册和发现的功能
enabled: true
##将请求路径上的服务名配置为小写
lower-case-service-id: true #eureka注册地址
eureka:
instance:
prefer-ip-address: true
instance-id: 127.0.0.1:${server.port}
hostname: localhost
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ logging:
level:
org.springframework.cloud.gateway: debug
org.springframework.http.server.reactive: debug
org.springframework.web.reactive: debug
reactor.ipc.netty: debug feign:
hystrix:
enabled: true

  

spring:
cloud:
gateway:
routes:
- id: test
uri: http://www.ityouknow.com/springcloud
predicates:
- Path=/user/**

  以上两种方式都可以进行网关配置。第一种时自动发现注册服务,服务名为你自己命名的微服务名称

第二种,需要手动配置,在Path中配置上关键字,uri为对应的服务名

简单Spring Cloud 微服务框架搭建的更多相关文章

  1. 【spring colud】spring cloud微服务项目搭建【spring boot2.0】

    spring cloud微服务项目搭建 =================================== 示例版本: 1.spring boot 2.0版本 2.开发工具 IntellJ IDE ...

  2. 浅谈现公司的Spring Cloud微服务框架

    目录 说在前面 服务注册与发现 服务网关及熔断 配置中心 消息中心.服务链路追踪 小言 说在前面 本文偏小白,大佬慎入,若有错误或者质疑,欢迎留言提问,谢谢,祝大家新年快乐. spring cloud ...

  3. Spring Cloud 微服务中搭建 OAuth2.0 认证授权服务

    在使用 Spring Cloud 体系来构建微服务的过程中,用户请求是通过网关(ZUUL 或 Spring APIGateway)以 HTTP 协议来传输信息,API 网关将自己注册为 Eureka ...

  4. Spring Cloud微服务框架介绍

    Spring Cloud为开发人员提供了一整套的快速构建分布式应用的工具,入服务注册.服务发现.熔断.负载均衡.路由等,提供了开箱即用的各种依赖以及良好的可扩展机制. 目前在Spring Cloud的 ...

  5. Spring Cloud微服务体系搭建

    前期架构设计图: 参考博文: Eureka相关: Eureka注册与发现(高可用注册中心.注册服务.Feign服务调用):https://blog.csdn.net/qq_32529383/artic ...

  6. Spring Cloud微服务系列文,服务调用框架Feign

    之前博文的案例中,我们是通过RestTemplate来调用服务,而Feign框架则在此基础上做了一层封装,比如,可以通过注解等方式来绑定参数,或者以声明的方式来指定请求返回类型是JSON.    这种 ...

  7. 只需五分钟-用Maven快速搭建Spring Cloud微服务

    Maven安装手册 1.准备安装包 安装包: apache-maven-3.5.4-bin.zip  (最好JDK 1.7及以上版本) 集成包: eclipse-maven3-plugin.zip 2 ...

  8. Spring Cloud微服务(一):公共模块的搭建

    本demo后台采用spring cloud微服务,前端选用vue,进行前后端分离搭建.具体项目见git:光头才能强 创建文件夹,并分别创建以下jar工程 创建公共模块(后续有需要,还会增加).无论是d ...

  9. Spring Cloud微服务中网关服务是如何实现的?(Zuul篇)

    导读 我们知道在基于Spring Cloud的微服务体系中,各个微服务除了在内部提供服务外,有些服务接口还需要直接提供给客户端,如Andirod.IOS.H5等等. 而一个很尴尬的境地是,如果直接将提 ...

随机推荐

  1. 修改apache2配置,禁止目录访问+禁止访问.git文件夹

    通过url访问服务器,无论是本地服务器还是远程服务器 如果你的文件根目录里有 index.html,index.php,浏览器就会显示 index.html的内容,如果没有 index.html,浏览 ...

  2. vue 全局引用jq(打包后可能会遇到的问题)

    问题描述:全局引用jquery打包到线上可能会不好使. 第一步: var path = require('path') var webpack = require('webpack') functio ...

  3. pdf.js浏览中文pdf乱码的问题解决

    由于项目中需要支持移动设备在线浏览pdf,苹果还好,天生支持,但是安卓中就不行了,需要第三方组件的支持. 这里就找到了pdf.js,由于pdf数据太多,开始的时候没法一一测试,所以随便测试打开了几篇没 ...

  4. 解决Python print输出不换行没空格的问题

    今天在做编程题的时候发现Python的print输出默认换行输出,并且输出后有空格. 题目要求输出 122 而我的输出是: 1 2 2 于是我百度查到取消print自动换行的方法:就是在print的值 ...

  5. Django学习之Cookie和Session

    一.Cookie 1.Cookie的由来 2.什么是Cookie 3.Cookie的原理 4.查看Cookie 二.Django中操作Cookie 1.获取Cookie 2.设置Cookie 3.删除 ...

  6. windows安装程序制作

    作为一个学计算机的,现在才知道那些安装软件都是用软件封装工具封装起来的. 我们写好exe以后可以下载一个Inno setup5 对其打包成可安装的软件,期间可加入图标,readme,等等一些东西.

  7. Java ——集合框架 list lambda set map 遍历方法 数据结构

    本节重点思维导图 集合框架 有序无序:元素放入的顺序与取出的顺序是否一致,一致即为有序,不一致即无序. List:允许重复.有序 ArrayList:长度可变的数组,遍历速度快 LinkedList: ...

  8. C++——堆、栈与内存管理

    简介 Stack,是存在于某作用域(scope) 的一块内存空间(memory space).例如当你调用函数,函数本身即会形成一个stack 用來放置它所接收的参数,以及返回地址.在函数本体(fun ...

  9. 20191128 Spring Boot官方文档学习(9.10)

    9.10.数据库初始化 可以使用不同的方式初始化SQL数据库,具体取决于堆栈是什么.当然,如果数据库是一个单独的进程,您也可以手动执行.建议使用单一机制进行模式生成. 9.10.1.使用JPA初始化数 ...

  10. “EndExecuteNonQuery”方法没有任何重载采用“0”个参数

    EndExecuteNonQuery需要参数IAsyncResult asyncResult myCmd.ExecuteNonQuery();//执行 ExecuteNonQuery 返回受影响行数