- 创建springboot项目
- 启动zookeeper
提供接口和实体类,可以制作为二方包
依赖:
org.projectlombok lombok 1.18.22
接口:
public interface UserService {
public User getUser(String uid);
}
实体类:
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {
private String uid;
private String username;
}
provider模块
依赖生产者模块,提供接口实现
配置com.fasterxml.jackson.core jackson-core 2.10.1 com.fasterxml.jackson.core jackson-databind 2.10.1 com.fasterxml.jackson.core jackson-annotations 2.10.1 org.apache.dubbo dubbo-spring-boot-starter 3.0.7 org.apache.dubbo dubbo-rpc-dubbo 3.0.7 org.apache.dubbo dubbo-registry-zookeeper 3.0.7 org.springframework.boot spring-boot-starter-web 2.6.6 org.example common 1.0-SNAPSHOT
server.port=8081 dubbo.application.name=provider-application dubbo.protocol.name=dubbo dubbo.protocol.port=20880 dubbo.registry.address=zookeeper://127.0.0.1:2181实现类
import bcy.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService
public class UserServiceImpl implements UserService {
public User getUser(String uid) {
User user = new User(uid, "bcy");
return user;
}
}
启动类
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo(scanBasePackages = "bcy.service.impl")
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class);
}
}
消费者模块
依赖
配置com.fasterxml.jackson.core jackson-core 2.10.1 com.fasterxml.jackson.core jackson-databind 2.10.1 com.fasterxml.jackson.core jackson-annotations 2.10.1 org.apache.dubbo dubbo-spring-boot-starter 3.0.7 org.apache.dubbo dubbo-rpc-dubbo 3.0.7 org.apache.dubbo dubbo-registry-zookeeper 3.0.7 org.springframework.boot spring-boot-starter-web 2.6.6 org.example common 1.0-SNAPSHOT
server.port=8082 dubbo.application.name=consumer-application dubbo.registry.address=zookeeper://127.0.0.1:2181消费者接口
import bcy.entity.User;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
@Service
public class OrderService {
@DubboReference
private UserService userService;
public String createOrder(){
User user = userService.getUser("1");
System.out.println("创建订单");
return user.toString()+" succeeded in creating the order";
}
}
对外开放接口
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import bcy.service.OrderService;
import javax.annotation.Resource;
@RestController
public class OrderController {
@Resource
private OrderService orderService;
@GetMapping("/createOrder")
public String createOrder() {
return orderService.createOrder();
}
}
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class ConsumerApplication {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class);
}
}



