SpringCloud极大的简化了分布式系统的开发,实现了微服务的快速部署和灵活应用

SpringCloud主要框架

* 服务发现--Netfix Eureka

* 服务调用--Netfix Feign

* 熔断器--Netfix Hystrix

* 服务网关--Netfix Zuul

* 分布式配置--Spring Cloud Config

* 消息总线--Spring Cloud Bus

注意SpringCloud和SpringBoot版本要一一对应,不然会报错

一。Eureka服务发现

外部的pom.xml

<?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">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>tensquare_eureka</module>
</modules> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent> <groupId>com.jinke</groupId>
<artifactId>springboot</artifactId>
<version>1.0-SNAPSHOT</version> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.3.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies> </project>

内部的pom.xml

<?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">
<parent>
<artifactId>springboot</artifactId>
<groupId>com.jinke</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>tensquare_eureka</artifactId> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
</dependencies> </project>

application.yml

server:
port: 6868
eureka:
client:
register-with-eureka: false
fetch-registery: false
service-url:
defaultZone: http://127.0.0.1:${server.port}/eureka/

EurekaServer.java

package com.tensquare.eureka;

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

直接启动看

二。Eureka服务注册

新建一个module

在pom.xml中增加

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>

application.yml

server:
port: 9004
spring:
application:
name: register
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:6868/eureka/
instance:
prefer-ip-address: true

RegisterApplication.java

package com.example.register;

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

applicaiton跑起来,看结果已经注册成功了

三。服务调用

被调register的LabelController

package com.example.register.controller;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/label")
@CrossOrigin
public class LabelController { @RequestMapping(value = "/{labelId}", method = RequestMethod.GET)
public String findById(@PathVariable("labelId") String id) {
System.out.println("调用成功");
return "success";
} }

主调register2的ProblerController

package com.example.register2.controller;

import com.example.register2.client.BaseClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; @RestController
@CrossOrigin
@RequestMapping("/problem")
public class ProblemController {
@Autowired
private BaseClient baseClient; @RequestMapping(value = "/label/{labelId}", method = RequestMethod.GET)
public String findByLabelId(@PathVariable String labelId) {
baseClient.findById(labelId);
return "success";
}
}

application

package com.example.register2;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class Register2Application { public static void main(String[] args) {
SpringApplication.run(Register2Application.class, args);
} }

BaseClient

package com.example.register2.client;

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; @FeignClient("register")
public interface BaseClient { @RequestMapping(value = "/label/{labelId}", method = RequestMethod.GET)
public String findById(@PathVariable("labelId") String labelId);
}

eureka和register、register2服务都分别跑起来

同时可以看到register的控制台输出了“调用成功”,即代表调用其他服务成功

Spring已经实现了负载均衡,即调用服务集群时,会公平的轮流调用其中的服务

四。熔断器

熔断器可以有效避免雪崩现象

当被调服务挂掉以后,会走熔断器里面的内容

调用的服务增加实现类

package com.example.register2.client.impl;

import com.example.register2.client.BaseClient;
import org.springframework.stereotype.Component; @Component
public class BaseClientImpl implements BaseClient {
@Override
public String findById(String labelId) {
System.out.println("熔断器触发了");
return "success";
}
}

application.yml增加

feign:
hystrix:
enabled: true

五。网关

网关的作用就相当于一个中转分发站,接口

新建一个网关

pom.xml

<?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">
<parent>
<artifactId>springboot</artifactId>
<groupId>com.jinke</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>manager</artifactId> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
</dependencies> </project>

application.yml

