Skip to content
Permalink
Browse files
Change some hard code to constants
  • Loading branch information
EarthChen committed Sep 6, 2021
1 parent 114a7d4 commit 6c1932eee699b14720605f80b66ac8a3cea36b15
Showing 10 changed files with 62 additions and 31 deletions.
@@ -44,7 +44,7 @@ class ApiConsumer {

DubboBootstrap bootstrap = DubboBootstrap.getInstance();
bootstrap.application(new ApplicationConfig("demo-consumer"))
.registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))
.registry(new RegistryConfig(TriSampleConstants.ZK_ADDRESS))
.reference(ref)
.start();

@@ -32,8 +32,8 @@ public static void main(String[] args) {

DubboBootstrap bootstrap = DubboBootstrap.getInstance();
bootstrap.application(new ApplicationConfig("demo-provider"))
.registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))
.protocol(new ProtocolConfig(CommonConstants.TRIPLE, 50051))
.registry(new RegistryConfig(TriSampleConstants.ZK_ADDRESS))
.protocol(new ProtocolConfig(CommonConstants.TRIPLE, TriSampleConstants.SERVER_POINT))
.service(service)
.start()
.await();
@@ -32,8 +32,8 @@ public static void main(String[] args) {

DubboBootstrap bootstrap = DubboBootstrap.getInstance();
bootstrap.application(new ApplicationConfig("demo-provider"))
.registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))
.protocol(new ProtocolConfig(CommonConstants.TRIPLE, 50051))
.registry(new RegistryConfig(TriSampleConstants.ZK_ADDRESS))
.protocol(new ProtocolConfig(CommonConstants.TRIPLE, TriSampleConstants.SERVER_POINT))
.service(service)
.start()
.await();
@@ -17,6 +17,7 @@

package org.apache.dubbo.sample.tri;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
@@ -31,12 +32,12 @@ public class GenericConsumer {
ref.setInterface("org.apache.dubbo.sample.basic.IGreeter2");
ref.setCheck(false);
ref.setTimeout(30000);
ref.setProtocol("tri");
ref.setProtocol(CommonConstants.TRIPLE);
ref.setGeneric("true");
ref.setLazy(true);
DubboBootstrap bootstrap = DubboBootstrap.getInstance();
bootstrap.application(new ApplicationConfig("demo-consumer"))
.registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))
.registry(new RegistryConfig(TriSampleConstants.ZK_ADDRESS))
.reference(ref)
.start();
this.generic = ref.get();
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.dubbo.sample.tri;


public class TriSampleConstants {

// macos 11 later the 50051 is occupied by system (pid=1!!!)
public static final int SERVER_POINT = 50052;

public static final String ZK_ADDRESS = "zookeeper://127.0.0.1:2181";

public static final String DEFAULT_ADDRESS = "tri://127.0.0.1:" + SERVER_POINT;
}
@@ -1,5 +1,6 @@
package org.apache.dubbo.sample.tri;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
@@ -20,12 +21,12 @@ public static void init() {
ref.setInterface("org.apache.dubbo.sample.tri.WrapGreeter");
ref.setCheck(false);
ref.setTimeout(30000);
ref.setProtocol("tri");
ref.setProtocol(CommonConstants.TRIPLE);
ref.setGeneric("true");
ref.setLazy(true);
DubboBootstrap bootstrap = DubboBootstrap.getInstance();
bootstrap.application(new ApplicationConfig("demo-consumer"))
.registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))
.registry(new RegistryConfig(TriSampleConstants.ZK_ADDRESS))
.reference(ref)
.start();
generic = ref.get();
@@ -14,24 +14,26 @@

public class GrpcProvider {
public static void main(String[] args) throws IOException, InterruptedException {
final Server server = ServerBuilder.forPort(50051)
final Server server = ServerBuilder.forPort(TriSampleConstants.SERVER_POINT)
.addService(ServerInterceptors.intercept(new GrpcPbGreeterImpl(new PbGreeterImpl()), new ServerInterceptor() {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> serverCall,
Metadata metadata, ServerCallHandler<ReqT, RespT> serverCallHandler) {
return serverCallHandler.startCall(new ForwardingServerCall.SimpleForwardingServerCall(serverCall) {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> serverCall,
Metadata metadata, ServerCallHandler<ReqT, RespT> serverCallHandler) {
return serverCallHandler.startCall(new ForwardingServerCall.SimpleForwardingServerCall(serverCall){
@Override
public void sendHeaders(Metadata headers) {
final String key = "user-attachment";
final Metadata.Key<String> metaKey = Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER);
if(metadata.containsKey(metaKey)){
headers.put(metaKey,metadata.get(metaKey));
}
super.sendHeaders(headers);
}
}, metadata);
};
}))
public void sendHeaders(Metadata headers) {
final String key = "user-attachment";
final Metadata.Key<String> metaKey = Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER);
if (metadata.containsKey(metaKey)) {
headers.put(metaKey, metadata.get(metaKey));
}
super.sendHeaders(headers);
}
}, metadata);
}

;
}))
.build();
server.start();
Runtime.getRuntime().addShutdownHook(new Thread() {
@@ -24,9 +24,7 @@ public static void init() {
ReferenceConfig<PbGreeter> ref = new ReferenceConfig<>();
ref.setInterface(PbGreeter.class);
ref.setCheck(false);
ref.setInterface(PbGreeter.class);
ref.setCheck(false);
ref.setUrl("tri://127.0.0.1:50051");
ref.setUrl(TriSampleConstants.DEFAULT_ADDRESS);
ref.setProtocol(CommonConstants.TRIPLE);
ref.setLazy(true);
ref.setTimeout(10000);
@@ -21,8 +21,8 @@ public static void main(String[] args) {

DubboBootstrap bootstrap = DubboBootstrap.getInstance();
bootstrap.application(new ApplicationConfig("demo-provider"))
.registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))
.protocol(new ProtocolConfig(CommonConstants.TRIPLE, 50051))
.registry(new RegistryConfig(TriSampleConstants.ZK_ADDRESS))
.protocol(new ProtocolConfig(CommonConstants.TRIPLE, TriSampleConstants.SERVER_POINT))
.service(pbService)
.service(wrapService)
.start()
@@ -28,7 +28,7 @@ public static void initStub() {

DubboBootstrap bootstrap = DubboBootstrap.getInstance();
bootstrap.application(new ApplicationConfig("demo-consumer"))
.registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))
.registry(new RegistryConfig(TriSampleConstants.ZK_ADDRESS))
.reference(ref)
.start();
delegate = ref.get();

0 comments on commit 6c1932e

Please sign in to comment.