Skip to content

Commit

Permalink
progress with #326
Browse files Browse the repository at this point in the history
  • Loading branch information
jvmlet committed Dec 8, 2022
1 parent 767510a commit c9f80c9
Show file tree
Hide file tree
Showing 24 changed files with 109 additions and 95 deletions.
7 changes: 5 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
grpcVersion=1.51.0
springBootVersion=2.7.6
springCloudVersion=2021.0.3


springBootVersion=3.0.0
springCloudVersion=2022.0.0-RC3

gradleErrorPronePluginVersion=3.0.1
errorProneVersion=2.16
lombokVersion=1.18.24
Expand Down
11 changes: 5 additions & 6 deletions grpc-spring-boot-starter-demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ facets {
}

grpcSpringBoot {
grpcSpringBootStarterVersion.set((String)null)
grpcSpringBootStarterVersion.set((String) null)
}
dependencyManagement {
imports {
mavenBom "org.springframework.boot:spring-boot-starter-parent:${springBootVersion}"
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
configurations.findAll{ cfg ->
configurations.findAll { cfg ->
if (cfg.name.startsWith("pureNetty")) {
cfg.exclude group: 'io.grpc', module: 'grpc-netty-shaded'

Expand All @@ -50,12 +50,11 @@ configurations.findAll{ cfg ->
}



}


extensions.facets.each{
if(it.name.endsWith("Test")) {
extensions.facets.each {
if (it.name.endsWith("Test")) {
configurations.getByName("${it.name}Implementation").extendsFrom(configurations.testImplementation)
configurations.getByName("${it.name}RuntimeOnly").extendsFrom(configurations.testRuntimeOnly)

Expand All @@ -75,7 +74,7 @@ dependencies {
implementation "org.springframework.security:spring-security-oauth2-resource-server"
implementation 'org.springframework.boot:spring-boot-starter-validation'


implementation("javax.annotation:javax.annotation-api:1.3.2")
implementation project(':grpc-spring-boot-starter')
implementation project(':grpc-client-spring-boot-starter')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.lognet.springboot.grpc.demo;

import javax.validation.Constraint;
import javax.validation.Payload;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import io.grpc.examples.GreeterOuterClass;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

public class PersonValidator implements ConstraintValidator<PersonConstraint, GreeterOuterClass.Person> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
<constraint annotation="org.lognet.springboot.grpc.demo.PersonConstraint"/>
</class>
<getter name="name">
<constraint annotation="javax.validation.constraints.NotEmpty"/>
<constraint annotation="jakarta.validation.constraints.NotEmpty"/>
</getter>
<getter name="nickName">

<!--should be empty for request message-->
<constraint annotation="javax.validation.constraints.Size">
<constraint annotation="jakarta.validation.constraints.Size">
<groups>
<value>org.lognet.springboot.grpc.validation.group.RequestMessage</value>
</groups>
Expand All @@ -25,21 +25,24 @@
</constraint>

<!--should NOT be empty for response message-->
<constraint annotation="javax.validation.constraints.NotEmpty">
<constraint annotation="jakarta.validation.constraints.NotEmpty">
<groups>
<value>org.lognet.springboot.grpc.validation.group.ResponseMessage</value>
</groups>
</constraint>
</getter>

<getter name="age">
<constraint annotation="javax.validation.constraints.Max">
<constraint annotation="jakarta.validation.constraints.Max">
<element name="value">120</element>
</constraint>
</getter>
<getter name="address"> <!-- invokes hasAddress() method, not getAddress(), hence AssertTrue -->
<constraint annotation="javax.validation.constraints.AssertTrue"/>
</getter>

<method name="hasAddress">
<return-value>
<constraint annotation="jakarta.validation.constraints.AssertTrue"/>
</return-value>
</method>


</bean>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.SocketUtils;
import org.springframework.test.util.TestSocketUtils;


import java.io.File;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -53,7 +54,7 @@ public abstract class ConfigServerEnvironmentBaseTest extends GrpcServerTestBase


public static void startConfigServer(Properties properties ) throws IOException, URISyntaxException {
int configPort = SocketUtils.findAvailableTcpPort();
int configPort = TestSocketUtils.findAvailableTcpPort();
File cfgFile = temporaryFolder.newFile("grpc-demo.properties");
try(OutputStream os = new FileOutputStream(cfgFile)) {
properties.store(os,null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.SocketUtils;
import org.springframework.test.util.TestSocketUtils;


import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;

Expand All @@ -28,7 +29,7 @@ public class EnvVarGrpcPortTest extends GrpcServerTestBase {

@ClassRule
public static EnvironmentVariables environmentVariables() {
randomPort = SocketUtils.findAvailableTcpPort();
randomPort = TestSocketUtils.findAvailableTcpPort();

environmentVariables = new EnvironmentVariables();
environmentVariables.set("GRPC_PORT", "" + randomPort);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class NettyTransportConfigTest1 extends GrpcServerTestBase {

@Override
protected GreeterGrpc.GreeterFutureStub beforeGreeting(GreeterGrpc.GreeterFutureStub stub) {
Assert.assertEquals(getPort(),gRpcServerProperties.getNettyServer().getPrimaryListenAddress().getPort());
Assert.assertNotEquals(getPort(),gRpcServerProperties.getNettyServer().getPrimaryListenAddress().getPort());
return stub;

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
grpc:
port:
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
management:
metrics:
export:
prometheus:
prometheus:
metrics:
export:
step: 10s
enabled: true
grpc:
Expand Down
19 changes: 8 additions & 11 deletions grpc-spring-boot-starter-demo/src/test/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
management:
metrics:
export:
prometheus:
prometheus:
metrics:
export:
enabled: false
simple:
simple:
metrics:
export:
enabled: false
endpoints:
web:
Expand All @@ -13,11 +15,6 @@ logging:
level:
root: INFO


---
spring:
config:
activate:
on-profile: "!default-port"
grpc:
port: 0
port: 0

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ java.util.Optional.ofNullable(grpcSpringBoot.getGrpcSpringBootStarterVersion().g
.ifPresent {v->
project.dependencies {
implementation("io.github.lognet:grpc-spring-boot-starter:${v}")
implementation("javax.annotation:javax.annotation-api:1.3.2")
}
}

Expand Down
9 changes: 6 additions & 3 deletions grpc-spring-boot-starter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ task testDependencyFatJar(type: org.springframework.boot.gradle.tasks.bundling.B
mainClass = 'org.lognet.grpc.demo.DemoApp'
setClasspath(configurations.testImplementation)
doFirst {
classpath sourceSets.getByName("test").java.outputDir
classpath sourceSets.getByName("test").java.classesDirectory
classpath sourceSets.getByName("main").output
}
archiveClassifier.convention("bootable-test")
Expand All @@ -150,7 +150,7 @@ jar {

// generate native config by executing test app in graalvm docker image
// then grab generated json files and filter to include class from `org.lognet.springboot.grpc` package only
dependsOn testDependencyFatJar
// dependsOn testDependencyFatJar
File aotDir = new File(buildDir, "native-configs");
File transformed = new File(aotDir, "transformed")
transformed.mkdirs()
Expand All @@ -159,6 +159,7 @@ jar {
include "*.*"
}
doFirst {
/*
exec {
commandLine("docker",
"run", "--rm",
Expand Down Expand Up @@ -200,7 +201,7 @@ jar {
}
}

*/
}

}
Expand Down Expand Up @@ -282,9 +283,11 @@ dependencies {
compileOnly "org.springframework.security:spring-security-oauth2-jose"
compileOnly 'org.springframework.boot:spring-boot-starter-validation'
compileOnly "org.springframework.boot:spring-boot-starter-actuator"
compileOnly "org.springframework.boot:spring-boot-starter-validation"
compileOnly 'org.springframework.cloud:spring-cloud-starter-consul-discovery'


testImplementation "javax.annotation:javax.annotation-api:1.3.2"
testImplementation "io.grpc:grpc-netty"
testImplementation 'org.springframework.boot:spring-boot-starter-test'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public void start() {
startDaemonAwaitThread();
log.info("gRPC Server started, listening on port {}.", server.getPort());


applicationContext.publishEvent(new GRpcServerInitializedEvent(applicationContext, server));
} catch (Exception e) {
throw new RuntimeException("Failed to start GRPC server", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.SocketUtils;

import java.io.IOException;
import java.net.InetSocketAddress;
Expand Down Expand Up @@ -157,7 +156,7 @@ public InetSocketAddress convert(String source) {
case 2:
port = Integer.parseInt(chunks[1]);
if (port < 1) {
port = SocketUtils.findAvailableTcpPort();
port = 0;
}
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import org.lognet.springboot.grpc.autoconfigure.consul.ServiceRegistrationMode;
import org.lognet.springboot.grpc.context.GRpcServerInitializedEvent;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
import org.springframework.context.SmartLifecycle;
import org.springframework.context.event.EventListener;
import org.springframework.core.io.Resource;
import org.springframework.util.SocketUtils;
import org.springframework.util.unit.DataSize;

import javax.annotation.PostConstruct;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.List;
Expand All @@ -25,7 +23,7 @@
@ConfigurationProperties("grpc")
@Getter
@Setter
public class GRpcServerProperties {
public class GRpcServerProperties implements InitializingBean {
public static final int DEFAULT_GRPC_PORT = 6565;
/**
* gRPC server port
Expand Down Expand Up @@ -67,31 +65,39 @@ public class GRpcServerProperties {
*/
private int shutdownGrace = 0;

@EventListener
public void onServerStarted(GRpcServerInitializedEvent event) {
runningPort = event.getServer().getPort();
}

public Integer getPortOrDefault() {
return Optional.ofNullable(port).orElse(DEFAULT_GRPC_PORT);
}

public Integer getRunningPort() {
if (null == runningPort) {
synchronized (this) {
if (null == runningPort) {
runningPort = Optional.ofNullable(port)
.map(p -> 0 == p ? SocketUtils.findAvailableTcpPort() : p)
.orElse(DEFAULT_GRPC_PORT);
}
}
}
return runningPort;
}

@Override
public void afterPropertiesSet() throws Exception {
Optional.ofNullable(nettyServer)
.map(NettyServerProperties::getPrimaryListenAddress)
.ifPresent(a -> port = a.getPort());
}

@Getter
@Setter
public static class RecoveryProperties{
public static class RecoveryProperties {
private Integer interceptorOrder;
}

@Getter
@Setter
public static class SecurityProperties {
private Resource certChain;
private Resource privateKey;
private Auth auth;

@Getter
@Setter
public static class Auth {
Expand Down Expand Up @@ -123,21 +129,15 @@ public static class NettyServerProperties {

private Boolean permitKeepAliveWithoutCalls;
/**
* grpc listen address. <P>If configured, takes precedence over {@code grpc.port} property value.</p>
* Supported format:
* <ul><li>{@code host:port} (if port is less than 1, uses random value)
* <li>{@code host:} (uses default grpc port, 6565 )</ul>
* grpc listen address. <P>If configured, takes precedence over {@code grpc.port} property value.</p>
* Supported format:
* <ul><li>{@code host:port} (if port is less than 1, uses random value)
* <li>{@code host:} (uses default grpc port, 6565 )</ul>
*/
private InetSocketAddress primaryListenAddress;

private List<InetSocketAddress> additionalListenAddresses;
}

@PostConstruct
public void init(){
Optional.ofNullable(nettyServer)
.map(NettyServerProperties::getPrimaryListenAddress)
.ifPresent(a->port = a.getPort());

}
}
Loading

0 comments on commit c9f80c9

Please sign in to comment.