我有两个 Spring Boot 应用程序:
cloud-config
它具有以下文件结构:
????src
????main
????environment
????default
????application.yml
????bootstrap.yml
...
Run Code Online (Sandbox Code Playgroud)
application.yml:
server:
port: 8087
spring:
cloud:
config:
server:
git:
uri: ssh://path-to-repository.git
Run Code Online (Sandbox Code Playgroud)
bootstrap.yml:
spring:
application:
name: cloud-config
Run Code Online (Sandbox Code Playgroud)main-application:
????src
????main
????environment
????default
????bootstrap.yml
????logback.xml
Run Code Online (Sandbox Code Playgroud)
我有application.yml我main-application的 git 存储库。
同样在 git 存储库中,我有一个带有属性的文件:application-default.properties.
我想转移logback.xml到 git 存储库以使用 spring-cloud 获取它。我怎样才能做到这一点?
我有简单的微服务架构:
当用户尝试登录时,凭据正在从边缘服务传递到身份验证服务。身份验证服务从用户服务(使用@FeignClient)获取用户数据,如果用户名/密码匹配,则生成令牌。没什么好看的。
这种方法有一个“小”问题:/api/user/{username}用户服务中的端点,由身份验证服务用来获取用户的数据,可能被任何用户用来获取任何其他用户的数据(密码、角色等)。一种解决方案是以某种方式为具有角色的身份验证服务创建 JWT 令牌,AUTH_SERVICE并在用户服务端检查 JWT,如果角色与AUTH_SERVICE拒绝请求不同。
还有其他解决方案吗?
编辑
我认为我的设计很常见,但显然我应该首先更具体:
编辑2:
我最终将 auth-service 合并到了 user-service,这是几个 SO 用户的建议。在考虑之后,似乎没有必要为 JWT 生成单独的身份验证服务。我已经接受了@Abhijit Sarkar 的回答,因为它有一些有效的观点,尽管他对额外调用 auth-service 以验证令牌的有效性并不正确。
我已经使用 Spring Cloud Netflix 堆栈(Eureka、Zuul、Zipkin、配置服务器等)开发了几个微服务。是否有任何开源/免费解决方案可用于启动/关闭微服务实例。例如,如果连续 3 次检查 CPU 使用率 > 90%,它将再添加一个实例。
我有一个简单的 Spring Boot 应用程序,它有一个简单的 REST 客户端,看起来像这样:
@Service
public class MyRestClient {
private static final String url = "http://localhost:8080/";
private RestTemplate restTemplate;
@Autowired
public MyRestClient(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String invoke() {
return restTemplate.getForObject(url, String.class);
}
}
Run Code Online (Sandbox Code Playgroud)
这与 Spring Boot 完美配合。
现在我正在尝试添加Spring Cloud到项目中以实现功能区客户端负载平衡。我按照这里的链接:
https://spring.io/guides/gs/client-side-load-balancing/
或者这里,这似乎是复制和粘贴,但具有更多更新的依赖项:
http://www.baeldung.com/spring-cloud-rest-client-with-netflix-ribbon
即使没有添加任何注释MyRestClient,我添加以下内容的那一刻:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我收到以下异常:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.client.RestTemplate]: Factory method 'restTemplate' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/http/impl/client/HttpClients
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at …Run Code Online (Sandbox Code Playgroud) 我尝试通过spring-cloud-stream使用测试消息发送和接收MessageCollector。
当我对流云使用本机 java 序列化时一切正常,但是当我将序列化更改为 json 时,MessageCollector 返回带有string有效负载而不是SomeObject有效负载的GenericMessage 。
配置是
cloud.stream.default.contentType=application/json
Run Code Online (Sandbox Code Playgroud)
测试用例:
Message outMsg = new GenericMessage<SomeObject>(new SomeObject(1));
someChannel.send(outMsg);
GenericMessage<SomeObject> inMsg = (GenericMessage<SomeObject>) messageCollector.forChannel(someChannel).poll();
Assert.assertTrue(inMsg.getPayload() instanceof SomeObject);
Run Code Online (Sandbox Code Playgroud)
因此,断言是错误的。inMsg包含字符串负载(字符串包含 SomeObject 的有效 json 表示)。
我的问题是:我如何GenericMessage从 SomeObject接收有效载荷MessageCollector?
生产环境工作正常,无需显式映射到SomeObject.
java spring spring-messaging spring-cloud spring-cloud-stream
我想我在使用 RabbitMQ 实现 Spring Cloud Bus 方面陷入了死胡同。
请记住,这是通过Spring Boot 2.0.0.M7 和 Spring Cloud Finchley.M5 完成的,根据它们的发行说明应该是兼容的。
我试图让它发挥作用:
/actuator/bus-refreshPOST 调用接收刷新请求但是,我可以在服务器的日志中看到这一点:
o.s.cloud.bus.event.RefreshListener : Received remote refresh request. Keys refreshed []
Run Code Online (Sandbox Code Playgroud)
(空数组是罪魁祸首吗?在这种情况下,我真的不知道该怎么办:()
客户端没有任何内容(除非我手动向总线刷新端点发送 POST 请求)......在这种情况下,日志中的输出如下:
o.s.cloud.bus.event.RefreshListener : Received remote refresh request. Keys refreshed [config.client.version, message]
Run Code Online (Sandbox Code Playgroud)
当我查看 RabbitMQ(作为 Docker 容器运行)中的图表时,我看到了三个连接的应用程序:客户端、Zuul(反向代理)和配置服务器。
你能帮我解决这个问题吗?非常感谢。以下是我对应用程序的配置。
配置服务器 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.paromsoft</groupId>
<artifactId>config</artifactId>
<version>0.0.1-SNAPSHOT</version> …Run Code Online (Sandbox Code Playgroud) java spring-boot spring-cloud spring-cloud-config spring-cloud-bus
我正在尝试将 Sleuth 集成到我们的系统中。如果我使用带注释的接口@FeignClient,则一切正常。这些接口会自动检测,并且 Sleuth 标头会通过 REST 调用传播。
但是,我们有一些现有代码直接使用 Feign.Builder 和 Feign 注释接口(只是没有用 注释@FeignClient)。这段代码添加了一些自定义的请求拦截器、编码器、代理等。
例如:
// Feign REST interface
public interface MyService {
@RequestMapping(method = RequestMethod.GET, value = "/version")
String getVersion();
}
// Creating the builder
Feign.Builder builder = Feign.builder();
builder.requestInterceptor(new MyCustomInterceptor());
// + adding proxy, encoder, decoder, etc
// Using the builder
MyService myService = builder.target(MyService.class, "http://localhost:8080/myservice");
myService.getVersion();
Run Code Online (Sandbox Code Playgroud)
我希望这个旧代码传播 Sleuth 标头。有什么简单的方法来连接它吗?
(我想一种选择是重新设计我们的 Feign 接口以使用 @FeignClient 并重新设计所有自定义拦截器、编码器等的应用方式,但最终这可能需要大量工作并有很大风险。)
我是否需要做一个特殊的请求拦截器来手动注入这些(例如从自动装配的 Tracer)?有没有一种干净的方法(或现有的类)来做到这一点?
spring-boot spring-cloud feign spring-cloud-netflix spring-cloud-sleuth
我想使用 spring cloud 编写一个简单的网关,以便对第三方的请求似乎来自我的服务(就像我过去对 Zuul 所做的那样)。
github 页面上的示例以及官方文档中的示例似乎正是我想要的。即一个简单的控制器路由将所有请求代理到给定路由:
@GetMapping("/proxy/path/**")
public ResponseEntity<?> proxyPath(ProxyExchange<?> proxy) throws Exception {
String path = proxy.path("/proxy/path/");
return proxy.uri(home.toString() + "/foos/" + path).get();
}
Run Code Online (Sandbox Code Playgroud)
但我遇到的问题是,人工制品spring-cloud-starter-gateway不包括org.springframework.cloud.gateway.mvc.ProxyExchange.
为了获得ProxyExchange,我需要包含spring-cloud-gateway-mvc与spring-cloud-starter-gateway. 将这两个东西放在一起会导致以下错误:
***************************************************
Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway
at this time. Please remove spring-boot-starter-web dependency.
***************************************************
Run Code Online (Sandbox Code Playgroud)
因此,我不得不使用以下两个依赖项来使基本网关正常工作:
spring-cloud-gatewayspring-cloud-gateway-mvc哪个工作正常,直到我需要org.springframework.cloud.gateway.filter.*在项目中的其他地方使用这些包,这意味着我需要包括:
spring-cloud-gateway-core, 或者spring-cloud-starter-gateway这意味着我又回到了不兼容横幅。
无论如何,是否可以仅使用spring-cloud-starter-gateway依赖项来使用简单的 …
我正在尝试为 SQS 编写集成测试。
我有一个本地运行的localstack docker容器,其中运行 SQSTCP/4576
在我的测试代码中,我定义了一个端点设置为本地 4576 的 SQS 客户端,并且可以成功连接并创建队列、发送消息和删除队列。我还可以使用 SQS 客户端来接收消息并提取我发送的消息。
我的问题是,如果我删除手动接收消息的代码以允许另一个组件获取消息,似乎什么也没有发生。我有一个弹簧组件注释如下:
@Component
public class MyListener {
@SqsListener(value = "my_queue", deletionPolicy = ON_SUCCESS)
public void receive(final MyMsg msg) {
System.out.println("GOT THE MESSAGE: "+ msg.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
@RunWith(SpringRunner.class)
@SpringBootTest(properties = "spring.profiles.active=test")
public class MyTest {
@Autowired
private AmazonSQSAsync amazonSQS;
@Autowired
private SimpleMessageListenerContainer container;
private String queueUrl;
@Before
public void setUp() {
queueUrl = …Run Code Online (Sandbox Code Playgroud) 按照spring 指南构建 eureka 服务器、spring 云网关和示例休息服务。
但是,网关无法使用 eureka 服务器中的服务名称检索 url。网关和服务的注册似乎没问题。当提供实际端点而不是服务 id 时,它工作正常。我无法理解为什么网关没有从尤里卡解析服务 ID。我是否缺少任何配置?
错误信息:
2018-09-05 23:20:17.751 INFO 47037 --- [ctor-http-nio-2] c.netflix.loadbalancer.BaseLoadBalancer : Client: localhost instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=localhost,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2018-09-05 23:20:17.756 INFO 47037 --- [ctor-http-nio-2] c.n.l.DynamicServerListLoadBalancer : Using serverListUpdater PollingServerListUpdater
2018-09-05 23:20:17.760 INFO 47037 --- [ctor-http-nio-2] c.n.l.DynamicServerListLoadBalancer : DynamicServerListLoadBalancer for client localhost initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=localhost,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@107dc063
2018-09-05 23:20:17.822 ERROR 47037 --- [ctor-http-nio-2] .a.w.r.e.DefaultErrorWebExceptionHandler : …Run Code Online (Sandbox Code Playgroud) spring-boot spring-cloud netflix-eureka spring-cloud-gateway