完善此前例子的配置
PPG007 ... 2021-12-27 Less than 1 minute
# 完善此前例子的配置
这里都使用 Apache Dubbo。
# 服务提供者
@Configuration
@EnableDubbo(scanBasePackages = "service.impl")
public class ProviderConfiguration {
@Bean
public ApplicationConfig applicationConfig(){
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("user-service-provider");
applicationConfig.setVersion("1.0");
applicationConfig.setOwner("PPG");
applicationConfig.setOrganization("BBZL");
applicationConfig.setEnvironment("test");
return applicationConfig;
}
@Bean
public RegistryConfig registryConfig(){
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setId("ppg");
registryConfig.setAddress("localhost");
registryConfig.setPort(2181);
registryConfig.setProtocol("zookeeper");
registryConfig.setTimeout(10000);
registryConfig.setCheck(false);
return registryConfig;
}
@Bean
public ProtocolConfig protocolConfig(){
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setId("PPG");
protocolConfig.setName("dubbo");
protocolConfig.setPort(20880);
protocolConfig.setHost("localhost");
return protocolConfig;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@DubboService(delay = -1,loadbalance = "roundrobin",registry = "ppg",protocol = "PPG",version = "1.0",weight = 2)
public class UserServiceImpl implements IUserService {
@Override
public Integer getAge(User user) {
Calendar birthday = Calendar.getInstance();
birthday.setTime(user.getBirthday());
Calendar now= Calendar.getInstance();
return now.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 服务消费者
@Configuration
@EnableDubbo(scanBasePackages = "service")
@ComponentScan(basePackages = "service")
public class ConsumerConfiguration {
@Bean
public ApplicationConfig applicationConfig(){
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("user-service-consumer");
applicationConfig.setVersion("1.0");
applicationConfig.setOwner("PPG");
applicationConfig.setOrganization("BBZL");
applicationConfig.setEnvironment("test");
return applicationConfig;
}
@Bean
public RegistryConfig registryConfig(){
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setId("ppg007");
registryConfig.setAddress("localhost");
registryConfig.setPort(2181);
registryConfig.setProtocol("zookeeper");
registryConfig.setTimeout(10000);
registryConfig.setCheck(false);
return registryConfig;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@Service
public class UserServiceConsumerImpl implements IUserServiceConsumer{
private IUserService userService;
@DubboReference(version = "1.0",timeout = 1000,retries = 5,loadbalance = "roundrobin",check = false)
public void setUserService(IUserService userService) {
this.userService = userService;
}
@Override
public void demo() {
User user = new User();
Calendar instance = Calendar.getInstance();
instance.set(Calendar.YEAR,2000);
Date bir = instance.getTime();
user.setBirthday(bir);
System.out.println(userService.getAge(user));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21