【分布式配置中心】
项目中配置文件居多,为了统一进行配置和管理,所以将配置文件通过分布式配置中心组件spring cloud config进行管理。
配置服务可以存放在本地,或者存放在远程仓库git.
spring cloud config组件分为两个角色,一个是config server 一个是config client
相当于,config server统一读取仓库的配置信息,放到config-server这个服务中。其他的server client就到这个服务中读取配置信息。
1.在远程仓库中存放配置信息。
2.新建项目,引入config依赖。
3.建立启动类,@EnableConfigServer启动配置。
4.在application.prioerties配置文件中配置git仓库地址,仓库名称,分支,用户名和密码。同时配置自己的服务名,端口号,发布自己这个服务。
5.server client 服务需要调用配置文件信息就在项目中建立bootstrap.properties文件,除了配置自己的服务名和端口号,要对配置文件所在的服务的分支名,配置信息所在的地址和文件名。
6.在需要引入配置文件信息的地方通过@Value("${foo}")的方式进行引入。
【高可用分布式配置中心】
当服务实例很多,都需要从配置中心读取信息,我们可以将配置中心做成一个为服务,使其集群化,实现高可用。
1.建立一个eureka-server ,作为统一的服务注册中心@EnableEurekaServer启动。然后将config-server和config-client通过@EnableEurekaClient都发布到上面。
------------------------------------------------------
下面进行操作一下【分布式配置中心】:
步骤一:新建项目,引入config依赖。
<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>springcloud-config-server</groupId>
<artifactId>springcloud-config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- 分布式配置 -->
<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>
<!-- 注册与消费服务 必导 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
步骤二:建立启动类,@EnableConfigServer启动配置。
package com.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
步骤三:在application.prioerties配置文件中配置git仓库地址,仓库名称,分支,用户名和密码。
spring.application.name=config-server
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/
spring.cloud.config.server.git.searchPaths=respo
spring.cloud.config.label=master
spring.cloud.config.server.git.username=your username
spring.cloud.config.server.git.password=your password
启动一下访问:
步骤四:客户端需要引入配置信息时,就是调用这个server服务。pom文件引入的依赖信息和server上面引入的都是一样的
server client 服务需要调用配置文件信息就在项目中建立bootstrap.properties文件,除了配置自己的服务名和端口号,要对配置文件所在的服务的分支名,配置信息所在的地址和文件名。
spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:8888/
server.port=8881
步骤五:建立启动类,并进行调用配置信息
package com.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
@Value("${foo}")
String foo;
@RequestMapping(value="/hello")
public String hello() {
return foo;
}
}
【高可用分布式配置中心】
建立一个服务注册中心。
步骤一:导入pom中依赖,主要是spring-cloud-starter-eureka-server
<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>springcloud-eureka-server</groupId>
<artifactId>springcloud-eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</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.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
步骤二:配置信息application.yml
server:
port: 8889
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
步骤三:启动类 (注意是:@EnableEurekaServer)
package com.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
步骤四:确保config-server和config-client都导入spring-cloud-starter-eureka-server依赖的前提下,在这两个项目中的启动类中加入@EnableEurekaClient 来将它们发布到新建的服务注册中心上面。
同时要在配置文件中配置服务发布中心的地址。
eureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka/
步骤五:启动eureka-server 再启动 config-server 再启动config-client