整合 SpringBoot
PPG007 ... 2021-12-27 About 1 min
# 整合 SpringBoot
# 导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<artifactId>UserService-API</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.8</version>
<type>pom</type>
</dependency>
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
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
# 服务提供者
编写配置文件:
spring:
application:
name: UserService-Provider-SpringBoot
dubbo:
application:
name: user-service-provider-spring-boot
version: 1.0
owner: PPG
organization: BBZL
environment: test
registry:
address: 127.0.0.1
protocol: zookeeper
id: ppg
port: 2181
check: false
timeout: 10000
protocol:
name: dubbo
port: 8848
id: PPG
host: localhost
monitor:
protocol: registry
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
编写接口实现类:
@Service
@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
2
3
4
5
6
7
8
9
10
11
主启动类上添加 @EnableDubbo
。
# 服务消费者
编写配置文件:
spring:
application:
name: UserService-Consumer-SpringBoot
server:
port: 9999
dubbo:
application:
name: user-service-consumer-spring-boot
version: 1.0
owner: PPG
organization: BBZL
environment: test
registry:
address: 127.0.0.1
port: 2181
protocol: zookeeper
id: ppg007
timeout: 10000
check: false
monitor:
protocol: registry
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
编写消费者接口实现类:
@Service
public class UserServiceConsumer 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() {
}
public String getUserDetail() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
Calendar instance = Calendar.getInstance();
instance.set(Calendar.YEAR,2000);
User user = new User();
user.setId(1)
.setUsername("ppg")
.setPassword("123")
.setBirthday(instance.getTime());
Integer age = userService.getAge(user);
HashMap<String, Object> hashMap = new HashMap<>(2);
hashMap.put("user",user);
hashMap.put("age",age);
return objectMapper.writeValueAsString(hashMap);
}
}
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
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
编写controller:
@RestController
public class UserController {
private UserServiceConsumer userServiceConsumer;
@Autowired
public void setUserServiceConsumer(UserServiceConsumer userServiceConsumer) {
this.userServiceConsumer = userServiceConsumer;
}
@RequestMapping("/userDetail")
public String userDetail() throws JsonProcessingException {
return userServiceConsumer.getUserDetail();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
主启动类上添加 @EnableDubbo
。