server:
port: 9011
spring:
application:
name: manager
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:6868/eureka/
instance:
prefer-ip-address: true
zuul:
routes:
register:
path: /register/**
serviceId: register

application

package com.jinke.manager;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ManagerApplication {
public static void main(String[] args) {
SpringApplication.run(ManagerApplication.class);
}
}

然后启动服务即可

六。网关过滤

在网关模块新建一个过滤类即可

package com.jinke.manager.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component; @Component
public class ManagerFilter extends ZuulFilter {
@Override
public String filterType() {
//过滤器类型:之前或之后
return "pre";
} @Override
public int filterOrder() {
//多个过滤器的执行顺序
return 0;
} @Override
public boolean shouldFilter() {
//是否开启该过滤器
return true;
} @Override
public Object run() throws ZuulException {
//如果设置setsendzullResponse(false)表示不再继续执行
System.out.println("已经执行到最后了");
return null;
}
}

七。SpringCloudConfig配置

新建config的module

pom.xml

<?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">
<parent>
<artifactId>springboot</artifactId>
<groupId>com.jinke</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>config</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
</dependencies>
</project>

application.xml

server:
port: 12000
spring:
application:
name: config
cloud:
config:
server:
git:
uri: https://gitee.com/king1039/springcloud-config.git
username: xxxxxxx@qq.com
password: xxxxxx

application

package com.jinke.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class);
}
}

服务跑起来,如果提示Authentication is required but no CredentialsProvider has been registered是因为要账号密码验证,在配置文件填上

在git上修改以后,刷新会得到新的结果

下面通过配置来调用服务

新建一个base的module

bootstrap.myl

spring:
cloud:
config:
name: springcloud
profile: config
label: master
uri: http://127.0.0.1:12000

git上的配置文件springcloud-config.yml

server:
port: 9001
spring:
application:
name: base
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:6868/eureka/
instance:
prefer-ip-address: true

开启eureka、config、base三个服务,然后调用

流程就是先读取Git上的配置,然后拉下来,再调用服务

下面来通过bus消息开启监听

config的配置文件application.yml

server:
port: 12000
spring:
application:
name: config
cloud:
config:
server:
git:
uri: https://gitee.com/king1039/springcloud-config.git
username: 821578297@qq.com
password: wl802085
rabbitmq:
host: 172.16.10.221
management:
endpoints:
web:
exposure:
include: bus-refresh

被调的服务配置文件

server:
port: 9001
spring:
application:
name: base
rabbitmq:
host: 172.16.10.221
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:6868/eureka/
instance:
prefer-ip-address: true

修改了被调配置文件后,使用post请求发送一次bus-refresh即可实现消息传递

如果要监听自定义的标签需要增加注解@RefreshScope

代码地址:https://github.com/king1039/SpringCloud.git

欢迎关注我的微信公众号:安卓圈

SpringCloud基础的更多相关文章

  1. 微服务架构案例(05):SpringCloud 基础组件应用设计

    本文源码:GitHub·点这里 || GitEE·点这里 更新进度(共6节): 01:项目技术选型简介,架构图解说明 02:业务架构设计,系统分层管理 03:数据库选型,业务数据设计规划 04:中间件 ...

  2. SpringCloud 基础

    目录 SpringCloud 基础 一.概述 二.服务发现组件 Eureka 1. 介绍 2. 搭建 Maven 父工程 3. 创建 Eureka 集群 4. 创建服务提供方集群 5. 创建服务消费方 ...

  3. SpringCloud基础概念学习笔记(Eureka、Ribbon、Feign、Zuul)

    SpringCloud基础概念学习笔记(Eureka.Ribbon.Feign.Zuul) SpringCloud入门 参考: https://springcloud.cc/spring-cloud- ...

  4. 微服务之SpringCloud基础

    SpringCloud微服务基础 微服务架构--SpringCloud网站架构模式 单点应用/分布式系统面向于服务架构(SOA) /微服务架构web项目三层架构1.控制层2.业务逻辑层3.数据访问层传 ...

  5. SpringCloud基础教程学习记录

    这个学习记录是学习自翟永超前辈的SpringCloud的基础教程. 自己写这个教程的目的主要是在于,想要更凝练总结一些其中的一些实用点,顺便做个汇总,这样自己在复习查看的时候更加方便,也能顺着自己的思 ...

  6. spring-cloud - 基础环境搭建

    spring-cloud中文文档:https://springcloud.cc/ spring-cloud中文导航:http://springcloud.fun/ 文章纯属用于个人学习的一个归纳,哪里 ...

  7. SpringCloud基础组件总结,与Dubbo框架、SpringBoot框架对比分析

    本文源码:GitHub·点这里 || GitEE·点这里 一.基础组件总结 1.文章阅读目录 1).基础组件 Eureka组件,服务注册与发现 Ribbon和Feign组件,实现负载均衡 Hystri ...

  8. SpringCloud基础篇AOP之拦截优先级详解

    前面两篇分别介绍了AOP的基本使用姿势和一些高级特性,当时还遗留了一个问题没有说明,即不同的advice,拦截同一个目标方法时,优先级是怎样的,本篇博文将进行详细分析 同一个切面中,不同类型的advi ...

  9. SpringCloud学习笔记(一、SpringCloud 基础)

    目录: 概述 观察者模式 代理模式 概述: spring系列中使用了大量的设计模式,而最常见的便是这观察者.代理模式,所以在讲解SpringCloud之前我们先学习下这两个最常见的设计模式. 观察者模 ...

随机推荐

  1. gitlab修改IP地址及仓库地址

    将IP修改为192.168.10.100,操作方法 . 先修改本地的IP地址 vim /etc/sysconfig/network-scripts/ifcfg-eth0TYPE=EthernetBOO ...

  2. django项目中使用邮箱找回密码功能

    本文使用qq邮箱,需要登录邮箱,在设置-账户里面开启SMTP服务,要记下授权码 前端html {#找回密码的表单#} <form action="" method=" ...

  3. python测试开发django-68.templates模板标签{% for %}

    前言 有些标签类似这样: {% tag %} ,需要开始和结束标签 例如:{% tag %} ...标签 内容 ... {% endtag %},一般用于循环列表对象输出内容. for 标签 {% f ...

  4. package的xml格式的改变

    package.xml文件格式由1到2发生了一些变化: 格式2有如下一行: <package format="2"> 依赖也发生一些变化<depend> 格 ...

  5. postman:设置环境变量(参数化 & 关联)

    postman是测试接口的主要工具之一 我们的测试环境主要分为test(测试环境)和pre_production(预生产环境),而他们的api都是一样的,只是ip及端口不一样 所以,我们可以设置环境变 ...

  6. MySQL中的内连接、左连接、右连接、全连接、交叉连接

    创建两个表(a_table.b_table),两个表的关联字段分别为:a_table.a_id和b_table.b_id CREATE TABLE a_table ( a_id int NOT NUL ...

  7. SparkStreaming 性能、稳定、容错与语义

      怎样提高Spark Streaming的性能 1.创建多个接收器 待定:: 2.调节每一个batch interval的数据块的数量,其实就是调整上面第二个问题中提到的配置spark.stream ...

  8. python - 将数据转换成 excl 表格, json 等文件 (dajngo - 打开网页后自动下载)

    本篇只讲述怎么用. 具体 tablib  更多详细用法可参考博客 : https://blog.csdn.net/liangyuannao/article/details/41476277 # 不得不 ...

  9. Python 弹出框代码

      from ctypes import * user32 = windll.LoadLibrary('user32.dll')#调用dll文件 #a是得到弹出框的选择按钮的值 user32.Mess ...

  10. Vue的单选/多选效果

    includes()方法判断是否包含某一元素,返回true或false表示是否包含元素,对NaN一样有效 filter()方法用于把Array的某些元素过滤掉,filter()把传入的函数依次作用于每 ...