Spring Cloud入门

Spring Cloud入门

 

转载 小明明明小 已关注


首先在idea中新建一个工程demo,作为父工程。

image.png

创建完成后的pom配置文件如下


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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.exampledemo0.0.1-SNAPSHOTjardemoDemo project for Spring Bootorg.springframework.bootspring-boot-starter-parent1.5.13.RELEASEUTF-8UTF-81.8Finchley.RELEASEorg.springframework.bootspring-boot-starter-testtestorg.springframework.cloudspring-cloud-dependenciesEdgware.SR4pomimportorg.springframework.bootspring-boot-maven-pluginserver1clientribbonzuul

声明多个模块:server,client,ribbon,zuul。

统一依赖的版本号。只是声明依赖,并未被继承,只有子工程中写了该依赖项,且未注明版本号,才会从父项目中继承此项。(当子工程指定版本时,使用子工程中指定的版本)。

所有在 里的依赖都会自动注入,默认被所有的子工程继承。即使子工程中不写依赖,在父工程中里的依赖也都会被子工程继承。

然后创建子工程eureka server1服务注册中心,用于提供服务的注册与发现。

image.png

server1 pom配置


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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.example

server1

0.0.1-SNAPSHOT

jar

server1Demo project for Spring Bootcom.exampledemo0.0.1-SNAPSHOTorg.springframework.cloudspring-cloud-starter-netflix-eureka-serverorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-maven-plugin

修改server1 pom 继承父工程

com.example

demo

0.0.1-SNAPSHOT

相关配置取自父工程

image.png

pom添加相应依赖,配置完成后在启动类中@EnableEurekaServer。

之后进行application.yml相关配置。

server:

port: 1111

eureka:

instance:

hostname: qlocal

prefer-ip-address: true

lease-expiration-duration-in-seconds: 20

lease-renewal-interval-in-seconds: 10

client:

fetch-registry: true

register-with-eureka: true

service-url:

defaultZone: http://qlocal:1110/eureka/,http://qlocal:1111/eureka/

spring:

application:

name: SERVERSEV

server.port 声明端口号为 1111

eureka.instance.hostname 设置当前实例主机名为 qlocal(在本地hosts中添加 127.0.0.1 qlocal)

eureka.instance.prefer-ip-address 不使用主机名定义注册中心地址,而使用IP地址的形式。Spring会自动为我们获取第一个非回环IP地址(除环路IP外的第一个IP地址)。

eureka.instance.lease-expiration-duration-in-seconds 定义服务失效时间为20 秒。表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该instance。

默认为90秒

如果该值太大,则很可能将流量转发过去的时候,该instance已经不存活了。

如果该值设置太小了,则instance则很可能因为临时的网络抖动而被摘除掉。

该值至少应该大于lease-renewal-interval-in-seconds的值。

eureka.instance.lease-renewal-interval-in-seconds 定义服务续约任务(心跳)的调用间隔10秒。 表示eureka client发送心跳给server端的频率。如果在leaseExpirationDurationInSeconds后,server端没有收到client的心跳,则将摘除该instance。除此之外,如果该instance实现了HealthCheckCallback,并决定让自己unavailable的话,则该instance也不会接收到流量。

默认30秒

eureka.client.fetch-registry 为true 进行服务检查

eureka.client.register-with-eureka 为true 向注册中心注册自己

eureka.client.service-url. defaultZone=http://qlocal:1110/eureka/,http://qlocal:1111/eureka/指定服务注册中心的位置为http://qlocal:1110/eureka/,http://qlocal:1111/eureka/ 两个地址。

spring.application.name=SERVERSEV 指定应用名称SERVERSEV

配置完成后访问 qlocal:1111

image.png

接着创建eureka client 服务提供者

image.png

pom配置


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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.example

client

0.0.1-SNAPSHOT

jar

client

Demo project for Spring Boot

com.example

demo

0.0.1-SNAPSHOT

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-maven-plugin

然后在启动类中添加@EnableEurekaClient

启动类

@RestController

@EnableEurekaClient

@SpringBootApplication

public class ClientApplication {

publicstaticvoidmain(String[] args){    SpringApplication.run(ClientApplication.class, args);}@Value("${server.port}")String port;@RequestMapping("/my")publicStringhome(@RequestParam String name){return"my name"+name +" 端口"+port;}

}

application.yml配置

server:

port: 1112

eureka:

client:

serviceUrl:

defaultZone: http://localhost:1110/eureka/,http://localhost:1111/eureka/

prefer-same-zone-eureka: true

instance:

lease-renewal-interval-in-seconds: 5

lease-expiration-duration-in-seconds: 10

spring:

application:

name: CLIENTSEV

eureka.client.prefer-same-zone-eureka 实例是否使用同一zone里的eureka服务器,默认为true,理想状态下,eureka客户端与服务端是在同一zone下

访问 qlocal:1112/my?name=1

image.png

image.png

eureka ribbon+hystrix 消费者+断路器

