Skip to content

Commit 0bf1391

Browse files
config
1 parent 2d02b4b commit 0bf1391

31 files changed

Lines changed: 1022 additions & 486 deletions

File tree

52.8 KB
Loading
30.2 KB
Loading
23.4 KB
Loading
25.3 KB
Loading

notes/微服务/SpringCloud.md

Lines changed: 364 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Spring Cloud
22

3-
1.本身比较靠后。 web ssm spring boot
4-
5-
2.Spring Cloud自身不稳定(a.部分组件停止更新 ;b.其他技术竞争非常厉害)
3+
Spring Cloud自身发展不稳定(a.部分组件停止更新 ;b.其他技术竞争非常厉害)
64

75

86

@@ -65,6 +63,8 @@ Spring Cloud包含了一系列技术:
6563

6664
![1591584096828](SpringCloud.assets/1591584096828.png)
6765

66+
### eureka
67+
6868
新建 子工程:eureka服务
6969

7070
引入依赖
@@ -185,7 +185,7 @@ eureka:
185185
</build>
186186
```
187187

188-
188+
### Feign
189189

190190
多个微服之间 的相互访问。Feign
191191

@@ -225,4 +225,363 @@ city2 ->CityClient -> city(queryCityBiId)
225225

226226
![1591691832817](SpringCloud.assets/1591691832817.png)
227227

228-
city2 ->controller - > cityClient -> city的controller-> city
228+
city2 ->controller - > cityClient -> city的controller-> city
229+
230+
231+
232+
### Ribbon
233+
234+
city2(消费者、客户端) -> city(生产者、服务端)
235+
236+
实现步骤:
237+
238+
city2
239+
240+
1.引入依赖
241+
242+
```xml
243+
<dependency>
244+
<groupId>org.springframework.cloud</groupId>
245+
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
246+
</dependency>
247+
248+
<dependency>
249+
<groupId>org.springframework.cloud</groupId>
250+
<artifactId>spring-cloud-starter-netflix-eureka-ribbon</artifactId>
251+
</dependency>
252+
253+
<dependency>
254+
<groupId>org.springframework.cloud</groupId>
255+
<artifactId>spring-cloud-starter-netflix-eureka-config</artifactId>
256+
</dependency>
257+
258+
```
259+
260+
2.配置
261+
262+
```xml
263+
eureka:
264+
client:
265+
service-url:
266+
defaultZone: http://localhost:9991/eureka/
267+
instance:
268+
instance-id: city2Id
269+
270+
```
271+
272+
3.编码
273+
274+
275+
276+
city2 -> 远程对象- >city
277+
278+
远程对象: RestTemplate ,将该对象 放入IoC容器
279+
280+
```java
281+
/*
282+
* Created by 颜群
283+
*/
284+
//将远程访问对象 放入IoC容器
285+
@Configuration
286+
public class ConfigBean {
287+
288+
@Bean
289+
@LoadBalanced//负载均衡
290+
public RestTemplate getRestTemplate(){
291+
return new RestTemplate() ;
292+
}
293+
}
294+
295+
```
296+
297+
298+
299+
(city2 -> controller) ->远程对象-> (controller - >city)
300+
301+
```java
302+
//远程访问city中的方法(ribbon)
303+
@GetMapping("/findCityByIdRibbon/{id}")
304+
public Message findCityByIdRibbon(@PathVariable("id") Integer id){
305+
System.out.println("ribbon调用远程方法");
306+
//返回值 = getForObject( 映射地址,返回值类型,参数)
307+
// return restTemplate.getForObject( "http://city/queryCityBiId/" , Message.class,id );//方法的返回值,就是远程访问方法的返回值
308+
return restTemplate.getForObject( "http://city/queryCityBiId/"+id , Message.class );//方法的返回值,就是远程访问方法的返回值
309+
// restTemplate.getForEntity() ;
310+
}
311+
312+
```
313+
314+
315+
316+
### 熔断器 与服务雪崩
317+
318+
![1591924428078](SpringCloud.assets/1591924428078.png)
319+
320+
321+
322+
Feign+Hystrix
323+
324+
具体实现:
325+
326+
1.引入依赖
327+
328+
​ 不用额外引入依赖。(因为Feign内部已经支持Hystrix)
329+
330+
2.配置
331+
332+
​ 开启Hystrix
333+
334+
​ A(开启)->B
335+
336+
city2(开启)->city
337+
338+
```xml
339+
#开启熔断
340+
feign:
341+
hystrix:
342+
enabled: true
343+
344+
```
345+
346+
347+
348+
3.编码
349+
350+
city2 -> 远程对象(CityClient) ->city
351+
352+
新建CityClient的实现类:
353+
354+
```java
355+
/*
356+
* Created by 颜群
357+
*/
358+
@Component
359+
public class CityClientImpl implements CityClient {
360+
361+
@Override
362+
public Message queryCityBiId(Integer id) {
363+
return new Message(false, StatusCode.ERROR, "请求失败,触发了本地熔断");
364+
}
365+
366+
@Override
367+
public Message addCity(City city) {
368+
return new Message(false, StatusCode.ERROR, "请求失败,触发了本地熔断");
369+
}
370+
}
371+
372+
```
373+
374+
375+
376+
开启熔断注解
377+
378+
CityClient接口
379+
380+
```java
381+
@FeignClient(value="city" , fallback = CityClientImpl.class)
382+
public interface CityClient {
383+
@GetMapping("queryCityBiId/{id}")
384+
public Message queryCityBiId(@PathVariable("id") Integer id) ;
385+
386+
@PostMapping("addCity")
387+
public Message addCity(@RequestBody City city) ;
388+
}
389+
390+
```
391+
392+
测试
393+
394+
![1591926522084](SpringCloud.assets/1591926522084.png)1.正常 eureka、city、city2
395+
396+
2.熔断:将city关闭后,再试
397+
398+
399+
400+
## 路由网关 Zuul
401+
402+
Zuul:映射
403+
404+
可以给 真实服务地址 做一个 虚拟映射,后续客户端只能通过 虚拟的映射地址 来访问我们真实的服务,可以保护我们的真实的服务。
405+
406+
![1592357856351](SpringCloud.assets/1592357856351.png)
407+
408+
创建网关服务micro_zuul
409+
410+
1.引入依赖
411+
412+
```xml
413+
<dependencies>
414+
415+
416+
417+
<dependency>
418+
<groupId>org.springframework.cloud</groupId>
419+
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
420+
</dependency>
421+
<dependency>
422+
<groupId>org.springframework.cloud</groupId>
423+
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
424+
</dependency>
425+
426+
<dependency>
427+
<groupId>org.springframework.cloud</groupId>
428+
<artifactId>spring-boot-starter-web</artifactId>
429+
</dependency>
430+
431+
432+
</dependencies>
433+
```
434+
435+
436+
437+
2.配置
438+
439+
application.yml
440+
441+
```.xml
442+
eureka:
443+
client:
444+
service-url:
445+
defaultZone: http://localhost:9991/eureka/
446+
447+
spring:
448+
application:
449+
name: zuul
450+
server:
451+
port: 10001
452+
453+
zuul:
454+
prefix: /yq
455+
ignored-services: "*"
456+
# ignored-services: "city"
457+
routes:
458+
city:
459+
path: /mycity/**
460+
461+
```
462+
463+
464+
465+
3.编码
466+
467+
```java
468+
@EnableEurekaServer//启动eureka服务
469+
@SpringBootApplication
470+
@EnableZuulProxy
471+
public class MicroZuulApplication {
472+
473+
public static void main(String[] args) {
474+
SpringApplication.run(MicroZuulApplication.class, args);
475+
}
476+
477+
}
478+
```
479+
480+
481+
482+
访问:
483+
484+
http://localhost:10001/yq/mycity/queryCities
485+
486+
http://localhost:10001:Zuul的地址
487+
488+
city:微服务在Eureka中的名字
489+
490+
queryCities:要访问真实服务的地址
491+
492+
493+
494+
## 分布式配置中心 Spring cloud Config
495+
496+
之前的微服务缺点:
497+
498+
1.项目-打包jar->运维,如果发现问题 需要返工。
499+
500+
2.很多微服务项目 的配置文件,存在着重复。
501+
502+
![1592807710385](SpringCloud.assets/1592807710385.png)
503+
504+
本例: micro_zuul ->Config Server -> gitHub
505+
506+
步骤1:
507+
508+
在github上建立 共配置的仓库:micro_config_rep
509+
510+
步骤2:
511+
512+
config约定,github上的配置文件命名必须是:A-B.yml /properties
513+
514+
将 原项目中的 配置文件A-B.yml,上传到github的micro_config_rep中
515+
516+
步骤3:
517+
518+
创建config项目
519+
520+
(1)引入依赖
521+
522+
```xml
523+
<dependency>
524+
<groupId>org.springframework.cloud</groupId>
525+
<artifactId>spring-cloud-config-server</artifactId>
526+
</dependency>
527+
```
528+
529+
(2)配置
530+
531+
application.yml
532+
533+
```xml
534+
spring:
535+
application:
536+
name: config
537+
cloud:
538+
config:
539+
server:
540+
git:
541+
uri: https://github.com/coding-technology/micro_config_rep.git
542+
server:
543+
port: 10010
544+
545+
```
546+
547+
548+
549+
550+
551+
(3)编码
552+
553+
```java
554+
@EnableConfigServer
555+
@SpringBootApplication
556+
public class MicroConfigApplication {
557+
558+
public static void main(String[] args) {
559+
SpringApplication.run(MicroConfigApplication.class, args);
560+
}
561+
562+
}
563+
564+
```
565+
566+
567+
568+
步骤4:改造本地的微服务 micro_zuul
569+
570+
(1)引入依赖
571+
572+
```xml
573+
574+
<dependency>
575+
<groupId>org.springframework.cloud</groupId>
576+
<artifactId>spring-cloud-starter-config</artifactId>
577+
</dependency>
578+
579+
```
580+
581+
(2)配置
582+
583+
说明:建议将云端的配置命名 bootstrap.yml/properties, bootstrap的优先级高于application.
584+
585+
586+
587+
(3)编码:省略

0 commit comments

Comments
 (0)