相关的资料
项目结构
项目说明
- [lesson1-config-api] 是一个接口工程,编译后是jar包,被其他工程依赖
- [lesson1-config-2-properties-provider] 是服务提供者
代码示例
- [IEchoService]
public interface IEchoService {
void echo();
}
- [EchoServiceImpl]
@Service
public class EchoServiceImpl implements IEchoService {
public void echo() {
System.out.printf("hello");
}
}
- [ProviderApp]
@RestController
@SpringBootApplication
@EnableDubbo(scanBasePackages = "provider.service.impl")
@PropertySource("classpath:/spring/dubbo-provider.properties")
public class ProviderApp {
@GetMapping("/hello")
public String hello() {
return "hello";
}
public static void main(String[] args) throws IOException {
/**
* 这种方式启动不能注册服务到zookeeper
*/
//SpringApplication.run(ProviderApp.class, args);
/**
* 官方的demon采用这种方式
*/
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderApp.class);
context.start();
System.in.read();
}
}
或者
@RestController
@SpringBootApplication
public class ProviderApp {
@GetMapping("/hello")
public String hello() {
return "hello";
}
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class);
context.start();
System.in.read();
}
@Configuration
@EnableDubbo(scanBasePackages = "provider.service.impl")
@PropertySource("classpath:/spring/dubbo-provider.properties")
static class ProviderConfiguration {
}
}
- [dubbo-provider.properties]
# dubbo-provider.properties
dubbo.application.name=annotation-provider
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.registry.address=zookeeper://127.0.0.1:2181
如果配置文件不指定注册中心的话,可以在代码里指定
@Configuration
@EnableDubbo(scanBasePackages = "provider.service.impl")
@PropertySource("classpath:/spring/dubbo-provider.properties")
static class ProviderConfiguration {
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
//registryConfig.setAddress("multicast://224.5.6.7:1234");
registryConfig.setAddress("zookeeper://127.0.0.1:2181");
return registryConfig;
}
}
- [pom.xml]
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.1</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-config-spring</artifactId>
<version>2.7.1</version>
</dependency>
dubbo-config-spring是必须的,如果没有是不会报错,但是服务注册不上
zkCli 查看
[zk: localhost:2181(CONNECTED) 259] ls /dubbo/com.xh.dubbo.learn.lesson1.api.IEchoService/providers
Node does not exist: /dubbo/com.xh.dubbo.learn.lesson1.api.IEchoService/providers
[zk: localhost:2181(CONNECTED) 260] ls /dubbo/com.xh.dubbo.learn.lesson1.api.IEchoService/providers
[dubbo%3A%2F%2F127.0.1.1%3A20880%2Fcom.xh.dubbo.learn.lesson1.api.IEchoService%3Fanyhost%3Dtrue%26application%3Dannotation-provider%26bean.name%3Dproviders%3Adubbo%3Acom.xh.dubbo.learn.lesson1.api.IEchoService%26default.deprecated%3Dfalse%26default.dynamic%3Dfalse%26default.register%3Dtrue%26deprecated%3Dfalse%26dubbo%3D2.0.2%26dynamic%3Dfalse%26generic%3Dfalse%26interface%3Dcom.xh.dubbo.learn.lesson1.api.IEchoService%26methods%3Decho%26pid%3D3935%26register%3Dtrue%26release%3D2.7.1%26side%3Dprovider%26timestamp%3D1557307484341]
在控制台关闭服务后发现zk中的节点没有删除
delete path # 删除叶子节点
rmr path # 递归删除节点和他的子节点