Skip to content

Commit

Permalink
Add use sofa-registry demo. (sofastack#644)
Browse files Browse the repository at this point in the history
* extract method 'selectActualFilters' to avoid duplicate code and fix some typo

* add method notes and fix some typo

* change ProviderBootstrap#unExport notes

* add sofaregsitry example

* add sofaregistry example

* add use sofa-registry demo

* reformat code
  • Loading branch information
zhaojigang authored and leizhiyuan committed Jun 3, 2019
1 parent e0369dd commit d5172c5
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public class RpcConstants {
*/
public static final String REGISTRY_PROTOCOL_MESH = "mesh";

/**
* sofa 注册中心
*/
public static final String REGISTRY_PROTOCOL_SOFA = "sofa";

/**
* xml文件注册中心
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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 com.alipay.sofa.rpc.sofaregistry;

import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.config.ConsumerConfig;
import com.alipay.sofa.rpc.config.RegistryConfig;
import com.alipay.sofa.rpc.context.RpcRuntimeContext;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import com.alipay.sofa.rpc.quickstart.HelloService;

/**
* use sofa-registry client demo
*/
public class SofaRegistryClient {
private final static Logger LOGGER = LoggerFactory.getLogger(SofaRegistryClient.class);

public static void main(String[] args) {
/**
* 运行时项目引入依赖
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>registry-client-all</artifactId>
<version>5.2.0</version>
</dependency>
*/
RegistryConfig registryConfig = new RegistryConfig()
.setProtocol(RpcConstants.REGISTRY_PROTOCOL_SOFA)
.setAddress("127.0.0.1:9603");

ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>()
.setInterfaceId(HelloService.class.getName())
.setRegistry(registryConfig)
.setProtocol("bolt")
.setConnectTimeout(10 * 1000);

HelloService helloService = consumerConfig.refer();
LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);

try {
while (true) {
try {
System.out.println(helloService.sayHello("world"));
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
try {
Thread.sleep(2000);
} catch (Exception e) {
}
}
} catch (Exception e) {
LOGGER.error("", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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 com.alipay.sofa.rpc.sofaregistry;

import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.config.ProviderConfig;
import com.alipay.sofa.rpc.config.RegistryConfig;
import com.alipay.sofa.rpc.config.ServerConfig;
import com.alipay.sofa.rpc.context.RpcRuntimeContext;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import com.alipay.sofa.rpc.quickstart.HelloService;
import com.alipay.sofa.rpc.quickstart.HelloServiceImpl;

/**
* use sofa-registry server demo
*/
public class SofaRegistryServer {
private static final Logger LOGGER = LoggerFactory.getLogger(SofaRegistryServer.class);

public static void main(String[] args) {
/**
* 运行时项目引入依赖
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>registry-client-all</artifactId>
<version>5.2.0</version>
</dependency>
*/
RegistryConfig registryConfig = new RegistryConfig()
.setProtocol(RpcConstants.REGISTRY_PROTOCOL_SOFA)
.setAddress("127.0.0.1:9603");

ServerConfig serverConfig = new ServerConfig()
.setProtocol("bolt")
.setPort(12200)
.setDaemon(false);

ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>()
.setRegistry(registryConfig)
.setInterfaceId(HelloService.class.getName())
.setRef(new HelloServiceImpl())
.setServer(serverConfig);

providerConfig.export();
LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
}
}

0 comments on commit d5172c5

Please sign in to comment.