Skip to content

Commit be5d0d2

Browse files
committed
update rpc02 and add spring04
1 parent 4f8c3c6 commit be5d0d2

File tree

17 files changed

+221
-26
lines changed

17 files changed

+221
-26
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.kimmking.spring04;
2+
3+
import org.springframework.core.Ordered;
4+
5+
public interface ABCPlugin extends Ordered {
6+
7+
void startup() throws Exception;
8+
void shutdown() throws Exception;
9+
10+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.kimmking.spring04;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Component;
5+
6+
import javax.annotation.PreDestroy;
7+
import java.util.List;
8+
9+
@Component
10+
public class ABCStartup {
11+
12+
@Autowired
13+
private List<ABCPlugin> services;
14+
15+
public void call () {
16+
services.forEach(System.out::println);
17+
}
18+
19+
@PreDestroy
20+
public void destroy() {
21+
22+
System.out.println(Thread.currentThread().getName()+"-destroy:" + this.getClass().getSimpleName());
23+
24+
}
25+
26+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.kimmking.spring04;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.ApplicationEvent;
5+
import org.springframework.context.ApplicationListener;
6+
import org.springframework.context.event.ContextClosedEvent;
7+
import org.springframework.context.event.ContextRefreshedEvent;
8+
import org.springframework.context.event.ContextStartedEvent;
9+
import org.springframework.context.event.ContextStoppedEvent;
10+
import org.springframework.stereotype.Component;
11+
12+
import java.util.List;
13+
14+
@Component
15+
public class ABCStartupListener implements ApplicationListener<ApplicationEvent> {
16+
17+
@Autowired
18+
private List<ABCPlugin> services;
19+
20+
@Override
21+
public void onApplicationEvent(ApplicationEvent event) {
22+
23+
System.out.println(event);
24+
25+
// 如果用spring boot可以怎么改进
26+
if (event instanceof ContextStartedEvent || event instanceof ContextRefreshedEvent) {
27+
services.forEach(x -> {
28+
try {
29+
x.startup();
30+
} catch (Exception e) {
31+
e.printStackTrace();
32+
}
33+
});
34+
}
35+
36+
if (event instanceof ContextClosedEvent | event instanceof ContextStoppedEvent) {
37+
services.forEach(x -> {
38+
try {
39+
x.shutdown();
40+
} catch (Exception e) {
41+
e.printStackTrace();
42+
}
43+
});
44+
}
45+
}
46+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.kimmking.spring04;
2+
3+
import org.springframework.beans.factory.DisposableBean;
4+
import org.springframework.core.Ordered;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component()
8+
public class MockABCPlugin1 implements ABCPlugin, DisposableBean {
9+
10+
@Override
11+
public String toString() {
12+
System.out.println(Thread.currentThread().getName()+"-"+this.getClass().getSimpleName()+" toString.");
13+
return this.getClass().getSimpleName();
14+
}
15+
16+
@Override
17+
public int getOrder() {
18+
return 1;
19+
}
20+
21+
@Override
22+
public void startup() throws Exception {
23+
// mock for netty server start
24+
System.out.println(Thread.currentThread().getName()+"-"+this.toString()+" started.");
25+
}
26+
27+
@Override
28+
public void shutdown() throws Exception { // 线程池之类可以改进为优雅停机
29+
System.out.println(Thread.currentThread().getName()+"-"+this.toString()+" stopped.");
30+
}
31+
32+
@Override
33+
public void destroy() throws Exception {
34+
System.out.println(Thread.currentThread().getName()+"-DisposableBean:" + this.toString());
35+
}
36+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.kimmking.spring04;
2+
3+
import org.springframework.core.Ordered;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component()
7+
public class MockABCPlugin2 implements ABCPlugin {
8+
9+
@Override
10+
public String toString() {
11+
return this.getClass().getSimpleName();
12+
}
13+
14+
@Override
15+
public int getOrder() {
16+
return 2;
17+
}
18+
19+
@Override
20+
public void startup() throws Exception {
21+
System.out.println(this.toString()+" started.");
22+
}
23+
24+
@Override
25+
public void shutdown() throws Exception {
26+
System.out.println(this.toString()+" stopped.");
27+
}
28+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.kimmking.spring04;
2+
3+
import org.springframework.context.ApplicationEvent;
4+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5+
6+
public class Spring04Main {
7+
8+
public static void main(String[] args) {
9+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Spring04Main.class.getPackage().getName());
10+
ABCStartup abc = context.getBean(ABCStartup.class);
11+
abc.call();
12+
13+
context.publishEvent(new ApplicationEvent("myEvent") {});
14+
15+
context.close();
16+
// context.destroy();
17+
// context.stop(); // 这3个有什么区别?
18+
19+
}
20+
21+
}

07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/UserService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ public interface UserService {
44

55
User findById(int id);
66

7+
//User findById(int id, String name);
8+
79
}

07rpc/rpc01/rpcfx-demo-consumer/src/main/java/io/kimmking/rpcfx/demo/consumer/RpcfxClientApplication.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,18 @@ public static void main(String[] args) {
2828
// UserService service = new xxx();
2929
// service.findById
3030

31-
UserService userService = Rpcfx.create(UserService.class, "http://localhost:8080/");
32-
User user = userService.findById(1);
33-
System.out.println("find user id=1 from server: " + user.getName());
34-
31+
// UserService userService = Rpcfx.create(UserService.class, "http://localhost:8080/");
32+
// User user = userService.findById(1);
33+
// System.out.println("find user id=1 from server: " + user.getName());
34+
//
3535
// OrderService orderService = Rpcfx.create(OrderService.class, "http://localhost:8080/");
3636
// Order order = orderService.findOrderById(1992129);
3737
// System.out.println(String.format("find order name=%s, amount=%f",order.getName(),order.getAmount()));
38-
//
39-
// //
40-
// UserService userService2 = Rpcfx.createFromRegistry(UserService.class, "localhost:2181", new TagRouter(), new RandomLoadBalancer(), new CuicuiFilter());
38+
39+
40+
UserService userService2 = Rpcfx.createFromRegistry(UserService.class, "localhost:2181", new TagRouter(), new RandomLoadBalancer(), new CuicuiFilter());
41+
User user = userService2.findById(1);
42+
System.out.println(user);
4143

4244
// SpringApplication.run(RpcfxClientApplication.class, args);
4345
}

07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/OrderServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ public class OrderServiceImpl implements OrderService {
77

88
@Override
99
public Order findOrderById(int id) {
10-
return new Order(id, "Cuijing" + System.currentTimeMillis(), 9.9f);
10+
return new Order(id, "Order" + System.currentTimeMillis(), 9.9f);
1111
}
1212
}

07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/RpcfxServerApplication.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ public class RpcfxServerApplication {
3232
public static void main(String[] args) throws Exception {
3333

3434
// // start zk client
35-
// RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
36-
// CuratorFramework client = CuratorFrameworkFactory.builder().connectString("localhost:2181").namespace("rpcfx").retryPolicy(retryPolicy).build();
37-
// client.start();
35+
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
36+
CuratorFramework client = CuratorFrameworkFactory.builder().connectString("localhost:2181").namespace("rpcfx").retryPolicy(retryPolicy).build();
37+
client.start();
3838
//
3939
//
4040
// // register service
4141
// // xxx "io.kimmking.rpcfx.demo.api.UserService"
4242
//
43-
// String userService = "io.kimking.rpcfx.demo.api.UserService";
44-
// registerService(client, userService);
43+
String userService = "io.kimking.rpcfx.demo.api.UserService";
44+
registerService(client, userService);
4545
// String orderService = "io.kimking.rpcfx.demo.api.OrderService";
4646
// registerService(client, orderService);
4747

@@ -54,7 +54,7 @@ public static void main(String[] args) throws Exception {
5454
private static void registerService(CuratorFramework client, String service) throws Exception {
5555
ServiceProviderDesc userServiceSesc = ServiceProviderDesc.builder()
5656
.host(InetAddress.getLocalHost().getHostAddress())
57-
.port(8080).serviceClass(service).build();
57+
.port(8081).serviceClass(service).build();
5858
// String userServiceSescJson = JSON.toJSONString(userServiceSesc);
5959

6060
try {

0 commit comments

Comments
 (0)