配置dubbo2.7 SpringBoot版

发布时间:2024-01-21 12:30

一、创建provder工程

1.引入依赖

      
		
			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
		

2.配置yml

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

3.编写实现类

@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;
}
}

4.启用dubbo

@SpringBootApplication
@EnableDubbo //启用dubbo
public class BootProviderApplication {
public static void main(String[] args) {
SpringApplication.run(BootProviderApplication.class, args);
}
}

二.创建consumer工程

1.引入依赖,同1.1

2.编写使用类


    @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";
	}

3.配置dubbo

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

4.启用dubbo

@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);
	}

}

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号