Nacos是什么
Nacos 致力于幫助您發現、配置和管理微服務,它 提供了一組簡單易用的特性集,幫助您快速實作動態服務發現、服務配置、服務元資料及流量管理,
注冊中心
nacos-server
可以直接從GitHub上下載安裝包:https://github.com/alibaba/nacos/releases
啟動成功后,瀏覽器訪問:http://127.0.0.1:8848/nacos/index.html Nacos控制臺,默認的賬號密碼為nacos/nacos
服務提供者
@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class NacosProvideApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProvideApplication.class, args);
}
@GetMapping("/helloNacos")
public String helloNacos(){
return "hello,nacos!";
}
}
server:
port: 9527
spring:
application:
name: nacos-provide
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
服務消費者
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class NacosConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApplication.class, args);
}
@Autowired
private RestTemplate restTemplate;
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
@GetMapping("/consumer")
public String test1() {
String result = restTemplate.getForObject("http://nacos-provide/helloNacos", String.class);
return "Return : " + result;
}
}
server:
port: 9528
spring:
application:
name: nacos-consumer
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
對接OpenFeign
定義遠程介面
通過@FeginClient注解指定被呼叫方的服務名,通過fallback屬性指定RemoteHystrix類,來進行遠程呼叫的熔斷和降級處理,
@FeignClient(name = "nacos-provide",fallback = RemoteHystrix.class)
public interface RemoteClient {
@GetMapping("/helloNacos")
String helloNacos();
}
@Component
public class RemoteHystrix implements RemoteClient {
@Override
public String helloNacos() {
return "請求超時了";
}
}
呼叫遠程介面
@SpringBootApplication
@RestController
@EnableDiscoveryClient
@EnableFeignClients
public class NacosFeignApplication {
public static void main(String[] args) {
SpringApplication.run(NacosFeginApplication.class, args);
}
@Autowired
private RemoteClient remoteClient;
@GetMapping("/feign")
public String test() {
return remoteClient.helloNacos();
}
}
server:
port: 9529
spring:
application:
name: nacos-feign
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
配置中心
在Nacos-Server中新建配置,其中Data ID它的定義規則是:${prefix}-${spring.profile.active}.${file-extension}
-
prefix 默認為 spring.application.name 的值,也可以通過配置項 spring.cloud.nacos.config.prefix 來配置,
-
spring.profile.active 即為當前環境對應的 profile,可以通過配置項 spring.profile.active 來配置,
-
file-exetension 為配置內容的資料格式,可以通過配置項 spring.cloud.nacos.config.file-extension 來配置,目前只支持 properties 和 yaml 型別,
-
** 注意:當 spring.profile.active 為空時,對應的連接符 - 也將不存在,dataId 的拼接格式變成 ${prefix}.${file-extension}**
spring:
application:
name: nacos-config
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
file-extension: yml@SpringBootApplication @EnableDiscoveryClient @RestController @RefreshScope public class NacosConfigApplication { public static void main(String[] args) { SpringApplication.run(NacosConfigApplication.class, args); } @Value("${nacos.config}") private String config; @RequestMapping("/getValue") public String getValue() { return config; } }
多環境配置
Data ID方案
Data ID的命名規則為:${prefix}-${spring.profile.active}.${file-extension},通過其中的spring.profile.active屬性即可進行多環境下組態檔的讀取
Group方案(不推薦使用)
首先配置Group為自定義Group
其次修改專案組態檔bootstrap.yml
spring:
application:
name: nacos-config
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
prefix: ${spring.application.name}
file-extension: yml
group: DEV_GROUP
命名空間方案
先創建命名空間,然后在命名空間下建Data ID
spring:
application:
name: nacos-config
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
prefix: ${spring.application.name}
file-extension: yml
namespace: edbd013b-b178-44f7-8caa-e73071e49c4d
共享配置
共享組態檔與專案自身組態檔在同一Group中
spring:
application:
name: nacos-config-share
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
prefix: ${spring.application.name}
file-extension: yml
shared-dataids: shareconfig1.yml,shareconfig2.yml
refreshable-dataids: shareconfig1.yml,shareconfig2.yml
共享組態檔與專案自身組態檔不在同一Group中
spring:
application:
name: nacos-config-share
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
prefix: ${spring.application.name}
file-extension: yml
shared-dataids: shareconfig1.yml,shareconfig2.yml
refreshable-dataids: shareconfig1.yml,shareconfig2.yml
ext-config:
- data-id: shareconfig3.yml
group: SHARE3_GROUP
refresh: true
- data-id: shareconfig4.yml
group: SHARE4_GROUP
refresh: true
持久化
在0.7版本之前,在單機模式時nacos使用嵌入式資料庫實作資料的存盤,不方便觀察資料存盤的基本情況,0.7版本增加了支持mysql資料源能力
- 進入nacos-server\nacos\conf目錄,初始化檔案:nacos-mysql.sql
- Nacos-server其實就是一個Java工程或者說是一個Springboot專案,他的組態檔在nacos-server-1.0.1\nacos\conf目錄下,名為 application.properties,在檔案底部添加資料源配置:
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://127.0.0.1:3306/mynacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=123456 - 啟動Nacos-server即可,
集群部署
添加mysql資料源
修改Nacos-server目錄conf/下的application.properties檔案
修改集群配置
修改conf/下的cluster.conf.example檔案,將其命名為cluster.conf,內容如下
10.1.8.27:8848
10.1.8.28:8848
10.1.8.29:8848
配置Nginx
upstream nacos-server {
server 127.0.0.1:8849;
server 127.0.0.1:8850;
server 127.0.0.1:8851;
}
server {
listen 8848;
server_name localhost;
location /nacos/ {
proxy_pass http://nacos-server/nacos/;
}
}
本文由博客群發一文多發等運營工具平臺 OpenWrite 發布
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/138067.html
標籤:Java
上一篇:Linux下安裝tomcat9
