Press "Enter" to skip to content

CentOS 7 安装 Alibaba Nacos

Read Time:6 Minute, 10 Second

准备好centos7,配好JAVA_HOME, Nacos下载地址
tar xf nacos-server-1.1.4.tar.gz -C {指定解压目录}
cd {解压目录}
#修改持久层为mysql(默认是用derby)
#将 conf/nacos-mysql.sql怼到你的数据库
vi conf/application.properties
#添加以下内容,具体内容根据自己的环境定,比如数据,用户和密码
# spring.datasource.platform=mysql
# db.num=1
# db.url.0=jdbc:mysql://11.162.196.16:3306/nacos_devtest?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
# db.user=nacos_devtest
# db.password=youdontknow

#启动单节点
sh startup.sh -m standalone

#入口地址
#http://{本机ip}/nacos/index.html#/login
#默认用户名/密码为 nacos/nacos
复制代码
nacos可以做为配置中心(同类产品有apollo, springcloud config), 注册中心(同类型eureka, consul,zookeeper)。
nacos可以做高可用,集群,具体怎么搭,请参考官网文档。

SpringBoot2.X整合Nacos的配置中心

依赖

第一种:使用springboot客户端。
Maven Pom

 <properties>
<nacos-config-spring-boot.version>0.2.1</nacos-config-spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>${nacos-config-spring-boot.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-actuator</artifactId>
<version>${nacos-config-spring-boot.version}</version>
</dependency>
</dependencies>
复制代码

本地properties配置

nacos.config.server-addr=地址:8848
复制代码

创建Nacos配置

Springboot 注入

@SpringBootApplication
@NacosPropertySource(dataId = "first_data", groupId = "first_group", autoRefreshed = true)
@RestController
public class NacosApplication {
@NacosValue(value = "${test.mytest:defaultString}", autoRefreshed = true)
private String test;
public static void main(String[] args) {
SpringApplication.run(NacosApplication.class, args);
}
@GetMapping("/get")
public JsonNode get() {
ObjectNode jsonNodes = JsonNodeFactory.instance.objectNode();
jsonNodes.put("testStringIs", test);
return jsonNodes;
}
}
复制代码

第二种:使用com.alibaba.cloud

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
复制代码

另外你的父pom

 <dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
复制代码

不过com.alibaba.cloud整合springboot我自己整合的时候,是失败的,nacos客户端根本没有发http请求去拿配置数据,也就无法注入springboot容器。

第三种:用spirng-cloud封装好的starter

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>0.9.0.RELEASE</version>
</dependency>
复制代码
spring.cloud.nacos.config.server-addr=192.168.208.102:8848
spring.application.name=first_data
spring.cloud.nacos.config.group=first_group
spring.cloud.nacos.config.file-extension=properties
复制代码

这个starter是有坑的,比如我拿之前first_data / first_group,他发http请求会发到http://192.168.208.102:8848/nacos/v1/cs/configs?dataId=first_data.properties&group=first_group,但是这个地址是拿不到的,需要去除掉first_data后缀properties才能拿到数据的,不知道是spring封装的有问题还是。抓包详情如下图(haha是已存在的一个dataid)。

# 抓包工具Mitmproxy
#java 参数指定 
-DproxyHost=127.0.0.1 -DproxyPort=8080
复制代码

Springboot服务注册

父工程pom

 <dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.9.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
</dependencyManagement>
复制代码

子项目pom

 <properties>
<nacos-discovery-spring-boot.version>0.2.1</nacos-discovery-spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.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>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.0.Final</version>
</dependency>
</dependencies>
复制代码

Springboot启动类

@SpringBootApplication
@EnableDiscoveryClient
public class Producer {
public static void main(String[] args) {
SpringApplication.run(Producer.class, args);
}
@RestController
class EchoController {
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
public String echo(@PathVariable String string) {
return "Hello Nacos Discovery " + string;
}
}
}
复制代码

application.properties

server.port=8100
spring.application.name=nacos-provider
spring.cloud.nacos.discovery.server-addr=192.168.208.102:8848
复制代码

服务发现于调用

pom

<properties>
<spring-cloud-openfeign.version>2.0.0.RELEASE</spring-cloud-openfeign.version>
<spring-cloud-netflix.version>2.0.0.RELEASE</spring-cloud-netflix.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>${spring-cloud-netflix.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>${spring-cloud-openfeign.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.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>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
</dependencies>
复制代码

启动类

@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@RestController
public class TestController {
private final RestTemplate restTemplate;
@Autowired
public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}
@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
public String echo(@PathVariable String str) {
return restTemplate.getForObject("http://nacos-provider/echo/" + str, String.class);
}
}
}
复制代码

application.properties

server.port=8200
spring.application.name=service-consumer
spring.cloud.nacos.discovery.server-addr=192.168.208.102:8848
复制代码

Feign客户端集成

pom

 <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
复制代码

声明Feign

@FeignClient("nacos-provider")
public interface Consumer {
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
public String echo(@PathVariable String string);
}
复制代码

Ctroller集成Feign

@RestController
public class FeignCtl {
@Autowired
Consumer consumer;
@GetMapping("/hello")
public String hello(@RequestParam String echoStr){
return consumer.echo(echoStr);
}
}
复制代码

Feign集成Sentinel,服务熔断

   <dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
复制代码

Springboot配置

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.208.102:8848
feign:
  sentinel:
    enabled: true
复制代码

声明feign时候指定熔断服务类

@FeignClient(value = "nacos-provider", fallback = EchoServiceFallback.class)
public interface Consumer {
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
public String echo(@PathVariable String string);
}
复制代码

熔断服务类

@Component
public class EchoServiceFallback  implements Consumer {
@Override
public String echo(String string) {
return "服务熔断,请稍微重试";
}
}
复制代码

服务熔断监控

#官方release地址
https://github.com/alibaba/Sentinel.git
复制代码
spring:
  cloud:
    sentinel:
      transport:
        port: 8719
        dashboard: localhost:8080
0 0
Happy
Happy
100 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleppy
Sleppy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Be First to Comment

    发表评论

    电子邮件地址不会被公开。 必填项已用*标注