提供者
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.2</version>
</dependency>
<!--dubbo-->
<!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<!--zookeeper-->
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- 引入zookeeper -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<!--排除这个slf4j-log4j12-->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
配置
server:
port: 8002
dubbo:
application:
name: produce #应用名
registry:
address: zookeeper://192.168.31.101:2181 #zookeeper地址
scan:
base-packages: com.cyz.service.impl #扫描的包名
项目结构
启动类
package com.cyz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author cyz
* @since 2024/3/26 20:21
*/
@SpringBootApplication
public class ProduceMain {
public static void main(String[] args) {
SpringApplication.run(ProduceMain.class, args);
}
}
服务类
接口
package com.cyz.service;
/**
* @author cyz
* @since 2024/3/26 20:29
*/
public interface TestService {
String sayHello(String name);
}
实现类
package com.cyz.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.cyz.service.TestService;
import org.springframework.stereotype.Component;
/**
* @author cyz
* @since 2024/3/26 20:30
*/
@Component
@Service
public class TestServiceImpl implements TestService {
@Override
public String sayHello(String name) {
return name + " say hello";
}
}
注意这边的service注解是com.alibaba.dubbo.config.annotation.Service;是dubbo的注解
启动服务,去zookeeper中查看
证明成功了
消费者
依赖
和提供者一样
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.2</version>
</dependency>
<!--dubbo-->
<!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<!--zookeeper-->
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- 引入zookeeper -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<!--排除这个slf4j-log4j12-->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
配置
server:
port: 8001
dubbo:
application:
name: consumer #应用名
registry:
address: zookeeper://192.168.31.101:2181 #zookeeper地址
项目结构
启动类
package com.cyz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author cyz
* @since 2024/3/26 20:22
*/
@SpringBootApplication
public class ConsumerMain {
public static void main(String[] args) {
SpringApplication.run(ConsumerMain.class, args);
}
}
服务类
必须和提供者的服务类一模一样,包括全限定类名
package com.cyz.service;
/**
* @author cyz
* @since 2024/3/26 20:29
*/
public interface TestService {
String sayHello(String name);
}
控制类
package com.cyz.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.cyz.service.TestService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author cyz
* @since 2024/3/26 20:46
*/
@RestController
public class TestController {
@Reference
TestService testService;
@GetMapping("/sayHello")
public String sayHello(String name) {
return testService.sayHello(name);
}
}
启动服务,查看zookeeper
调用接口访问
至此整合成功