spring cloud微服务实践一
最近在学习spring框架.其中spring cloud在微服务方面很火,所以在学习过程中,也做一些记录.
注:这一个系列的开发环境版本为 java1.8, spring boot2.x, spring cloud Greenwich.SR2, IDE为 Intelli IDEA
spring cloud的简介
关于spring cloud是什么,做什么的问题这里就不再详细说明了.需要的可以看
这篇文章[http://www.ityouknow.com/springcloud/2017/05/01/simple-springcloud.html] (博客园markdown不支持超链接).
接下来我们就来实践spring cloud的几个核心组件.
注册中心Eureka
Eureka是Netflix开源的一款提供服务注册和发现的产品.它是spring cloud最核心的组件之一.
接下来我们看看具体的构建步骤:
构建步骤
1.创建spring cloud项目
选择菜单 File>New>Project, 选择 Spring Initializr,然后 next.

2.输入项目名称
Group 为组织名, Artifact 为项目名, 输出完毕后 next.

3.选择依赖
接下来选择依赖,直接Spring Cloud, 然后 next.

4.选择项目路径
选好路径,直接 next.

5.完成创建
到这里,一个标准的spring cloud项目就出来了

6.补充代码
接下来就是补充代码了.
实例代码
1.首先的依赖关系: 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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xingyys.firstCloud</groupId>
<artifactId>discovery</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>discovery</name>
<description>Demo project for Spring Cloud Discovery</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</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>
</project>
要注意的是spring boot2.x版本和1.x版本在依赖上有一些不同,所以特别注意 properties中的<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
2.启动代码中添加@EnableEurekaServer注解
@SpringBootApplication
@EnableEurekaServer
public class DiscoveryApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryApplication.class, args);
}
}
3.配置文件 application.properties
spring.application.name=spring-cloud-eureka
server.port=8000
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
- eureka.client.register-with-eureka :表示是否将自己注册到Eureka Server,默认为true。
- eureka.client.fetch-registry :表示是否从Eureka Server获取注册信息,默认为true。
- eureka.client.serviceUrl.defaultZone :设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。
4.编译打包
进入到discovery目录下执行命令:
# 忽略测试
mvn clean package -Dmaven.test.skip=true
编译成功后会在 target目录下生成 discovery.xx.jar包
5.运行discovery
java -jar target/discovery-0.0.1-SNAPSHOT.jar
都成功的话,浏览器访问 http://localhost:8000:

