如果您对无法使用spring-cloud-config-server和无法使用此文件夹为保护您的隐私手机感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解无法使用spring-cloud-conf
如果您对无法使用 spring-cloud-config-server和无法使用此文件夹 为保护您的隐私手机感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解无法使用 spring-cloud-config-server的各种细节,并对无法使用此文件夹 为保护您的隐私手机进行深入的分析,此外还有关于0701-spring cloud config-简介、Config Server开发、Config Client开发、spring cloud config client No instances found of configserver (esc-eureka-config)、Spring Cloud Config Server、spring cloud config server 乱码的实用技巧。
本文目录一览:- 无法使用 spring-cloud-config-server(无法使用此文件夹 为保护您的隐私手机)
- 0701-spring cloud config-简介、Config Server开发、Config Client开发
- spring cloud config client No instances found of configserver (esc-eureka-config)
- Spring Cloud Config Server
- spring cloud config server 乱码
无法使用 spring-cloud-config-server(无法使用此文件夹 为保护您的隐私手机)
如何解决无法使用 spring-cloud-config-server?
我正在使用 spring boot 2.4.5 并尝试配置 spring 云服务器以从本地 git 存储库读取属性文件。下面的链接是服务启动和spring cloud服务器启动日志,
限制服务: https://drive.google.com/file/d/1U6o2UzdHDquyzsyUv0Im3h0RqbFXaCJG/view?usp=sharing
spring-cloud-config-server: https://drive.google.com/file/d/1A5J0S7sZRMQfeb2Hv94slgt4o5xtjsEo/view?usp=sharing
所有配置后,URL,http://localhost:8888/limits-service/default返回404如下,
白标错误页面 此应用程序没有针对 /error 的显式映射,因此您将其视为后备。
2021 年 5 月 20 日星期四 14:07:14 IST 出现意外错误(类型=未找到,状态=404)。 没有可用的消息
代码组件:
课程限制:
package com.home.microservices.limitsservice.bean;
public class Limits {
private int minimum;
private int maximum;
public Limits() {
super();
}
public Limits(int minimum,int maximum) {
super();
this.minimum = minimum;
this.maximum = maximum;
}
public int getMinimum() {
return minimum;
}
public void setMinimum(int minimum) {
this.minimum = minimum;
}
public int getMaximum() {
return maximum;
}
public void setMaximum(int maximum) {
this.maximum = maximum;
}
@Override
public String toString() {
return "Limits [minimum=" + minimum + ",maximum=" + maximum + "]";
}
}
控制器:
package com.home.microservices.limitsservice.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.home.microservices.limitsservice.bean.Limits;
import com.home.microservices.limitsservice.Configuration;
@RestController
public class LimitsController {
@Autowired
private Configuration configuration;
@GetMapping("/limits")
public Limits retrieveLimits() {
return new Limits(configuration.getMinimum(),configuration.getMaximum());
}
}
配置:
@Component
@ConfigurationProperties("limits-service")
public class Configuration {
private int minimum;
private int maximum;
public int getMinimum() {
return minimum;
}
public void setMinimum(int minimum) {
this.minimum = minimum;
}
public int getMaximum() {
return maximum;
}
public void setMaximum(int maximum) {
this.maximum = maximum;
}
}
application.properties(limits-service):
logging.level.org.springframework = debug
spring.application.name=limits-service
spring.config.import=optional:configserver:http://localhost:8888
limits-service.minimum=3
limits-service.maximum=997
pom.xml 依赖项(限制服务):
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
class SpringCloudconfigserverApplication:
@Enableconfigserver
@SpringBootApplication
public class SpringCloudconfigserverApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudconfigserverApplication.class,args);
}
}
application.properties(spring-cloud-config-server):
logging.level.org.springframework = debug
spring.application.name=spring-cloud-config-server
server.port=8888
spring.cloud.config.server.git.uri=file:///D:/Learning/Microservices/git-localconfig-repo
spring.cloud.config.import-check.enabled=false
pom.xml 依赖项(spring-cloud-config-server):
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Git repo 目录位置(我使用 Windows):
D:\Learning\Microservices\git-localconfig-repo\limits-service.properties
解决方法
您阅读 application.properties 文件添加组件字段@value
@Component
@PropertySource("file:D:\Learning\Microservices\git-localconfig-
repo\application.properties")
@ConfigurationProperties(prefix="limits-service")
public class Configuration {
@Value("${minimum}")
private int minimum;
@Value("${maximum}")
private int maximum;
public int getMinimum() {
return minimum;
}
public void setMinimum(int minimum) {
this.minimum = minimum;
}
public int getMaximum() {
return maximum;
}
public void setMaximum(int maximum) {
this.maximum = maximum;
}
}
这个文件application.properties
logging.level.org.springframework = debug
spring.application.name=limits-service
spring.config.import=optional:configserver:http://localhost:8888
limits-service.minimum=3
limits-service.maximum=997
0701-spring cloud config-简介、Config Server开发、Config Client开发
一、概述
参看地址:
https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_spring_cloud_config
https://gitee.com/itmuch/spring-cloud-book
1.1、为什么需要统一管理配置
集中管理、不同环境不同配置、运行期间动态调整配置、自动刷新
默认都是配置在配置文件中、数据库等中
1.2、简介
主要有两种分布式配置Zookeeper、Consul。
其实还有许多,如百度的disconf、阿里的diamond、携程的apollo
为分布式系统外部化配置提供了服务器端和客户端的支持。它包括Config Server和Config Client两部分。由于Config Server和Config Client都实现了对Spring Environment 和PropertySource抽象的映射,因此,Spring Cloud Config非常适合Spring应用程序,当然也可以与任何其他语言编写的应用程序配合使用。
Config Server是一个可横向扩展、集中式的配置服务器。它用于集中管理应用程序各个环境下的配置。默认使用Git存储配置内容(也可以使用Subversion,本地文件系统或Vault存储配置。)因此可以方便的实现对配置的版本控制与内容审计等
Config Client是Config Server的客户端,用于操作存储在Config Server中的配置属性。
1.3、架构图
在Config Server可以有dev、stage、prod等环境
二、Config Server开发
2.1、项目搭建
如git地址:https://github.com/bjlhx15/spring-cloud.git
增加pom
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
启动类增加注解
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
2.2、增加配置项目git
如:https://github.com/bjlhx15/spring-cloud-config-test-repo.git
2.3、在application.yml中增加
server:
port: 8080
spring:
cloud:
config:
server:
git:
uri: https://github.com/bjlhx15/spring-cloud-config-test-repo
启动项目测试即可.
2.4、访问地址
HTTP服务具有以下形式的资源映射规则:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
其实是按照格式随意拼接即可。如:http://localhost:8080/aa-profile.properties 或者 http://localhost:8080/aa-profile.yml
其中label是git的分支版本。
2.5、测试
增加foobar-dev.yml测试。
访问:http://localhost:8080/foobar-dev.yml,发现是配置的文件,。
即访问优先级:先找到能匹配的文件【label/具体文件名】,如果不匹配即找到application配置文件。
平时使用建议使用一个配置文件即可
三、Config Client开发
3.1、创建项目
git地址:https://github.com/bjlhx15/spring-cloud.git
增加pom【注意是spring-cloud-starter-config】
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
配置文件
新增一个bootstrap.yml
spring:
cloud:
config:
uri: http://localhost:8080
name: foobar
profile: dev
label: master # 当configserver的后端存储是Git时,默认就是master
application: #和配置文件匹配最好
name: foobar
在application.yml
server:
port: 8040
原因是:Spring云上下文
Spring Cloud应用程序通过创建“bootstrap”上下文来运行,该上下文是主应用程序的父上下文。【bootstrap.yml 优先级高于application.yml】开箱即用,它负责从外部源加载配置属性,并且还解密本地外部配置文件中的属性。这两个上下文共享一个环境,它是任何Spring应用程序的外部属性的来源。bootstrap属性以高优先级添加,因此默认情况下它们不能被本地配置覆盖。
bootstrap程序上下文默认约定:使用bootstrap.yml【bootstrap.properties】覆盖application.yml(或.properties)
但是可以使用spring.cloud.bootstrap.enabled=false禁用
bootstrap.*里面的配置→链接Config Server,加载远程配置之后→在加载application.*配置
2.2、增加启动类
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
2.3、增加测试
@RestController
public class ConfigClientController {
// 传统方式需要在配置文件加上配置 ,但是优先级低于使用bootstap加载的配置
@Value("${profile}")
private String profile;
@GetMapping("/profile")
public String getProfile() {
return this.profile;
}
}
注意点:
1、pom的spring-cloud-starter-config不要引错;
2、不要不适用bootstrap.yml。否则默认是8888端口
3、如果本地属性也有远端属性,以bootstrap.yml先与本地application为主,故使用远端属性profile
4、注意git上的配置文件如果是yml类型,注意属性“:”后有空格。。
spring cloud config client No instances found of configserver (esc-eureka-config)
报错:
2018-04-28 12:00:38.812 WARN 4468 --- [ main] com.netflix.discovery.DiscoveryClient : Using default backup registry implementation which does not do anything.
2018-04-28 12:00:38.816 INFO 4468 --- [ main] com.netflix.discovery.DiscoveryClient : Not registering with Eureka server per configuration
2018-04-28 12:00:38.823 INFO 4468 --- [ main] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp 1524888038821 with initial instances count: 0
2018-04-28 12:00:38.943 WARN 4468 --- [ main] lientConfigServiceBootstrapConfiguration : Could not locate configserver via discovery
java.lang.IllegalStateException: No instances found of configserver (esc-eureka-config)
at org.springframework.cloud.config.client.ConfigServerInstanceProvider.getConfigServerInstance(ConfigServerInstanceProvider.java:25) ~[spring-cloud-config-client-2.0.0.RC1.jar:2.0.0.RC1]
at org.springframework.cloud.config.client.DiscoveryClientConfigServiceBootstrapConfiguration.refresh(DiscoveryClientConfigServiceBootstrapConfiguration.java:80) [spring-cloud-config-client-2.0.0.RC1.jar:2.0.0.RC1]
at org.springframework.cloud.config.client.DiscoveryClientConfigServiceBootstrapConfiguration.startup(DiscoveryClientConfigServiceBootstrapConfiguration.java:66) [spring-cloud-config-client-2.0.0.RC1.jar:2.0.0.RC1]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
at org.springframework.context.event.ApplicationListenerMethodAdapter.doInvoke(ApplicationListenerMethodAdapter.java:264) [spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.context.event.ApplicationListenerMethodAdapter.processEvent(ApplicationListenerMethodAdapter.java:182) [spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.context.event.ApplicationListenerMethodAdapter.onApplicationEvent(ApplicationListenerMethodAdapter.java:144) [spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) [spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) [spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) [spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:400) [spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:354) [spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:888) [spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553) [spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:137) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:197) [spring-cloud-context-2.0.0.RC1.jar:2.0.0.RC1]
at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:104) [spring-cloud-context-2.0.0.RC1.jar:2.0.0.RC1]
at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:70) [spring-cloud-context-2.0.0.RC1.jar:2.0.0.RC1]
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) [spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) [spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) [spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127) [spring-context-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:74) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:358) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:317) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at com.acgist.esceurekagateway.EscEurekaGatewayApplication.main(EscEurekaGatewayApplication.java:16) [classes/:na]
上面原因可能是,配置中心没有启动,或者名称错误。
当然还有可能是注册中心eureka.client.service-url.defaultZone=配置没有放在bootstrap.properties里面。
Spring Cloud Config Server
Spring Cloud Config Server
<parent> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Angel.SR4</version> </parent>
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies>
application.yml
spring: cloud: config: server: git: uri: //git RUI searchPaths: ConfigData
Application.java
@SpringBootApplication @EnableConfigServer public class Application { public static void main(String[] args) { SpringApplication.run(Application .class, args); } }
That''s It!
spring cloud config server 乱码
spring-cloud-config-sever 使用git存放配置,文件 application.properties 内容:
info.foo: bar
ss: 你好
访问 http://localhost:8888/foo/development
返回的内容中文乱码
{"name":"foo","profiles":["development"],"label":"master","version":"8e15704bdca3bc0a5423ada1fccd2050cad8eac8","state":null,"propertySources":[{"name":"file://C:\\Users\\sub/config-repo/application.properties","source":{"ss":"ä½ å¥½","info.foo":"bar"}}]}
今天关于无法使用 spring-cloud-config-server和无法使用此文件夹 为保护您的隐私手机的讲解已经结束,谢谢您的阅读,如果想了解更多关于0701-spring cloud config-简介、Config Server开发、Config Client开发、spring cloud config client No instances found of configserver (esc-eureka-config)、Spring Cloud Config Server、spring cloud config server 乱码的相关知识,请在本站搜索。
本文标签: