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

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

一般情况下,基本的微服务框架包含:框架:注册中心、负载均衡、声明式服务(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. Ambari显示server 返回500 error

    Ambari server 搭建过程中到了revicw环境遇到点击deploy:发现页面没有响应 Console显示server 返回500 error错误,页面中没有提示更多的报错信息. 经过日志查 ...

  2. collection集合整体结构图

  3. Git - 对一组仓库进行配置

    对一组仓库使用一套配置,另一组仓库使用另一套配置的需求也是有的,比如公司仓库的配置和我个人项目的仓库配置并不完全相同,每次都修改单个仓库的配置太麻烦并且可能会粗心忘改了以错误的配置进行提交,如何对一个 ...

  4. import 和组件库按需引入

    概述 今天查资料查到了一些有趣的东西,记录下来,供以后开发时参考,相信对其他人也有用. 参考资料: import.require.export.module.exports 混合使用详解 从 impo ...

  5. 有关MSSQL2000在Win7上的安装

    https://baijiahao.baidu.com/s?id=1593533837896849226&wfr=spider&for=pc 怎么在win7下安装sql server2 ...

  6. 宝塔面板修改用户名和密码报错:TypeError: cannot concatenate 'str' and 'NoneType' objects

    [root@dapao~]# bt 14 正在执行(14)... ================================================================== ...

  7. 【MM系列】SAP 的账期分析和操作

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 的账期分析和操作   前言部 ...

  8. 【Python】关于近期爬虫学习的总结

    写在开头 在之前的三篇文章中,我尝试了使用python爬虫实现的对于特定站点的<剑来>小说的爬取,对于豆瓣的短评的爬取,也有对于爬取的短评数据进行的词云展示,期间运用了不少的知识,现在是时 ...

  9. 深入理解java:2.3. 并发编程 java.util.concurrent包

    JUC java.util.concurrent包, 这个包是从JDK1.5开始引入的,在此之前,这个包独立存在着,它是由Doug Lea开发的,名字叫backport-util-concurrent ...

  10. python multiprocessing模块 介绍

    一 multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu\_count\(\)查看),在python中大部分情况需要使用多进 ...