在我之前的文章提到过,spring-cloud-alibaba无缝的集成类feign组件,底层属于http调用,所以使用起来会非常灵活,
但是在性能上不及rpc,Dubbo正是一款高性能的rpc框架,有着良好的负载均衡与熔断降级特性,在微服务中也是一个非常流行的框架
集成方案有多种,可以让zookeeper作为注册中心,然后把zookeeper集群集成到nacos中,或者直接让nacos作为注册中心,推荐后一种方式,因为阿里巴巴推出nacos的目的就是想代替zookeeper的方案
version: '3.1'
services:
zoo1:
image: zookeeper
restart: always
hostname: zoo1
networks:
- zoo-net
ports:
- 2181:2181
volumes:
- zoo1-data:/data
- zoo1-log:/datalog
environment:
ZOO_MY_ID: 1
ZOO_SERVERS: server.1=0.0.0.0:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181
ZOO_4LW_COMMANDS_WHITELIST: "*"
zoo2:
image: zookeeper
restart: always
hostname: zoo2
networks:
- zoo-net
ports:
- 2182:2181
environment:
ZOO_MY_ID: 2
ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=0.0.0.0:2888:3888;2181 server.3=zoo3:2888:3888;2181
ZOO_4LW_COMMANDS_WHITELIST: "*"
zoo3:
image: zookeeper
restart: always
hostname: zoo3
networks:
- zoo-net
ports:
- 2183:2181
environment:
ZOO_MY_ID: 3
ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=0.0.0.0:2888:3888;2181
ZOO_4LW_COMMANDS_WHITELIST: "*"
volumes:
zoo1-data:
external: false
zoo1-log:
external: false
networks:
zoo-net:
public interface HelloService {
public String sayHello(String name);
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--接口项目-->
<dependency>
...
</dependency>
dubbo:
protocol:
name: dubbo
port: -1
registry:
address: spring-cloud://jd
scan:
base-packages: com.zhubome.democommonservice4.service
cloud:
subscribed-services:
spring:
application:
name: alibaba-dubbo-server
cloud:
nacos:
discovery:
server-addr: jd:8848
import com.zhubome.sca.dubbointerface.service.HelloService;
import org.apache.dubbo.config.annotation.Service;
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class DemoCommonService4Application {
public static void main(String[] args) {
SpringApplication.run(DemoCommonService4Application.class, args);
}
}
java -jar -Dspring.cloud.nacos.discovery.ip=`hostname -i` -jar xxx.jar
前提是已做好hostname公网ip映射
必要依赖
与提供者同
application.yaml
spring:
application:
name: sca-consumer-service
cloud:
nacos:
discovery:
server-addr: jd:8848
server:
port: 10107
dubbo:
protocol:
name: dubbo
port: -1
registry:
address: spring-cloud://jd
cloud:
subscribed-services: alibaba-dubbo-server
import com.zhubome.sca.dubbointerface.service.HelloService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Reference
private HelloService helloService;
@GetMapping("/dubbo")
public String sayHello() {
System.out.println(helloService.sayHello("SnailClimb"));
return helloService.sayHello("world");
}
}