分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
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的组织相关的服务 |
统一配置管理之服务器侧
Pom详细
<?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> <groupId>com.liumiaocn.demo.springcloud</groupId> <artifactId>configservice</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>configservice</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-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>Camden.SR3</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>
- 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
configservice
需要加入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 | http://localhost:8801/eureka/ 注意此处的8801端口号需要跟Server端一致。 |
spring.profiles.active | spring cloud config支持本地/svn/git,设定为native表示为本地方式 |
生成Package
项目 | 详细 |
---|---|
命令 | mvn clean package |
执行场所 | pom所在目录 |
目标文件所在目录 | 工程根目录下Target |
目标文件名称 | configservice-0.0.1-SNAPSHOT.jar |
orgservice的设定文件
为orgservice准备如下两个设定文件分别对应于dev环境和test环境
orgservice-dev.properties
demo.word=hellodev
- 1
orgservice-test.properties
demo.word=hellotest
- 1
启动
>启动各个服务后,也启动configservice
Eureka确认
orgservice设定文件确认
项目 | 访问方法(URL) |
---|---|
orgservice dev | http://localhost:8888/orgservice/dev |
orgservice test | http://localhost:8888/orgservice/test |
统一配置管理之Client侧
Pom
<?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> <groupId>com.liumiaocn.demo.springcloud</groupId> <artifactId>orgservice</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>orgservice</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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>Camden.BUILD-SNAPSHOT</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> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories></project>
- 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
设定文件
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 | http://localhost:8801/eureka/ 注意此处的8801端口号需要跟Server端一致。 |
spring.cloud.config.profile | 选择了服务器侧的orgservice-test.properties作为配置设定文件 |
spring.cloud.config.uri | 服务器侧的URI,此处为http://localhost:8888/ |
Demo代码
为了验证真正读入了服务器侧的设定文件,我们使用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,能够不设定大体能运行的选项基本都没有设定,如何更好的设计和搭建分布式的微服务架构,在其他的文章中我们会继续讨论。
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow