测试老兵 Dubbo 学习--配置学习

飞狐 · April 24, 2019 · 609 hits

演练的github地址:

https://github.com/apache/incubator-dubbo-samples.git

0x01 dubbo提供服务

public interface CatService {
int getId();

String getName();

String cat();
}
public class CatServiceImpl implements CatService {
@Override
public int getId() {
return 1;
}

@Override
public String getName() {
System.out.println("I am a Cat!");
return "I am a Cat!";
}

@Override
public String cat() {
System.out.println("Meow Meow!");
return "Meow Meow!";
}
}

0x01 dubbo-provider配置

dubbo-demo-provider.xml
总计4个服务

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"
>

<!-- optional provider's application name, used for tracing dependency relationship -->
<dubbo:application name="concurrency-backend">
<dubbo:parameter key="qos.enable" value="true"/>
<dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
<dubbo:parameter key="qos.port" value="33333"/>
</dubbo:application>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- <dubbo:protocol port="-1"/>-->
<dubbo:protocol name="dubbo" port="20891"/>

<dubbo:config-center address="zookeeper://127.0.0.1:2181"/>

<bean id="catService" class="org.apache.dubbo.samples.governance.impl.CatServiceImpl"/>
<dubbo:service interface="
org.apache.dubbo.samples.governance.api.CatService" ref="catService"/>

<bean id="
dogService" class="org.apache.dubbo.samples.governance.impl.DogServiceImpl"/>
<dubbo:service interface="
org.apache.dubbo.samples.governance.api.DogService" ref="dogService"/>

<bean id="
lionService" class="org.apache.dubbo.samples.governance.impl.LionServiceImpl"/>
<dubbo:service interface="
org.apache.dubbo.samples.governance.api.LionService" ref="lionService"/>

<bean id="
tigerService" class="org.apache.dubbo.samples.governance.impl.TigerServiceImpl"/>
<dubbo:service interface="
org.apache.dubbo.samples.governance.api.TigerService" ref="tigerService"/>

</beans>

0x02 Provider服务运行

public class BackendProvider {

public static void main(String[] args) throws Exception {
// new EmbeddedZooKeeper(2181, false).start();
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring/dubbo-demo-provider.xml"});
context.start();

System.in.read(); // press any key to exit
}

}

0x03 测试工程

jar包引用

Provider相关的两个jar包打出来,作为测试工程的libLibrary
dubbo-samples-chain-api-1.0-SNAPSHOT.jar
dubbo-samples-chain-backend-1.0-SNAPSHOT.jar

pom.xml新增:

<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.16.Final</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-rpc-rest</artifactId>
<version>${dubbo.rpc.version}</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>${curator-client.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${curator-client.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>

consumer.xml配置

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"
>

<!-- optional, consumer's application name, used for tracing dependency relationship (not a matching criterion),
don't set it same as provider -->
<dubbo:application name="concurrency-front"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>

<dubbo:config-center address="zookeeper://127.0.0.1:2181"/>

<!-- generate proxy for the remote service, then demoService can be used in the same way as the
local regular interface -->
<dubbo:reference id="catService" check="false" interface="org.apache.dubbo.samples.governance.api.CatService"/>
<dubbo:reference id="
dogService" check="false" interface="org.apache.dubbo.samples.governance.api.DogService"/>
<dubbo:reference id="
lionService" check="false" interface="org.apache.dubbo.samples.governance.api.LionService"/>
<dubbo:reference id="
tigerService" check="false" interface="org.apache.dubbo.samples.governance.api.TigerService"/>

</beans>

spring.xml导入consumer.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
>

<context:property-placeholder location="classpath:properties/hero-${spring.profiles.active}.properties" ignore-unresolvable="true" />

<import resource="classpath:spring/hero-spring-mvc.xml"/>
<import resource="classpath:spring/hero-spring-jdbc.xml"/>
<import resource="classpath:spring/hero-spring-redis.xml"/>
<!-- <import resource="classpath:spring/hero-spring-es.xml" />-->
<import resource="classpath:spring/dubbo-demo-consumer.xml"/>

<aop:aspectj-autoproxy/>

<beans>
<bean id="multiDataSourceAspect" class="com.hero.common.aop.DataSourceAspect" ></bean>
</beans>

</beans>

BaseSuport配置

@ContextConfiguration(locations = {"classpath*:spring/hero-spring.xml"})
@Listeners({MyExtentTestNgFormatter.class})
//@ActiveProfiles(value = "local")
public class BaseSupport extends AbstractTestNGSpringContextTests {

@Autowired
public AppConfig appConfig;


@DataProvider(name = "feeder")
public Iterator<Object[]> feeder(Method testMethod) {
Class<?> testClass = testMethod.getDeclaringClass();
EquivalenceGeneratorFactory generatorFactory = new EquivalenceGeneratorFactory();
AnnotationMapper mapper = new AnnotationMapper(generatorFactory);
BeneratorContext context = new DefaultBeneratorContext();
context.setGeneratorFactory(generatorFactory);
mapper.parseClassAnnotations(testClass.getAnnotations(), context);
Generator<Object[]> generator = mapper.createAndInitMethodParamsGenerator(testMethod, context);
return new FeedIterator(generator);
}
}

测试demo

/**
* @Des:
* @Auther: 飞狐
* @Date: 2019-04-24
*/

public class DubboSampleTest extends BaseSupport {


@Resource
private CatService catService;

@Test
public void test1(){
System.out.println(catService.cat());
}

}
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
No Reply at the moment.
需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up