发布时间:2024-01-21 12:30
com
dubbo-common
0.0.1-SNAPSHOT
org.apache.dubbo
dubbo-spring-boot-starter
2.7.8
org.slf4j
slf4j-log4j12
org.apache.zookeeper
zookeeper
3.4.13
org.slf4j
slf4j-log4j12
org.apache.curator
curator-recipes
4.2.0
1.配置项目端口
2.配置dubbo应用程序名称
3.配置注册中心
4.配置dubbo协议和端口(通过端口提供服务)
server:
port: 8881
dubbo:
application:
name: boot-provider
registry:
address: zookeeper://127.0.0.1:2181
protocol:
name: dubbo
port: 9991
@Component
@DubboService
public class UserServiceImpl implements UserService {
@Override
public User findById(int id) {
User user = new User();
user.setId(id);
user.setUsername("alice");
user.setPassword("123");
return user;
}
}
@SpringBootApplication
@EnableDubbo //启用dubbo
public class BootProviderApplication {
public static void main(String[] args) {
SpringApplication.run(BootProviderApplication.class, args);
}
}
@DubboReference
private UserService userService;
@RequestMapping("/getUserById")
public String getUserById(String id, Model model) {
UserBean userBean = userService.getUserById(id);
Object json = JSONObject.toJSON(userBean);
model.addAttribute("user", json);
return "success";
}
server:
port: 8882
dubbo:
application:
name: boot-consumer
registry:
address: zookeeper://127.0.0.1:2181
spring:
thymeleaf:
cache: false
mode: HTML
prefix: classpath:/templates/
#prefix: /WEB-INF/
suffix: .html
@SpringBootApplication
@ComponentScan(basePackages = { "com.dubbo" })
@EnableDubbo
public class Application {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(Application.class, args);
}
}