网址:https://github.com/
配置中心
原理:
github上的配置,名为School-dev.yml
spring:
application:
name: userClient
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.0.250/school
username: root
password: ps123456
server:
port: 80
eureka:
instance:
hostname: localhost
#注册到注册中心的微服务都是IP地址
preferIpAddress: true
client:
#是否注册到注册中心
registerWithEureka: true
#是否抓取注册中心的注册信息
fetchRegistry: true
serviceUrl:
#微服务和客户端用来注册和发现的地址,此处为linux虚拟机运行地址
defaultZone: http://192.168.174.129:8761/eureka/
application.yml 配置中心服务引用github仓库(自己创建的)
spring:
cloud:
config:
server:
git:
uri: https://github.com/qq1073674233/ConfigCenter #github路径
server:
port: 8888 #不指定端口,默认为8080
###配置中心依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
读取资源的两种类型均可:yml,properties
URL:http://localhost:8888/School-dev.yml
URL:http://localhost:8888/School-dev.properties
application.name错误
spring:
application:
name: Schoo
github上名字为:School-dev.yml
应为 School
2018-12-04 00:11:23.183 WARN 7804 --- [ restartedMain] ationConfigEmbeddedWebApplicationContext :
Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'inMemoryDatabaseShutdownExecutor' defined in class path resource
[org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.class]:
Unsatisfied dependency expressed through method 'inMemoryDatabaseShutdownExecutor' parameter 0;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'dataSource' defined in class path resource
错误:服务启动时间早于注册中心
Cannot execute request on any known server
2018-12-04 08:11:31.965 WARN 5796 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient:
DiscoveryClient_USERSERVICE/OuYang:userService:8881 - registration failed Cannot execute request on any known server
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
默认访问方式:http://localhost:8888/School/dev,读取的是properties格式
微服务启动前配置bootstrap.yml
#注册中心的位置在IP和端口
spring:
application:
name: School #github已定义的项目名
profiles:
active: dev #github已定义的名字,推荐:开发环境dev,测试环境test,生产环境,gene
cloud:
config:
uri: http://localhost:8888
#该文件等同于http://localhost:8888/School-dev.yml
#此文件需添加spring-cloud-starter-config依赖才会读取
读取bootstrap.yml文件的依赖
<dependency> <!--此依赖才可读取配置资源bootstrap.yml-->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
main类添加注解 @EnableConfigServer
异常:loc header execption 一定是jar包损坏
zuul
路由配置application.yml , 均衡配置客户端,程序访问的入口
spring:
application:
name: zuul
server:
port: 80
eureka:
instance:
hostname: localhost
#注册到注册中心的微服务都是IP地址
preferIpAddress: true
client:
#是否注册到注册中心
registerWithEureka: false
#是否抓取注册中心的注册信息
fetchRegistry: true
serviceUrl:
#微服务和客户端用来注册和发现的地址
defaultZone: http://192.168.174.129:8761/eureka/
访问地址,服务名为全小写userClient→userclient
http://localhost/userclient/userList.html
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-a47e74522c.css" rel="stylesheet">
</div>