注册中心的高可用
既然注册中心这么重要,那么单机运行怎么能保证服务的可靠性呢.所以我们就需要对注册中心做集群.
Eureka通过互相注册的方式来实现高可用的部署,所以我们只需要将Eureke Server配置其他可用的serviceUrl就能实现高可用部署.
接下来我们就来看看怎么实现吧:
双活配置
1.创建application-node1.properties,作为node1服务中心的配置,并将serviceUrl指向node2`, :
spring.application.name=discovery-node1
server.port=8001
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.instance.hostname=node1
eureka.client.serviceUrl.defaultZone=http://node2:8002/eureka/
2.创建application-node2.properties,作为node2服务中心的配置,并将serviceUrl指向node1:
spring.application.name=discovery-node2
server.port=8002
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.instance.hostname=node2
eureka.client.serviceUrl.defaultZone=http://node1:8001/eureka/
3.修改hosts
127.0.0.1 node1
127.0.0.1 node2
4.修改代码,添加@EnableEurekaClient注解:
@SpringBootApplication
@EnableEurekaServer
@EnableEurekaClient
public class DiscoveryApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryApplication.class, args);
}
}
5.打包启动
依次执行下面命令
#打包
mvn clean package -Dmaven.test.skip=true
# 分别以node1和node2 配置信息启动eureka
# --spring.profiles.active 指定启动不同的配置文件
java -jar target/discovery-0.0.1-SNAPSHOT.jar --spring.profiles.active=node1
java -jar target/discovery-0.0.1-SNAPSHOT.jar --spring.profiles.active=node2
成功后访问浏览器就会变成这样:

更多集群
如果是三个及以上的节点又要怎么配合呢,其实也是同样的原理.以三个节点为例,每个节点注册为其他节点的client就可以了.
因为spring cloud也支持yaml格式的配置文件,所以三个节点的配置文件可以写在一个yaml文件中:
---
spring:
application:
name: discovery
profiles: nodes1
server:
port: 8000
eureka:
instance:
hostname: node1
client:
serviceUrl:
defaultZone: http://node2:8001/eureka/,http://node3:8002/eureka/
---
spring:
application:
name: discovery
profiles: nodes2
server:
port: 8001
eureka:
instance:
hostname: node2
client:
serviceUrl:
defaultZone: http://node1:8000/eureka/,http://node3:8002/eureka/
---
spring:
application:
name: discovery
profiles: nodes3
server:
port: 8002
eureka:
instance:
hostname: node3
client:
serviceUrl:
defaultZone: http://node1:8000/eureka/,http://node2:8001/eureka/
分别启动:
java -jar target/discovery-0.0.1-SNAPSHOT.jar --spring.profiles.active=nodes1
java -jar target/discovery-0.0.1-SNAPSHOT.jar --spring.profiles.active=nodes2
java -jar target/discovery-0.0.1-SNAPSHOT.jar --spring.profiles.active=nodes3
浏览器访问:

spring cloud微服务实践一的更多相关文章
- spring cloud微服务实践二
在上一篇,我们已经搭建了spring cloud微服务中的注册中心.但只有一个注册中心还远远不够. 接下来我们就来尝试提供服务. 注:这一个系列的开发环境版本为 java1.8, spring boo ...
- spring cloud微服务实践七
在spring cloud 2.x以后,由于zuul一直停滞在1.x版本,所以spring官方就自己开发了一个项目 Spring Cloud Gateway.作为spring cloud微服务的网关组 ...
- spring cloud微服务实践五
本篇我们来看看怎么实现spring cloud的配置中心. 在分布式系统中,特别是微服务架构下,可能会存在许多的服务,每个服务都会存在一个或多个的配置文件.那怎么多的配置文件的管理就会成为一个大问题. ...
- spring cloud微服务实践六
本片我们就来认识下spring cloud中的zuul组件. 注:这一个系列的开发环境版本为 java1.8, spring boot2.x, spring cloud Greenwich.SR2, ...
- spring cloud微服务实践三
上篇文章里我们实现了spring cloud中的服务提供者和使用者.接下来我们就来看看spring cloud中微服务的其他组件. 注:这一个系列的开发环境版本为 java1.8, spring bo ...
- Spring Cloud微服务实践之路-起始
由于各种原因,公司要对现有的营销产品进行微服务化,如果可以,则对公司所有产品逐步进行微服务化. 而本人将探索这条路,很艰难,但干劲十足.整个过会记录下来,以便以后查阅. 感谢公司!感谢领导! 相关书籍 ...
- spring cloud微服务实践四
spring cloud的hystrix还有一个配搭的库hystrix-dashboard,它是hystrix的一款监控工具,能直观的显示hystrix响应信息,请求成功率等.但是hystrix-da ...
- Spring Cloud微服务实践之路- Eureka Server 中的第一个异常
EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER ...
- 放弃Dubbo,选择最流行的Spring Cloud微服务架构实践与经验总结
http://developer.51cto.com/art/201710/554633.htm Spring Cloud 在国内中小型公司能用起来吗?从 2016 年初一直到现在,我们在这条路上已经 ...
随机推荐
- [大数据相关] Hive中的全排序:order by,sort by, distribute by
写mapreduce程序时,如果reduce个数>1,想要实现全排序需要控制好map的输出,详见Hadoop简单实现全排序. 现在学了hive,写sql大家都很熟悉,如果一个order by解决 ...
- Mac 下反编译Android APK
准备工作:安装ApkTool.dex2jar.JD-GUI 安装ApkTool 1.下载ApkTool.大家可以从 https://ibotpeaches.github.io/Apktool/inst ...
- leetcode 143. Reorder List 、86. Partition List
143. Reorder List https://www.cnblogs.com/grandyang/p/4254860.html 先将list的前半段和后半段分开,然后后半段进行逆序,然后再连接 ...
- 浅谈Mysql共享锁、排他锁、悲观锁、乐观锁及其使用场景
浅谈Mysql共享锁.排他锁.悲观锁.乐观锁及其使用场景 Mysql共享锁.排他锁.悲观锁.乐观锁及其使用场景 一.相关名词 |--表级锁(锁定整个表) |--页级锁(锁定一页) |--行级锁(锁 ...
- WIN 10 看不到SAMBA共享的硬盘
1.SMB1.0/CIFS协议默认被关闭了,之前的勒索病毒就是用的这个协议的漏洞,所以你去“启动和关闭windows功能”下手动勾选启用SMB1.0/CIFS协议 2.管理员身份执行 sc.exe c ...
- mysql常用命令、非交互式mysql命令看29条
CentOS下mysql数据库常用命令总结1.更改root密码 mysqladmin -uroot password 'yourpassword' 2.远程登陆mysql服务器 mysql -uroo ...
- WordPress获取特色图像的链接地址
为什么要获取WordPress的特色图像呢? 这主要是因为,我们已经写好了静态模板文件,只有获取WordPress特色图像地址插入进去就可以了,非常方便. 还有就是有的时候,我们需要设置图片的宽度为1 ...
- npm的问题【解决】
1.解决npm下载慢的问题,使用该命令 npm install --registry=https://registry.npm.taobao.org 好处:比起cnpm官网解释的,这个更好,使用cnp ...
- python获取昨日日期
获取昨日日期oneday = datetime.timedelta(days=1) 一天 day = datetime.datetime.strptime(self.date,'%Y-%m-%d') ...
- Hystrix多个线程池切换执行超时带来的问题(图解)
线程池切换带来的超时问题 上图有什么问题: Controller的Hystrx线程池已经到了超时时间,而FeignClient的Hystrx线程池还没到超时时间. 场景: Controller ...