pom配置


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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.exampleribbon0.0.1-SNAPSHOTjarribbonDemo project for Spring Bootcom.exampledemo0.0.1-SNAPSHOTorg.springframework.cloudspring-cloud-starter-netflix-ribbonorg.springframework.bootspring-boot-starter-weborg.springframework.cloudspring-cloud-starter-netflix-eureka-serverorg.springframework.cloudspring-cloud-starter-hystrixorg.springframework.retryspring-retryorg.springframework.bootspring-boot-maven-plugin

想要自动重试必须添加retry依赖

想要自动重试必须添加retry依赖

想要自动重试必须添加retry依赖

org.springframework.retry

spring-retry

application.yml配置

eureka:

client:

service-url:

defaultZone: http://qlocal:1110/eureka/,http://qlocal:1111/eureka/

server:

port: 1114

spring:

application:

name: RIBBONSERV

cloud:

loadbalancer:

retry:

enabled: true

hystrix:

command:

default:

execution:

isolation:

thread:

timeoutInMilliseconds: 10000

RIBBONSERV:

ribbon:

ConnectTimeout: 250

ReadTimeout: 1000

OkToRetryOnAllOperations: true

MaxAutoRetriesNextServer: 2

MaxAutoRetries: 1

spring.cloud. loadbalancer.retry.enabled=true 开启重连机制

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000 断路器的超时时间,断路器的超时时间需要大于ribbon的超时时间,不然不会触发重试。

RIBBONSERV: #服务名(spring.application.name=RIBBONSERV)

ribbon:

ConnectTimeout: 250 #ribbon请求连接的超时时间

ReadTimeout: 1000 #ribbon 请求处理的超时时间

OkToRetryOnAllOperations: true #对所有操作请求都进行重试

MaxAutoRetriesNextServer: 2 #对下个实例的重试次数

MaxAutoRetries: 1 #对当前实例的重试次数

启动类配置

添加@EnableEurekaClient 向服务中心进行注册

@Bean //向程序ioc注入bean restTemplate

@LoadBalanced //表明 restTemplate开启负载均衡功能

@EnableHystrix

@EnableEurekaClient

@SpringBootApplication

public class RibbonApplication {

publicstaticvoidmain(String[] args){    SpringApplication.run(RibbonApplication.class, args);}@Bean@LoadBalancedRestTemplaterestTemplate(){returnnewRestTemplate();}

}

新建一个Service类,通过访问之前Clientsev的/my接口进行测试。这里使用了程序名替代了具体的url地址,ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名

service

@Service

public class HelloService {

@Autowired

RestTemplate restTemplate;

@HystrixCommand(fallbackMethod = "error")

public String helservice(String name){

return restTemplate.getForObject("http://CLIENTSEV/my?name="+name,String.class);

}

public String error(String name){

return "my name" + name +" 断路" ;

}

}

新建controller 类 调用service方法

@RestController

public class HelloController {

@Autowired

HelloService helloService;

@RequestMapping(value = "/my")

public String con(@RequestParam String name){

return helloService.helservice(name);

}

}

启动 一次开启 server 1110 1111 client 1112 1113 ribbon 1114(qlocal:1114/my?name=1)

image.png

zuul 网关

pom


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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.examplezuul0.0.1-SNAPSHOTjarzuulDemo project for Spring Bootcom.exampledemo0.0.1-SNAPSHOTorg.springframework.cloudspring-cloud-starter-netflix-zuulorg.springframework.cloudspring-cloud-starter-netflix-eureka-serverorg.springframework.retryspring-retryorg.springframework.retryspring-retryorg.springframework.bootspring-boot-maven-plugin

application.yml

eureka:

client:

service-url:

defaultZone: http://qlocal:1110/eureka/,http://qlocal:1111/eureka/

server:

port: 1115

spring:

application:

name: ZUULSERV

cloud:

loadbalancer:

retry:

enabled: true

zuul:

routes:

ribbon:

path: /ribbon/**

serviceId: RIBBONSERV

hystrix:

command:

default:

execution:

isolation:

thread:

timeoutInMilliseconds: 10000

ZUULSERV:

ribbon:

ConnectTimeout: 250

ReadTimeout: 1000

OkToRetryOnAllOperations: true

MaxAutoRetriesNextServer: 2

MaxAutoRetries: 1

启动类

@EnableEurekaClient

@EnableZuulProxy

@SpringBootApplication

public class ZuulApplication {

publicstaticvoidmain(String[] args){    SpringApplication.run(ZuulApplication.class, args);}

}

--------------------------------------------------------------------------------------------------------------------访问地址 qlocal:1115/ribbon/my?name=1

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,710评论 4 376
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,839评论 2 308
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,295评论 0 255
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,776评论 0 223
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,198评论 3 297
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,074评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,200评论 2 322
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,986评论 0 214
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,733评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,877评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,348评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,675评论 3 265
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,393评论 3 246
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,209评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,996评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,212评论 2 287
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 36,003评论 2 280

推荐阅读更多精彩内容