Spring Cloud 学习笔记一
一、spring cloud 搭建注册中心(Eureka server)
1、spring cloud中提供了多种分步式服务组件,其都依赖于注册中心(eureka),注册中心的服务者与发现者都通过Eureka server相互通信,服务者与发现者都是Eureka client,同时注册到Eureka server 中,然后通过服务名相互调用接口,故我们需要首先搭建注册中心(Eureka Server),以下以inlij idea 为例:
a、inlij idea新建项目->选择spring initializer->next->next->选择web web、cloud discovery Eureka server->完成
b、打开项目pom文件,可以看到已自动添加Eureka server服务:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
C、打开application.properties文件,配置注册中心地址,如下:
spring.application.name=euraka-service
server.port=8088
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
D、配置运行项目,打开localhost:8088/, 出现发下界面表示成功:
@SpringBootApplication
@EnableEurekaServer
public class EurakaServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EurakaServiceApplication.class, args);
}
}

注:上面已注册了client,第一次运行应只注册了自已
1、搭建Eureka client(服务提供者)
A、inlij idea新建项目->选择spring initializer->next->next->选择web web、cloud discovery Eureka discovery->完成
B、打开项目pom文件,可以看到已自动添加Eureka client服务:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
C、打开application.properties文件,配置注册中心地址,如下:
spring.application.name=euraka-client
server.port=8089
eureka.instance.hostname=localhost //注册中心地址,这里是本地
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:8088/eureka/
C、配置运行项目后,可在注册中心务中看到,如下图:
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
1、搭建注册中心(服务发现者)Eureka client,创建项目如2所示,所有都不变,然后运行如下图:

注:现在eureka demo 就可以访问Eureka client 中的接口了,后面我们将使用feign来访问
二、使用feign来现实访问服务者之间的接口
1、feign是spring cloud 中用于服务之间访问最简单的方式,使用起来非常方便,用使接口加注解就可完成服务间调用,当然这里不讲原理,只讲用法
A、inlij idea新建项目->选择spring initializer->next->next->选择web web、Cloud routing feign->完成。
B、打开项目pom文件,可以看到已自动添加feign服务:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-openfeign</artifactId>
</dependency>
C、打开application.properties文件,配置注册中心地址,如下:
spring.application.name=feign-service
server.port=8094
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:8088/eureka/
D、配置feign,eureka client
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class FeignServiceApplication {
public static void main(String[] args) {
SpringApplication.run(FeignServiceApplication.class, args);
}
}
E、添加接口,并使用注解配置访问eureka client 服务,调用getdemoData方法
@FeignClient(value = "euraka-client")
public interface feignInterface {
@RequestMapping("/getdemoData")
public String getEurekaClient();
}
F、编写controller,如下图
@RestController
public class feignContrller {
@Autowired
private feignInterface feignInterfaces;
@GetMapping("/getFeignData")
public String getFeignData(){
return feignInterfaces.getEurekaClient();
}
}
G、运行访问是否成功

三、注册配置中心
1、配置中心是用来统一管理配置,配置文件可以放在git上,也可以放在本地,也可以布署在另一台服务器上,这样我们在更改配置而不影响原有代码,也不必重新打包项目
2、开始搭建配置中心服务项目(config server)
A、inlij idea新建项目->选择spring initializer->next->next->选择web web、cloud cofing config server->完成
A、打开pom文件,查看引用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
C、打开application.properties文件,配置注册中心地址,如下:
spring.application.name=config-service
server.port=8092
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:8088/eureka/
spring.profiles.active = native
spring.cloud.config.server.native.searchLocations=classpath:/resources/config-service/
注:这里首先把项目注册到注册中心,然后开启本地配置中心,配置本地路径
D、编写本地配置文件resources\config-service\config-dev.yml
config:
userName: whc
password: 123456
E、注解配置中心
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServiceApplication.class, args);
}
}
F、运行配置中心,输入如下地址:
http://localhost:8092/config/dev
结果如下,表示成功:

2、注册配置中心客户端(config client)
A、inlij idea新建项目->选择spring initializer->next->next->选择web web、cloud cofing config Client->完成
B、打开pom文件,查看配置引用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
A、由于spring cloud会先去读bootstrap.properties文件,不能直接配置applicat
ion.properties文件,我们需要新建bootstrap.properties文件,配置如下:
spring.application.name=config-client
server.port=8093
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:8088/eureka/
spring.cloud.config.enabled=true
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-service
spring.cloud.config.name=config
spring.cloud.config.profile=dev
注:首先配置了注册中心,然后配置了配置中心项目的serviceId,指明了配置文件名name,再指明了配置环境dev,spring cloud 会自动去找config-service服务下的配置文件config-dev.yml文件
B、配置注解,如下:
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
注:发现我们只配置了注册中心
C、编写读取配置文件controller,如下:
@RestController
public class demoController {
@Value("${config.userName}")
private String userName;
@Value("${config.password}")
private String userPwd;
@GetMapping("/getUserNameAndPwd")
public String getUserNameAndPwd(){
return "UserName:"+userName+",password:"+userPwd;
}
}
D、运行结果如下:

