本文共 8952 字,大约阅读时间需要 29 分钟。
分享一下我老师大神的人工智能教程!零基础,通俗易懂!
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
Spring Cloud是Spring总多的Project中的一个,它提供了一整套的工具帮助系统架构师们在进行分布式设计的时候可以拿来即用, 在创建和发布微服务时极为便捷和有效。本系列文章将会使用最简单的例子和最为容易的方式来学习Spring Cloud。本文将会介绍如何引入spring cloud config在微服务的架构中实现统一配置管理。
项目 | 详细 |
---|---|
Config Service | Spring Cloud Config:统一配置管理服务 |
Dashboard Service | Hystrix Dashboard |
Api Route Service | Zuul:Api Gateway |
Discovery Service | Eureka:服务发现 |
User Service | RESTFUL的用户相关的服务 |
Org Service | RESTFUL的组织相关的服务 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 4.0.0 com.liumiaocn.demo.springcloud configservice 0.0.1-SNAPSHOT jar configservice Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.4.3.RELEASE UTF-8 UTF-8 1.8 org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-config-server org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies Camden.SR3 pom import org.springframework.boot spring-boot-maven-plugin
需要加入EnableConfigServer注解。
package com.liumiaocn.demo.springcloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableConfigServer@EnableEurekaClientpublic class ConfigserviceApplication { public static void main(String[] args) { SpringApplication.run(ConfigserviceApplication.class, args); }} 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
server.port=8888spring.application.name=configserviceeureka.client.serviceUrl.defaultZone=http://localhost:8801/eureka/spring.profiles.active=native 1 2 3 4 5
项目 | 详细 |
---|---|
server.port | configService提供服务所用Port |
spring.application.name | 向Eureka Server进行注册时使用的服务名 |
eureka.client.serviceUrl.defaultZone | 注意此处的8801端口号需要跟Server端一致。 |
spring.profiles.active | spring cloud config支持本地/svn/git,设定为native表示为本地方式 |
项目 | 详细 |
---|---|
命令 | mvn clean package |
执行场所 | pom所在目录 |
目标文件所在目录 | 工程根目录下Target |
目标文件名称 | configservice-0.0.1-SNAPSHOT.jar |
为orgservice准备如下两个设定文件分别对应于dev环境和test环境
demo.word=hellodev 1
demo.word=hellotest 1
>启动各个服务后,也启动configservice
项目 | 访问方法(URL) |
---|---|
orgservice dev | |
orgservice test |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 4.0.0 com.liumiaocn.demo.springcloud orgservice 0.0.1-SNAPSHOT jar orgservice Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.4.3.RELEASE UTF-8 UTF-8 1.8 org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-starter-eureka org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies Camden.BUILD-SNAPSHOT pom import org.springframework.boot spring-boot-maven-plugin spring-snapshots Spring Snapshots https://repo.spring.io/snapshot true spring-milestones Spring Milestones https://repo.spring.io/milestone false
server.port=9002spring.application.name=orgserviceeureka.client.serviceUrl.defaultZone=http://localhost:8801/eureka/spring.cloud.config.uri=http://localhost:8888/spring.cloud.config.profile=test 1 2 3 4 5 6
项目 | 详细 |
---|---|
server.port | orgService提供服务所用Port |
spring.application.name | 向Eureka Server进行注册时使用的服务名 |
eureka.client.serviceUrl.defaultZone | 注意此处的8801端口号需要跟Server端一致。 |
spring.cloud.config.profile | 选择了服务器侧的orgservice-test.properties作为配置设定文件 |
spring.cloud.config.uri | 服务器侧的URI,此处为 |
为了验证真正读入了服务器侧的设定文件,我们使用Spring的Value注解读入设定文件中的demo.word
package com.liumiaocn.demo.springcloud;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication@RestController@RequestMapping("/org")@EnableEurekaClientpublic class OrgserviceApplication { @Value("${demo.word:default-value}") String strDemoWord; @RequestMapping("/detail") public String Detail(){ return "## The detail information about: Organization : with demo word: "+strDemoWord; } public static void main(String[] args) { SpringApplication.run(OrgserviceApplication.class, args); }} 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
>本文介绍了Spring Cloud Config的Server端和Client端如何使用,至次,通读这六篇文章,大体一个小时时间,对有spring基础的初学者应该能够执行搭建结合了Eureka/Zuul/Hystrix/Spring Cloud Config等常用组件进行Sample工程的搭建和基础入门。
但是为了简单起见,比如设定文件只是使用了application.properties,能够不设定大体能运行的选项基本都没有设定,如何更好的设计和搭建分布式的微服务架构,在其他的文章中我们会继续讨论。