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

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

一般情况下,基本的微服务框架包含:框架:注册中心、负载均衡、声明式服务(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. EasyUI datagrid动态加载json数据

    最近做一个项目,要求是两张张表可能查找出10多种不同的结果集. 如果想只用一个表格就把全部的结果不同的显示出来那么就肯定不同使用固定的字段名字,要通过动态加载后台返回来的数据把它显示出来就必须动态加载 ...

  2. centos7 安装 chrome

    1. 配置yum源 在目录 /etc/yum.repos.d/ 下新建文件 google-chrome.repo cd /etc/yum.repos.d/ vim google-chrome.repo ...

  3. java跨越请求实例

    使用Access-Control-Allow-Origin解决跨域 什么是跨域 当两个域具有相同的协议(如http), 相同的端口(如80),相同的host(如www.google.com),那么我们 ...

  4. python常用笔记

    A trick when you want to flatten a matrix X of shape (a,b,c,d) to a matrix X_flatten of shape (b*c*d ...

  5. hg(Mercurial)使用参考

    hg(Mercurial)使用参考   使用hg(mercurial)有好几个月了,个人感觉这款分布式的版本控制系统非常不错,易学,易用:你可以从做在你旁边的同事拉取完整的代码; 对网络的依赖性更低, ...

  6. open jdk卸载

    //查找:open jdk # rpm -qa | grep java //卸载open jdk # rpm -e --nodeps 包 # source /etc/profile # java -v ...

  7. [ros] ros入门记录

    ROS入门 半天入门ROS,总体感觉比较好理解,python写不用编译超级爽,学完ros去学电控去了. ros2比ros1好用,所以最终是学ros2. ros1 安装 添加源 > sudo sh ...

  8. Unsupervised Image-to-Image Translation Networks

    Abstract: 无监督图像到图像的翻译目的是学习不同域图像的一个联合分布,通过使用来自单独域图像的边缘分布.给定一个边缘分布,可以得到很多种联合分布.如果不加入额外的假设条件的话,从边缘分布无法推 ...

  9. Delphi XE2 之 FireMonkey 入门(40) - 控件基础: TMemo

    Delphi XE2 之 FireMonkey 入门(40) - 控件基础: TMemo 值得注意的变化: 1.其父类 TScrollBox 的许多特性也很有用处, 如:   Memo1.UseSma ...

  10. Delphi XE2 之 FireMonkey 入门(38) - 控件基础: TPopupMenu、TMenuItem、TMenuBar、TMainMenu

    Delphi XE2 之 FireMonkey 入门(38) - 控件基础: TPopupMenu.TMenuItem.TMenuBar.TMainMenu 相关控件: TMenuBar.TPopup ...