基于Dubbo和Nacos搭建微服务的配置中心和服务注册中心
2023-03-01 本文已影响0人
逆熵架构
1.SpringCloud Alibaba接入Nacos&restTemplate
1.添加nacos依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${latest.version}</version>
</dependency>
2.配置application.properties
spring:
application:
name: {应用名称}
cloud:
nacos:
discovery:
server-addr: 111.52.99.114:18848
username: nacos
password: nacos
PS:nacos2.0除了需要8848的端口之外,还新增了端口+1000的新端口用于客户端和服务端GRPC通信的端口,所以如果是自己部署的nacos需要放开这俩个端口,服务才可以启动成功。
3.配置类
3.1 添加Spring Cloud原生注解 @EnableDiscoveryClient
3.2 客户端服务使用@LoadBalanced
做负载均衡调用
@LoadBalanced
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
PS:使用
@LoadBalanced
需要引入负载均衡器的实现才能生效。<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency>
3.3 通过restTemplate
调用
@RequestMapping(value = "mappingURL", method = RequestMethod.GET)
public String echo(@PathVariable String str) {
return restTemplate.getForObject("http://{服务提供者-服务名}/echo/" + str, String.class);
}
4.nacos控制台
服务注册成功,nacos控制台可以看到服务列表,服务调用正常。如图:
[图片上传失败...(image-e90e6-1677721796202)]
2.Dubbo2.7集成Nacos2.0
1.添加依赖
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.15</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.15</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.0.2</version>
</dependency>
2.Dubbo&Nacos配置
dubbo:
application:
version: 1.0.0
name: service-provider
registry:
address: nacos://111.52.99.114:18848?username=nacos&password=nacos
protocol:
name: dubbo
port: -1
provider:
timeout: 30000
💖:对于需要认证的nacos,使用url拼接用户名密码。
3.启动类注解
@EnableDubbo(scanBasePackages = "cn.bingo.lottery")
@SpringBootApplication
public class DempApplication {
public static void main(String[] args) {
SpringApplication.run(DempApplication.class,args);
}
}
4.服务导出和服务引用
//服务导出
@DubboService(version = "1.0.0",interfaceClass = IActivityBooth.class,retries = 0)
public class ActivityBooth implements IActivityBooth {
}
//服务引用
@DubboReference(version = "1.0.0",timeout =3000 )
private IActivityBooth activityBooth;
5.控制台服务列表
服务注册成功,nacos控制台可以看到服务列表,服务调用正常。如图:
[图片上传失败...(image-2ba3f-1677721796202)]
3.SpringCloud Alibaba接入Nacos&Dubbo
1.添加nacos依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-context</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
2.配置application.properties
dubbo.application.name=service-provider
dubbo.scan.base-packages=cn.service.test.api
spring:
application:
name: {应用名称}
cloud:
nacos:
discovery:
server-addr: 111.52.99.114:18848
username: nacos
password: nacos
3.服务导出和服务引用
//服务引用
@DubboReference(version = "1.0.0",retries = 0)
private ProductApi productApi;
//服务导出
@DubboService(version = "1.0.0",interfaceClass = ProductApi.class,retries = 0)
@Slf4j
public class ProductApiImpl implements ProductApi {}
4.nacos控制台
可以看到这种接入,dubbo2.0的接口级对齐了springcloud的服务级。
[图片上传失败...(image-a671d7-1677721796202)]