创建空站点
站点pom.xml配置
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ji</groupId>
<artifactId>spring-clound-learning</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>spring-clound-learning</name>
<description>Spring Cloud project</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<modules>
<module>eureka-server</module>
<module>compute-service</module>
<module>eureka-feign</module>
<module>config-server</module>
<module>config-client</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.8.0.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
初始化配置信息到git仓库
Administrator@MS-20170309PMWZ MINGW64 /e
$ mkdir config-a
Administrator@MS-20170309PMWZ MINGW64 /e
$ cd config-a
Administrator@MS-20170309PMWZ MINGW64 /e/config-a
$ git init
Initialized empty Git repository in E:/config-a/.git/
Administrator@MS-20170309PMWZ MINGW64 /e/config-a (master)
$ mkdir spring-boot-learning-test
Administrator@MS-20170309PMWZ MINGW64 /e/config-a (master)
$ cd spring-boot-learning-test/
Administrator@MS-20170309PMWZ MINGW64 /e/config-a/spring-boot-learning-test (master)
$ vim config-client-demo.yml
from: local
Administrator@MS-20170309PMWZ MINGW64 /e/config-a/spring-boot-learning-test (master)
$ vim config-client-demo-dev.yml
from: local-dev
Administrator@MS-20170309PMWZ MINGW64 /e/config-a/spring-boot-learning-test (master)
$ cd ..
Administrator@MS-20170309PMWZ MINGW64 /e/config-a (master)
$ git add .
warning: LF will be replaced by CRLF in spring-boot-learning-test/config-client-demo.yml.
The file will have its original line endings in your working directory.
Administrator@MS-20170309PMWZ MINGW64 /e/config-a (master)
$ git commit -m save
[master (root-commit) 0da19b5] save
1 file changed, 1 insertion(+)
create mode 100644 spring-boot-learning-test/config-client-demo.yml
Administrator@MS-20170309PMWZ MINGW64 /e/config-a (master)
$ git remote add origin git@gitlab.yanhetec.com:yuanciLin/config-a.git
Administrator@MS-20170309PMWZ MINGW64 /e/config-a (master)
$ git remote -v
origin git@gitlab.yanhetec.com:yuanciLin/config-a.git (fetch)
origin git@gitlab.yanhetec.com:yuanciLin/config-a.git (push)
Administrator@MS-20170309PMWZ MINGW64 /e/config-a (master)
$ git push -u origin master
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 289 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
remote:
remote: The private project yuanciLin/config-a was successfully created.
remote:
remote: To configure the remote, run:
remote: git remote add origin git@gitlab.yanhetec.com:yuanciLin/config-a.git
remote:
remote: To view the project, visit:
remote: http://gitlab.yanhetec.com/yuanciLin/config-a
remote:
Branch master set up to track remote branch master from origin.
To gitlab.yanhetec.com:yuanciLin/config-a.git
* [new branch] master -> master
启动服务注册中心
启动Spring Cloud 基于Spring Boot 2.0.6的服务注册与发现(Eureka)中创建的服务注册中心
创建配置服务中心
pom.xml配置
<?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>
<parent>
<groupId>com.ji</groupId>
<artifactId>spring-clound-learning</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>config-server</artifactId>
<packaging>jar</packaging>
<name>config-server</name>
<description>Spring Cloud project</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 监控中心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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-config-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
yml文件配置
server:
port: 8771
eureka:
instance:
hostname: localhost
status-page-url: http://localhost:${server.port}/actuator/health
client:
registerWithEureka: true #否注册自身到eureka服务器
fetchRegistry: true #是否从eureka服务器获取注册信息
serviceUrl:
defaultZone: http://localhost:1111/eureka/
spring:
application:
name: cloud-config
cloud:
config:
label: master
discovery:
enabled: true #开启config服务发现,既开启集群环境,此开关开启时,client必须使用服务名链接config server。
server:
# git管理配置
git:
strict-host-key-checking: false
uri: git@gitlab.yanhetec.com:yuanciLin/config-a.git
search-paths: spring-boot-learning-test
username: yuanciLin
password: password01
clone-on-start: true
force-pull: true
basedir: /config-repo # git下载存储路径
# svn:
# uri: svn://localhost/demo/spring_cloud/trunk/config-repo/
# username: yuanciLin
# password: password01
# search-paths: spring-boot-learning-test
# default-label: trunk
# basedir: /config-repo
Application.java
@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
启动配置服务中心
打开浏览器输入http://localhost:1111/进入服务注册中心,点开cloud-config的status链接进入http://localhost:8771/actuator/health,确认服务以启动
创建获取配置中心配置的客户端示例服务
pom.xml配置
<?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.didispace</groupId>
<artifactId>config-client</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>config-client</name>
<description>Spring Cloud project</description>
<parent>
<groupId>com.ji</groupId>
<artifactId>spring-clound-learning</artifactId>
<version>1.0.0</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 监控中心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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-config</artifactId>
</dependency>
<!-- spring 配置读取(@ConfigurationProperties(prefix = "test")) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
bootstrap.yml文件配置
-
bootstrap.yml(bootstrap.properties)与application.yml(application.properties)执行顺序
-
bootstrap.yml(bootstrap.properties)用来程序引导时执行,应用于更加早期配置信息读取,如可以使用来配置application.yml中使用到参数等
-
application.yml(application.properties) 应用程序特有配置信息,可以用来配置后续各个模块中需使用的公共参数等。
-
bootstrap.yml 先于 application.yml 加载
-
server:
port: 8781
spring:
application:
name: config-client-demo
cloud:
config:
discovery:
enabled: true
service-id: cloud-config
profile: dev
label: master
eureka:
instance:
hostname: localhost
status-page-url: http://localhost:${server.port}/swagger-ui.html
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:1111/eureka/
发布示例服务
示例服务中from属性即中配置服务中心获取
@RefreshScope
@RestController
class TestController {
@Value("${from}")
private String from;
@GetMapping("/from")
public String from() {
return this.from;
}
}
启动示例服务
在浏览器输入地址http://localhost:8781/swagger-ui.html,测试示例服务如图