1、创建mave项目:
2、修改pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ym</groupId> <artifactId>springbootdubbo</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>service</module> <module>serviceImpl</module> <module>testweb</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.1.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.4.6.v20170531</version> </dependency> </dependencies> <build> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.0.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
3、创建远程接口定义模块:
4、创建接口:
5、创建接口实现模块:
6、springboot整合application.yml:
spring: application: name: dubbo-provider-app server: port: 9090 dubbo: scan: base-packages: com.ym.service application: id: dubbo-provider name: dubbo-provider protocol: id: duboo name: dubbo port: 12345 status: server #标明是一个server registry: id: my-reg address: zookeeper://192.168.1.224:2181 endpoint: dubbo: enabled: true management: port: 9091 health: dubbo: status: extras: load,threadpool defaults: memory
7、实现接口:
package com.ym.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.ym.service.TestService;
/**
* Created with IntelliJ IDEA.
* User: Dony
* Date: 2018/11/17
* Time: 16:04
* Description:
*/
//@Service此时不再使用这个注解
@Service(version = "1.0",application = "${dubbo.application.id}",protocol = "${dubbo.protocol.id}",registry = "${dubbo.registry.id}") //这个注解时dubbo提供的,其作用是创建此类型的对象,然后作为服务提供者发布
public class TestServiceImpl implements TestService {
@Override
public String getData(String name) {
return "result=" + name;
}
}
8、创建启动程序:
package ym;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Created with IntelliJ IDEA.
* User: Dony
* Date: 2018/11/17
* Time: 20:34
* Description:
*/
@SpringBootApplication
public class StartSpringBootMain {
public static void main(String[] args) {
SpringApplication.run(StartSpringBootMain.class);
}
}
观察dubbo控制台:
9、创建消费端:
10、整合springboot和dubbo(application.yml):
spring: application: name: dubbo-consumer-app server: port: 8080 dubbo: application: id: dubbo-consumer name: dubbo-consumer protocol: id: duboo name: dubbo port: 54321 registry: id: my-reg address: zookeeper://192.168.1.224:2181
11、创建controller程序,引用远程接口:
package com.ym.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.ym.service.TestService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created with IntelliJ IDEA.
* User: Dony
* Date: 2018/11/17
* Time: 20:58
* Description:
*/
@RestController
@RequestMapping("/test")
public class TestController {
//@Autowired 不是使用这个注解,使用dubbo注解引用远程服务
@Reference(version = "1.0", application = "${dubbo.application.id}")
private TestService testService;
@RequestMapping("/getdata/{name}")
public String getData(@PathVariable("name") String name) {
return testService.getData(name);
}
}
12、创建启动程序,并启动项目:
package com.ym;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Created with IntelliJ IDEA.
* User: Dony
* Date: 2018/11/17
* Time: 21:14
* Description:
*/
@SpringBootApplication(scanBasePackages = "com.ym")
public class StartSpringBootMain {
public static void main(String[] args) {
SpringApplication.run(StartSpringBootMain.class);
}
}
13、观察dubbo控制台:
转载于:https://blog.51cto.com/3265857/2318354