SpringCloud无废话入门01:最简SpringCloud应用
1.创建Parent
Parent很简单,创建一个空的maven项目,pom如下:
<?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>
<groupId>com.zuikc</groupId>
<artifactId>springcloud.parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>eureka01</module>
<module>eureka02</module>
<module>provider01</module>
<module>provider02</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
</project>
注意两个地方:
1:spring-boot-starter-parent要被指定为整个解决方案的parent;
2:4个Modul随着下文项目的创建会被自动创建;
2.创建eureka01
如果说dubbo的注册中心zookeeper,那么在spring cloud中的注册中心就是:eureka,并且使用起来还要更容易上手一些。
现在,就让我们来创建第一个eureka站点。
首先先创建一个maven子模块,并添加公用依赖。
类型选择如下:
创建完毕,Pom修改为如下:
<?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/maven-v4_0_0.xsd">
<parent>
<artifactId>springcloud.parent</artifactId>
<groupId>com.zuikc</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<name>eureka01</name>
<artifactId>eureka01</artifactId>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>
代码:
package com.zuikc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @ClassName RegisterApplication
* @Description 我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系“码农星球”
* @Author 码农星球
**/
@EnableEurekaServer
@SpringBootApplication
public class RegisterApplication {
public static void main(String[] args) {
SpringApplication.run(RegisterApplication.class,args);
}
}
application.yml
server:
port: 9091
eureka:
instance: #定义Eureka实例
hostname: eureka-9091.com #Eureka实例所在的主机名
client:
register-with-eureka: false #表示是否将自己注册到Eureka Server上,默认为true
fetch-registry: false #表示是否从Eureka Server上获取注册信息,默认为true
service-url:
defaultZone: http://localhost:9091/eureka/
接着run RegisterApplication,如果没有异常报错,再从浏览器访问http://localhost:9091/,看到如下界面,则说明成功。
3.创建服务提供者provider01、provider02
有了注册中心了,现在,让我们写两个服务提供者站点。
服务提供者,也叫Eureka客户端,它把自己注册到Eureka服务器中,并周期性的发送心跳来更新它的服务租约。
为什么要两个呢?为了体现分布式呀。我们创建两个子模块,它们的类型都是webapp,如下,
Pom为:
<?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/maven-v4_0_0.xsd">
<parent>
<artifactId>springcloud.parent</artifactId>
<groupId>com.zuikc</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<name>provider01</name>
<artifactId>provider01</artifactId>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
然后启动类为:
package com.zuikc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @ClassName Provider1Application
* @Description 我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系“码农星球”
* @Author 码农星球
**/
@SpringBootApplication
@EnableDiscoveryClient
public class Provider1Application {
public static void main(String[] args) {
SpringApplication.run(Provider1Application.class, args);
}
}
然后controller为:
package com.zuikc;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @ClassName HelloController
* @Description 我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系“码农星球”
* @Author 码农星球
**/
@Configuration
@Controller
public class HelloController {
@RequestMapping("hello")
@ResponseBody
public String hello() {
return "hello zuikc.com!";
}
}
然后application.yml为:
server:
port: 9191
spring:
application:
name: hello-service
eureka:
client:
service-url:
defaultZone: http://localhost:9091/eureka/
注意,第二个服务项目,上面的配置中的port为9192就是了,其它配置和控制器本身的代码都一模一样。
然后,先启动eureka,再分别启动两个服务提供者。结果如下:
4.Eureka的集群
以下这张图是springcloud著名的架构图,
在这张图中,我们可以看到,包括EurekaServer(即注册中心)本身,也是不止一个的。这是因为,在实际的应用中,注册中心理应是可以被集群的。这样,当集群中有分片发生故障的时候,Eureka会自动转入自我保护模式。它允许在分片发生故障的时候继续提供服务的发现和注册,当故障分配恢复时,集群中的其他分片会把他们的状态再次同步回来。
5.创建eureka02
既然说到了注册中心的集群,那我们就来继续创建一个eureka服务器吧。
在parent下建立子module。所有过程和创建eureka01一样,并且pom和RegistApplication也一样。
唯一需要改变的就是配置文件。
首先,然我们回到eureka01中,修改application.yml如下:
server:
port: 9091
eureka:
instance: #定义Eureka实例
hostname: eureka-9091.com #Eureka实例所在的主机名
client:
#register-with-eureka: false #表示是否将自己注册到Eureka Server上,默认为true
#fetch-registry: false #表示是否从Eureka Server上获取注册信息,默认为true
service-url:
defaultZone: http://localhost:9092/eureka/
server:
enable-self-preservation: false
spring:
application:
name: eureka-service
首先,我们将client下两个不允许自己注册的属性注释掉。
同时,将service-url的地址指向到了第二个注册中心的地址。也就是说,两个注册中心要互相引用。
其次,我们加了enable-self-preservation: false,这属性默认是开启的,如果默认开启,注册中心的服务列表就算有些服务断开了,也会继续保存。
最后,我们加了spring这个属性节点,将本身作为服务命名为eureka-service。
Ok,然后再来修改第二个eureka服务器的配置文件,
server:
port: 9092
eureka:
instance: #定义Eureka实例
hostname: eureka-9092.com #Eureka实例所在的主机名
client:
#register-with-eureka: false #表示是否将自己注册到Eureka Server上,默认为true
#fetch-registry: false #表示是否从Eureka Server上获取注册信息,默认为true
service-url:
defaultZone: http://localhost:9091/eureka/
server:
enable-self-preservation: false
spring:
application:
name: eureka-service
整个项目创建完毕,目录结构大概是这样的,
接下来,让我们首先启动eureka01。注意,启动01的时候由于02还没有启动,所以会报连接不到02,但是没有关系,忽略就行,因为02马上就会启动,它会自动续约。
接着启动eureka02,然后再启动两个provider吧。
可以看到,4个服务都已经启动了,
注意,这个时候去9092,也就是eureka02,也能看到这4个服务,
感谢关注“码农星球”。本文版权属于“码农星球”。我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系我们。
SpringCloud无废话入门01:最简SpringCloud应用的更多相关文章
- SpringCloud无废话入门05:Spring Cloud Gateway路由、filter、熔断
1.什么是路由网关 截至目前为止的例子中,我们创建了一个service,叫做:HelloService,然后我们把它部署到了两台服务器(即提供了两个provider),然后我们又使用ribbon将其做 ...
- SpringCloud无废话入门04:Hystrix熔断器及监控
1.断路器(Circuit Breaker)模式 在上文中,我们人为停掉了一个provider,在实际的生产环境中,因为意外某个服务down掉,甚至某一层服务down掉也是会是有发生的.一旦发生这种情 ...
- SpringCloud无废话入门03:Feign声明式服务调用
1.Feign概述 在上一篇的HelloService这个类中,我们有这样一行代码: return restTemplate.getForObject("http://hello-servi ...
- SpringCloud无废话入门02:Ribbon负载均衡
1.白话负载均衡 在上一篇的介绍中,我们创建了两个一模一样的服务提供者:Provider1和Provider2,然后它们提供的服务也一模一样,都叫Hello-Service.为什么一样的服务我们要部署 ...
- SpringBoot无废话入门01:最简SpringBoot应用
虽然本篇讲的是一个最简的SpringBoot应用,但是要说明的是:学习SpringBoot是有门槛的,这个门槛就是, 1:首先得有框架的基础,比如SSM: 2:MAVEN基础. 在学好上面两者的基础上 ...
- SpringBoot无废话入门04:MyBatis整合
1.Parent引入及pom配置 首先,如果要支持mybatis,那么我们就应该引入mybatis的starter.同时,由于连接本身还需要用jdbc的connetor和连接池,所以一并需要引入这些依 ...
- SpringBoot无废话入门03:SpringMVC支持
1.默认配置 Springboot对于路径的默认位置为: spring.resources.static-locations=classpath:/META-INF/resources/,classp ...
- SpringBoot无废话入门02:SpringBoot启动分析
1.核心注解 在上文中,我们讲到了@SpringBootApplication是SpringBoot的核心注解. 可以很方便的在idea中下载源码来查看该注解的源码,如下: 可以看到,该注解本身又被其 ...
- 无废话ExtJs 入门教程十七[列表:GridPanel]
无废话ExtJs 入门教程十七[列表:GridPanel] extjs技术交流,欢迎加群(201926085) 在Extjs中,GridPanel用于数据显示,即我们平时说的列表页.在本节中,我们先对 ...
随机推荐
- WPF: Hide grid row
http://stackoverflow.com/questions/2502178/wpf-hide-grid-row Setting all the Items in the Row to Vis ...
- 51Nod1309 Value of all Permutations 期望
原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1309.html 题目传送门 - 51Nod1309 题意 长度为N的整数数组A,有Q个查询,每个查询 ...
- BZOJ4811 [Ynoi2017]由乃的OJ 树链剖分
原文链接http://www.cnblogs.com/zhouzhendong/p/8085286.html 题目传送门 - BZOJ4811 题意概括 是BZOJ3668长在树上并加上修改和区间询问 ...
- day21 模块 异常处理
常用模块:http://www.cnblogs.com/Eva-J/articles/7228075.html 今日概要: #time # —— 时间:时间戳 字符串 结构化时间 #collectio ...
- 搭建TensorFlow中碰到的一些问题(TensorBoard不是内部或外部指令也不是可运行的程序)~
一.windows10环境+pip python软件包(最新版)+Pycharm软件(过段时间在弄下CUDA和GPU吧) 直接使用pip指令来安装tensorflow软件(如果很久没有更新pip软件包 ...
- python自带线程池
1. 注意: 导包是: from multiprocessing.pool import ThreadPool #线程池不在thrading中 2. 代码: from mutiprocessing.p ...
- Hexo博客yilia主题添加Gitment评论系统
一开始搭建hexo+yilia博客使用的评论功能是通过来必力实现的.来必力免费,功能多,一开始的体验效果很好,但是后来打开网站发现来必力加载的越来越慢(来必力是韩国的公司,可能是国内限制),遂打算换一 ...
- APP开发,微信第三方登录的介绍
去年做了一阵APP相关的开发,经常遇到第三方登陆的需求,比如微信.微博.fb的第三方登陆等等,其实主要的流程都大同小异,这里就以微信为例来介绍,希望对大家有帮助. 微信开放平台(open.weixin ...
- js算法初窥07(算法复杂度)
算法复杂度是我们来衡量一个算法执行效率的一个度量标准,算法复杂度通常主要有时间复杂度和空间复杂度两种.时间复杂度就是指算法代码在运行最终得到我们想要的结果时所消耗的时间,而空间复杂度则是指算法中用来存 ...
- superset链接本地mysql数据库
刚安装好superset的时候大家都知道是用的其自动生成的sqllite数据库,如果我们想让器链接到自己数据库,给大家分享一下我的方法,以mysql为例: 1.安装好数据库mysql: $ sudo ...