文章目录
- 0100 系统环境
- 0200 nacos安装
- 0201 下载
- 0202 安装
- 0300 工程说明
- 0301 结构说明
- 0302 运行效果
- 0400 代码说明
- 0401 服务提供者(Provider Service)
- 0402 服务消费者(Consumer Service)
- 服务提供者SDK(Provider Service sdk)
- 0500 负载均衡
- 0600 源码地址
0100 系统环境
JDK: jdk-20.0.2spring cloud alibaba: 2022.0.0.0spring cloud: 2022.0.4spring boot: 3.1.5nacos: 2.3.0-BETA
0200 nacos安装
0201 下载
官网:https://nacos.io/zh-cn/index.html
github:https://github.com/alibaba/nacos
当前最新版本:https://github.com/alibaba/nacos/releases/download/2.3.0-BETA/nacos-server-2.3.0-BETA.zip
0202 安装
- 导入数据库脚本:./conf/mysql-schema.sql,如下:
mysql> show tables;
+----------------------+
| Tables_in_nacos |
+----------------------+
| config_info |
| config_info_aggr |
| config_info_beta |
| config_info_tag |
| config_tags_relation |
| group_capacity |
| his_config_info |
| permissions |
| roles |
| tenant_capacity |
| tenant_info |
| users |
+----------------------+
12 rows in set (0.00 sec)
- 配置数据库
修改./conf/application.properties
#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:
### Deprecated configuration property, it is recommended to use `spring.sql.init.platform` replaced.
spring.datasource.platform=mysql
spring.sql.init.platform=mysql### Count of DB:
db.num=1### Connect URL of DB:
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=root
db.password.0=123456### Connection pool configuration: hikariCP
db.pool.config.connectionTimeout=30000
db.pool.config.validationTimeout=10000
db.pool.config.maximumPoolSize=20
db.pool.config.minimumIdle=2
- 启动服务
# 默认是集群模式
./startup.sh -m standalone
访问地址:http://192.168.1.200:8848/nacos
先做个基础设置
创建namespace:prod
创建个配置文件
0300 工程说明
0301 结构说明
demo-springcloud-alibaba-nacos (父工程)
├── demo-springcloud-alibaba-nacos-consumer (服务消费者,端口:8000,未使用配置中心)
├── demo-springcloud-alibaba-nacos-provider1 (服务提供者,端口:9001,已使用配置中心)
├── demo-springcloud-alibaba-nacos-provider2 (服务提供者,端口:9002,已使用配置中心)
└── demo-springcloud-alibaba-nacos-provider-sdk(服务端,SDK)
注意: 示例中,为了方便看效果,服务提供者分别提供两个工程,内部代码都一样,只是端口不同。
0302 运行效果
访问这个地址,将随机返回结果:http://localhost:8000/echo2/a
我的端口是:9001, 配置中心的值是:abc, 传过来的值是:a
我的端口是:9002, 配置中心的值是:abc, 传过来的值是:a
修改配置中心配置项,可立即得到最新结果;
0400 代码说明
0401 服务提供者(Provider Service)
demo-springcloud-alibaba-nacos-provider1,端口:9001
关键依赖(pom.xml)
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
关键配置(application.properties)
spring.config.import=optional:nacos:demo-springcloud-alibaba-nacos-configspring.application.name=demo-springcloud-alibaba-nacos-provider
spring.profiles.active=testserver.port=9001spring.cloud.nacos.server-addr=192.168.1.200:8848
spring.cloud.nacos.discovery.server-addr=192.168.1.200:8848
spring.cloud.nacos.discovery.namespace=prod
spring.cloud.nacos.discovery.group=demo
spring.cloud.nacos.discovery.weight=5
spring.cloud.nacos.config.server-addr=192.168.1.200:8848
spring.cloud.nacos.config.file-extension=properties
spring.cloud.nacos.config.namespace=prod
spring.cloud.nacos.config.group=demomanagement.endpoints.web.exposure.include=*
启动类:
@SpringBootApplication
@EnableDiscoveryClient
public class SpringcloudAlibabaNacosProvider1 {public static void main(String[] args) {SpringApplication.run(SpringcloudAlibabaNacosProvider1.class, args);}
}
demo-springcloud-alibaba-nacos-provider1,端口:9002
与demo-springcloud-alibaba-nacos-provider1代码完全一样,只是application.properties中的server.port=9002
spring.application.name=demo-springcloud-alibaba-nacos-provider
spring.profiles.active=testserver.port=9002
....
0402 服务消费者(Consumer Service)
demo-springcloud-alibaba-nacos-consumer,端口:8000
关键依赖(pom.xml)
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
关键配置(application.properties)
spring.application.name=demo-springcloud-alibaba-nacos-consumer
server.port=8000spring.cloud.nacos.discovery.server-addr=192.168.1.200:8848
spring.cloud.nacos.discovery.namespace=prod
spring.cloud.nacos.discovery.group=demo
spring.cloud.loadbalancer.nacos.enabled=truemanagement.endpoints.web.exposure.include=*
启动类:
@SpringBootApplication
@EnableDiscoveryClient
public class SpringcloudAlibabaNacosConsumer {public static void main(String[] args) {SpringApplication.run(SpringCloudNetflixEurekaConsumer.class, args);}
}
服务提供者SDK(Provider Service sdk)
实际工作中,该工程应该由服务提供者开发团队开发,供服务消费者依赖调用,来实现远程调用。本实例使用openfeign。
关键依赖(pom.xml)
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
关键代码(demo-springcloud-alibaba-nacos-provider-sdk工程ProviderService类):
// 接口,无需手写实现类
@Component
@FeignClient(name = ProviderConstants.SERVICE_ID)
public interface ProviderService {@RequestMapping(value = "/echo/{str}")String echo(@RequestParam("str") String str);
}
服务提供者依赖后使用(demo-springcloud-alibaba-nacos-provider1工程)
服务消费者依赖后使用(demo-springcloud-alibaba-nacos-consumer工程)
@Autowired
private ProviderService providerService;@ResponseBody
@RequestMapping(value = "/echo2/{str}")
public String echo2(@PathVariable String str) {return providerService.test(name);
}
0500 负载均衡
客户端负载,按服务端权重负载:(demo-springcloud-alibaba-nacos-consumer工程)
@Configuration(proxyBeanMethods = false)
public class CustomLoadBalancerConfig {@Resourceprivate NacosDiscoveryProperties nacosDiscoveryProperties;@Beanpublic ReactorLoadBalancer<ServiceInstance> nacosLoadBalancer(Environment environment, LoadBalancerClientFactory loadBalancerClientFactory) {var name = ProviderConstants.SERVICE_ID;return new NacosLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name,nacosDiscoveryProperties);}
}
0600 源码地址
https://gitee.com/xiaojianhx/demo-springcloud-alibaba-nacos