private static final Logger logger = Logger.getLogger(HelloWorldServer.class.getName());
private int port = 50051; private Server server;
public static void main(String[] args) throws IOException, InterruptedException { final HelloWorldServer server = new HelloWorldServer(); server.start(); server.blockUntilShutdown(); }
private void start() throws IOException { server = ServerBuilder.forPort(port) .addService(new GreeterImpl()) .build() .start(); logger.info("Server started, listening on " + port);
System.err.println("*** shutting down gRPC server since JVM is shutting down"); HelloWorldServer.this.stop(); System.err.println("*** server shut down"); } }); }
private final ManagedChannel channel; private final GreeterGrpc.GreeterBlockingStub blockingStub; private static final Logger logger = Logger.getLogger(HelloWorldClient.class.getName());
public static void main(String[] args) throws InterruptedException { HelloWorldClient client = new HelloWorldClient("127.0.0.1", 50051); try { String user = "world"; if (args.length > 0) { user = args[0]; } client.greet(user); } finally { client.shutdown(); } }
public HelloWorldClient(String host, int port) { channel = ManagedChannelBuilder.forAddress(host, port) .usePlaintext() .build();
➜ /Users/lintong/coding/java/grpc-java/compiler git:(ae49d275b) $ ../gradlew java_pluginExecutable -PskipAndroid=true * Skipping the build of Android projects because skipAndroid=true
> Configure project :grpc-compiler *** Building codegen requires Protobuf *** Please refer to https://github.com/grpc/grpc-java/blob/master/COMPILING.md#how-to-build-code-generation-plugin
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.3/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
var ( addr = flag.String("addr", "localhost:50051", "the address to connect to") name = flag.String("name", defaultName, "Name to greet") )
func main() { flag.Parse() // Set up a connection to the server. conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { log.Fatalf("did not connect: %v", err) } defer conn.Close() c := pb.NewGreeterClient(conn)
// Contact the server and print out its response. ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() r, err := c.SayHello(ctx, &pb.HelloRequest{Name: *name}) if err != nil { log.Fatalf("could not greet: %v", err) } log.Printf("Greeting: %s", r.GetMessage()) }
// 定义结构体 type Inventory struct { Material string Count uint } // 赋值 sweaters := Inventory{"wool", 17} // 定义模板 tmpl, err := template.New("test").Parse(` {{.Count}} items are made of {{.Material}} `) if err != nil { panic(err) } // 填充模板 err = tmpl.Execute(os.Stdout, sweaters) if err != nil { panic(err) }
输出
1 2
17 items are made of wool
2.if else
1.判断字符串是否相等
1 2 3 4 5 6 7 8 9 10 11
{{ if eq .Material "wool" }} Material is "wool" {{ else if eq .Material "not wool" }} Material is not "wool" {{ end }} {{ if eq .Count 17 }} Count is 17 {{ else if eq .Count 10 }} Count is not 10 {{ end }}
输出
1 2 3 4
Material is "wool"
Count is 17
2.多条件,与或非
1 2 3 4
{{ if or (eq .Material "not wool") (eq .Count 17) }} Count is 17 {{ end }}