Skip to content

Commit 715cfc6

Browse files
committed
add readme by dubbo annotation
1 parent 1f063cd commit 715cfc6

File tree

3 files changed

+110
-3
lines changed

3 files changed

+110
-3
lines changed

dubbo-annotation-sample/README.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,110 @@
6767
3. 注册中心全部宕掉后,服务提供者和服务消费者仍然可以通过本地缓存列表进行通讯;
6868
4. 服务提供者无状态,任意一台宕掉后,不影响使用;
6969
5. 服务提供者全部宕机后,服务消费者应用将无法使用,并无限次数的重试等待服务提供者恢复;
70+
71+
### Dubbo注解方式实战
72+
##### 1. 编写Provider端Hello服务
73+
```java
74+
@Service(version = AnnotationConstants.VERSION)
75+
public class HelloServiceImpl implements IHelloService {
76+
77+
/**
78+
* Hello Service
79+
*
80+
* @param name
81+
* @return
82+
*/
83+
@Override
84+
@SneakyThrows
85+
public String sayHello(String name) {
86+
System.out.println("provider received invoke of sayHello: " + name);
87+
Thread.sleep(300);
88+
return "Annotation, hello " + name;
89+
}
90+
}
91+
```
92+
93+
##### 2. 配置Dubbo的Provider端
94+
```java
95+
@Configuration
96+
@EnableDubbo(scanBasePackages = "com.ipman.sample.dubbo.annotation.service.impl")
97+
@PropertySource("classpath:/spring/dubbo-provider.properties")
98+
public class ProviderConfiguration {
99+
100+
@Bean
101+
public ProviderConfig providerConfig() {
102+
ProviderConfig providerConfig = new ProviderConfig();
103+
providerConfig.setTimeout(1000);
104+
return providerConfig;
105+
}
106+
}
107+
```
108+
109+
##### 3. 启动zookeeper,启动Dubbo的Provider端
110+
```java
111+
@SpringBootApplication
112+
public class DubboAnnotationProviderApplication {
113+
114+
private static TestingServer server;
115+
116+
@SneakyThrows
117+
public static void main(String[] args) {
118+
//开启ZK
119+
DubboAnnotationProviderApplication.mockZKServer();
120+
121+
Thread.sleep(3000);
122+
123+
//开启Dubbo
124+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class);
125+
context.start();
126+
}
127+
128+
/**
129+
* 启动本地ZK
130+
*
131+
* @throws Exception
132+
*/
133+
private static void mockZKServer() throws Exception {
134+
//Mock zk server,作为 配置中心
135+
server = new TestingServer(2181, true);
136+
server.start();
137+
}
138+
}
139+
```
140+
141+
##### 4.配置Dubbo的Consumer端
142+
```java
143+
@Configuration
144+
@EnableDubbo(scanBasePackages = "com.ipman.sample.dubbo.annotation.action")
145+
@PropertySource("classpath:/spring/dubbo-consumer.properties")
146+
@ComponentScan(value = "com.ipman.sample.dubbo.annotation.action")
147+
public class ConsumerConfiguration {
148+
149+
}
150+
```
151+
152+
##### 5.配置Consumer要消费的Provider服务
153+
```java
154+
@Component("annotationAction")
155+
public class AnnotationAction {
156+
157+
//消费者指定引用的提供者接口服务
158+
@Reference(interfaceClass = IHelloService.class,
159+
version = AnnotationConstants.VERSION,
160+
timeout = 1000,
161+
methods = {@Method(name = "sayHello", timeout = 3000, retries = 1)})
162+
public IHelloService helloService;
163+
164+
165+
//测试Provider
166+
public String doSayHello() {
167+
try {
168+
return helloService.sayHello("ipman");
169+
} catch (Exception e) {
170+
e.printStackTrace();
171+
return "Throw Exception";
172+
}
173+
}
174+
}
175+
```
176+

dubbo-annotation-sample/dubbo-annotation-consumer/src/main/java/com/ipman/sample/dubbo/annotation/action/AnnotationAction.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.ipman.sample.dubbo.annotation.service.AnnotationConstants;
44
import com.ipman.sample.dubbo.annotation.service.IHelloService;
5+
import org.apache.dubbo.config.annotation.Method;
56
import org.apache.dubbo.config.annotation.Reference;
67
import org.springframework.stereotype.Component;
78

@@ -19,8 +20,8 @@ public class AnnotationAction {
1920
//消费者指定引用的提供者接口服务
2021
@Reference(interfaceClass = IHelloService.class,
2122
version = AnnotationConstants.VERSION,
22-
timeout = 1000)
23-
// methods = {@Method(name = "sayHello", timeout = 3000, retries = 1)})
23+
timeout = 1000,
24+
methods = {@Method(name = "sayHello", timeout = 3000, retries = 1)})
2425
public IHelloService helloService;
2526

2627

dubbo-annotation-sample/dubbo-annotation-provider/src/main/java/com/ipman/sample/dubbo/annotation/DubboAnnotationProviderApplication.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,4 @@ private static void mockZKServer() throws Exception {
3333
server = new TestingServer(2181, true);
3434
server.start();
3535
}
36-
3736
}

0 commit comments

Comments
 (0)