Dubbo3的新特性和修改这里就不再赘述了,个人认为主要是注册的颗粒度的改变,把之前的接口级改成了应用级,减少了注册列表的数量更易于维护。
服务端:
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>3.0.7</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.0.4</version>
</dependency>
</dependencies>
启动服务类:
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableDubbo(scanBasePackages = "cn.com.auth.service")
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
配置文件application.properties
spring.application.name=user
server.port=9989
dubbo.application.name=auth
dubbo.protocol.id=dubbo
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
dubbo.registry.address=nacos://localhost:8848
dubbo.registry.register-mode=instance
编写接口作为服务service
package cn.com.auth.api;
public interface AuthService {
public String call(String str);
}
实现类
package cn.com.auth.service;
import cn.com.auth.api.AuthService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@DubboService
public class AuthServiceImpl implements AuthService {
private Logger logger = LoggerFactory.getLogger(AuthServiceImpl.class);
@Override
public String call(String str) {
return "hello"+str;
}
}
把接口类放在一个module下,打包上传私服,以供客户端引用
<dependency>
<groupId>cn.com.auth</groupId>
<artifactId>auth_api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
客户端:
pom.xml 这里需要把服务端的auth_api引入
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>3.0.7</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cn.com.auth</groupId>
<artifactId>auth_api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
服务启动类:
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableDubbo
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
application.properties
spring.application.name=client
server.port=8080
dubbo.application.name=client
dubbo.protocol.id=dubbo
dubbo.protocol.name=dubbo
dubbo.cloud.subscribed-services=auth
dubbo.registry.address=nacos://localhost:8848?register-consumer-url=true
对外http服务Controller类,调用dubbo服务auth_api的接口
import cn.com.auth.api.AuthService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CustomerController {
@DubboReference
AuthService service;
@PostMapping("/call")
public String call(){
return service.call("call");
}
}
启动服务端和客户端,查看nacos注册情况
已经注册好了,用postman调用http服务看结果
结果返回正常,一切OK!