Eureka注册中心:《使用IntelliJ IDEA创建Spring Cloud服务注册中心》
服务提供者创建:《使用IntelliJ IDEA创建Spring Cloud的Eureka Client》
Ribbon实现负载均衡:《使用IntelliJ IDEA创建Ribbon项目实现负载均衡》
集成Feign的项目:《使用IntelliJ IDEA创建集成Feign的项目简化服务调用的网络连接》
Ribbon项目中使用Hystrix熔断器:《使用IntelliJ IDEA在Spring Cloud的Ribbon项目中使用Hystrix熔断器》
Feign项目中使用Hystrix熔断器:《使用IntelliJ IDEA在Spring Cloud的Feign项目中使用Hystrix熔断器》
熔断器可视化监控项目:《使用IntelliJ IDEA创建Spring Cloud的熔断器可视化监控项目》
配置中心Config:《使用IntelliJ IDEA创建Spring Cloud的路由网关Zuul》
为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。支持配置服务放在配置服务的内存中即本地,也支持放在远程 Git 仓库中。在 Spring Cloud Config 组件中,分两个角色,一是 Config Server,二是 Config Client。
本文主要讲述配置文件放在Config Server本地时的情况
创建Config Server
File---new---module---Spring Assistant
点击next
如下图选择Config Server
点击Finish
配置Config Server
pom.xml中添加eureka注册中心的依赖,因为需要向eureka注册中心(acyxdiscovery)注册服务和获取其它服务相关信息
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
AcyxconfigserverApplication.java添加注解@EnableConfigServer、@EnableDiscoveryClient
package com.acyx.configserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class AcyxconfigserverApplication {
public static void main(String[] args) {
SpringApplication.run(AcyxconfigserverApplication.class, args);
}
}
resources目录下新建application.yml,application.yml的功能和application.properties是一样的,但yml文件是树状结构,有更好的层次感,更易于理解。然后,删除原有的application.properties。如下图
在resources目录下添加config,如下图
在config中添加文件config-demo-dev.properties
version=1.0.0
demo.message=this is demo dev message
在config中添加文件config-demo-prod.properties
version=2.0.0
demo.message=this is demo prod message
application.yml
server:
port: 9201 # 默认8888 如果配置为其它端口,则客户端新增/修改配置文件需为 bootstrap.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
#配置当前服务的名称
spring:
application:
name: config-server #向注册中心进行注册的服务名
profiles:
active: native #设置为本地启动的方式,而不是通过git
cloud:
config:
server:
native:
searchLocations: classpath:/config # 配置文件所在目录
依次启动Eureka注册中心、acyxconfigserver
在浏览器中访问:http://localhost:9201/config-demo/dev
在浏览器中访问:http://localhost:9201/config-demo/prod
创建Config Client
File---new---module---Spring Assistant
点击next
选择Config Client
点击Finish
配置Config Client
pom.xml中添加eureka注册中心的依赖,因为需要向eureka注册中心(acyxdiscovery)注册服务和获取其它服务相关信息。添加spring-boot-starter-web依赖。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
AcyxconfigclientApplication.java添加注解@EnableDiscoveryClient
package com.acyx.configclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class AcyxconfigclientApplication {
public static void main(String[] args) {
SpringApplication.run(AcyxconfigclientApplication.class, args);
}
}
resources目录下新建application.yml,application.yml的功能和application.properties是一样的,但yml文件是树状结构,有更好的层次感,更易于理解。然后,删除原有的application.properties。
在Config Server的项目中,其默认端口为8888 ,实际配置的端口为9201,因为配置为了其它端口,则客户端新增/修改配置文件需为 bootstrap.yml,bootstrap 开头的配置文件会被优先加载和配置,将链接config-server 的链接配置在 bootstrap.yml 。因此,在resources目录下新建bootstrap.yml。
新建controller包,并添加ConfigClientTest.java
package com.acyx.configclient.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientTest {
@Value("${version}")
private String version;
@Value("${demo.message}")
private String message;
@RequestMapping(value = "/config-test", method = RequestMethod.GET)
public String clientTestLocal() {
return "get local config version:" + version + " message:"+message;
}
}
bootstrap.yml ,name的值和profile的值组合之后,就构成了Config Client访问配置服务中心Config Server的具体的配置文件名为:config-demo-dev.properties
spring:
application:
name: config-client
cloud:
config:
uri: http://localhost:9201 #配置服务中心的uri
name: config-demo #配置文件名称的前缀
enabled: true #开启配置
profile: dev #版本
label: "" #git配置的分支信息,如:master
application.yml
server:
port: 9300
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
此时,依次启动Eureka注册中心、acyxconfigserver、acyxconfigclient,然后在浏览器中访问:http://127.0.0.1:8761
然后,在浏览器中新建选项卡并输入:http://localhost:9300/config-test
此时,若将acyxconfigclient中bootstrap.yml中的profile的值从dev修改为prod,重新启动acyxconfigclient,在浏览器中新建选项卡并输入:http://localhost:9300/config-test ,即使用的配置文件为配置中心acyxconfigserver中resources下的config-demo-prod.properties中的值。
bootstrap.yml ,name的值和profile的值组合之后,就构成了Config Client访问配置服务中心Config Server的具体的配置文件名为:config-demo-prod.properties