四、配置网关(zuul-service)
1、网关是spring cloud中很重要的角色,它可以解决跨域的问题;网关可以把请求映射到多个地址,通过项目名来区分请求,并转发给对应的服务
A、inlij idea新建项目->选择spring initializer->next->next->选择web web、cloud routing Zuul->完成
B、打开pom文件,查看引用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-zuul</artifactId>
</dependency>
C、打开application.properties文件,配置注册中心地址,如下:
spring.application.name=zuul-service
server.port=8091
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:8088/eureka/
zuul.routes.api-a.path=/eureka-client/**
zuul.routes.api-a.serviceId=euraka-client
zuul.routes.api-b.path=/eureka-demo/**
zuul.routes.api-b.serviceId=euraka-demo
注:首先配置注册中心,然后配置zuul 路由项目,就可以完成网关映射
D、注解配置
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ZuulServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulServiceApplication.class, args);
}
}
E、运行查看结果

Spring Cloud 学习笔记一的更多相关文章
- Spring Cloud学习笔记--Spring Boot初次搭建
1. Spring Boot简介 初次接触Spring的时候,我感觉这是一个很难接触的框架,因为其庞杂的配置文件,我最不喜欢的就是xml文件,这种文件的可读性很不好.所以很久以来我的Spring学习都 ...
- Spring Cloud 学习笔记 (一)-- Eureka 服务器
开局一张图,截取了本人学习资料中的一张图,很好地展示了Eureka的架构. Eureka服务器 管理服务的作用.细分为服务注册,服务发现. 所有的客户端在Eureka服务器上注册服务,再从Eureka ...
- Spring Cloud 学习笔记(二)——Netflix
4 Spring Cloud Netflix Spring Cloud 通过自动配置和绑定到Spring环境和其他Spring编程模型惯例,为Spring Boot应用程序提供Netflix OSS集 ...
- Spring Cloud 学习笔记(一)——入门、特征、配置
[TOC] 0 放在前面 0.1 参考文档 http://cloud.spring.io/spring-cloud-static/Brixton.SR7/ https://springcloud.cc ...
- Spring Cloud学习笔记-006
服务容错保护:Spring Cloud Hystrix 在微服务架构中,我们将系统拆分成了很多服务单元,各单元的应用间通过服务注册与订阅的方式互相依赖.由于每个单元都在不同的进程中运行,依赖通过远程调 ...
- Spring Cloud学习笔记-005
服务消费者 之前已经搭建好了微服务中的核心组件——服务注册中心(包括单节点模式和高可用模式).也有了服务提供者,接下来搭建一个服务消费者,它主要完成两个目标,发现服务以及消费服务.其中,服务发现的任务 ...
- Spring Cloud学习笔记-002
搭建Spring Cloud注册中心:Eureka 服务注册:在服务治理框架中,通常都会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务,将主机与端口号.版本号.通信协议等一些附加信息告诉注 ...
- Spring Cloud学习笔记-007
声明式服务调用:Spring Cloud Feign Feign基于Netflix Feign实现,整合了Spring Cloud Ribbon和Spring Cloud Hystrix,除了提供这两 ...
- Spring Cloud学习笔记-008
继承特性 通过上节的示例实践,当使用Spring MVC的注解来绑定服务接口时,几乎完全可以从服务提供方的Controller中依靠复制操作,构建出相应的服务客户端绑定接口.既然存在这么多复制操作,自 ...
- Spring Cloud学习笔记-009
API网关服务:Spring Cloud Zuul API网关是一个更为智能的应用服务器,它的定义类似于面向对象设计模式中的Façade模式,它的存在就像是整个微服务架构系统的门面一样,所有的外部客户 ...
随机推荐
- Python爬虫(三)爬淘宝MM图片
直接上代码: # python2 # -*- coding: utf-8 -*- import urllib2 import re import string import os import shu ...
- 自定义Element父子不关联的穿梭树
Element自身是有一个Transfer穿梭框组件的,这个组件是穿梭框结合checkbox复选框来实现的,功能比较单一,自己想实现这个功能也是很简单的,只是在项目开发中,项目排期紧,没有闲功夫来实现 ...
- 第三章:Python高级编程-深入类和对象
第三章:Python高级编程-深入类和对象 Python3高级核心技术97讲 笔记 3.1 鸭子类型和多态 """ 当看到一直鸟走起来像鸭子.游泳起来像鸭子.叫起来像鸭子 ...
- C. Four Segments 前缀后缀
C. Four Segments 这种分成了三个节点一般都可以处理一下前缀处理一下后缀,或者处理一下前面的这个点,处理一下后面的这个点,然后再枚举中间这个点. 如果和中间这个点有关的,那么就可以换一下 ...
- STL下<algorithm>下的sort函数
定义: sort函数用于C++中,对给定区间所有元素进行排序,默认为升序,也可进行降序排序.sort函数进行排序的时间复杂度为nlog2n,比冒泡之类的排序算法效率要高,sort函数包含在头文件为#i ...
- 爬虫系列 一次采集.NET WebForm网站的坎坷历程
今天接到一个活,需要统计人员的工号信息,由于种种原因不能直接连数据库 [无奈].[无奈].[无奈].采取迂回方案,写个工具自动登录网站,采集用户信息. 这也不是第一次采集ASP.NET网站,以前采集的 ...
- 老板:kill -9 的原理都不知道就敢去线上执行?明天不用来了!
GitHub 14.5k Star 的Java工程师成神之路,开放阅读了! 相信很多程序员对于Linux系统都不陌生,即使自己的日常开发机器不是Linux,那么线上服务器也大部分都是的,所以,掌握常用 ...
- (2)通信中为什么要进行AMC?
AMC,Adaptive Modulation and Coding,自适应调制与编码. 通信信号的传输环境是变化不定的,信道环境时好时差.在这种情景下,我们不可能按照固定的MCS进行信号发送.假如信 ...
- Java -> 构造器(构造方法)
构造方法 我们对封装已经有了基本的了解,接下来我们来看一个新的问题,依然以Person为例,由于Person中的属性都被private了,外界无法直接访问属性,必须对外提供相应的set和get方法.当 ...
- flask之Blueprint蓝图
flask_Blueprint.py ''' flask中的Blueprint蓝图: (1)新建模块,例如Bp1.py,Bp2.py,在模块中创建蓝图实例 (2)通过app.register_blue ...