project01 - 使用nacos作为注册中心和配置中心

This commit is contained in:
Shikong 2023-03-26 21:03:14 +08:00
parent c4852c80bd
commit ab92069b81
16 changed files with 303 additions and 6 deletions

View File

@ -2,11 +2,13 @@
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile default="true" name="Default" enabled="true" />
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="project-01" />
<module name="provider" />
<module name="consumer" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>

9
.idea/encodings.xml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/project-01/consumer/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/project-01/consumer/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/project-01/provider/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/project-01/provider/src/main/resources" charset="UTF-8" />
</component>
</project>

15
.idea/git_toolbox_prj.xml Normal file
View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GitToolBoxProjectSettings">
<option name="commitMessageIssueKeyValidationOverride">
<BoolValueOverride>
<option name="enabled" value="true" />
</BoolValueOverride>
</option>
<option name="commitMessageValidationEnabledOverride">
<BoolValueOverride>
<option name="enabled" value="true" />
</BoolValueOverride>
</option>
</component>
</project>

View File

@ -9,7 +9,7 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="graalvm-17" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

51
docs/00.环境部署.md Normal file
View File

@ -0,0 +1,51 @@
### Nacos
#### 从 github 上下载对应环境的安装包
> https://github.com/alibaba/nacos/releases
解压后执行
```shell
cd nacos/bin
```
##### linux
```shell
# 默认为 cluster 集群模式
# sh startup.sh
sh startup.sh -m standalone
```
##### windows
单机模式
```shell
# 默认为 cluster 集群模式
# startup.cmd
startup.cmd -m standalone
```
##### 默认账号密码为 nacos nacos
#### 单机模式下配置 mysql
先在数据库中创建数据库
并执行 **conf/mysql-schema.sql** 初始化数据库表
编辑 conf/application.properties
修改 Config Module Related Configurations 部分
```properties
spring.sql.init.platform=mysql
### Count of DB:
db.num=1
### Connect URL of DB:
db.url.0=jdbc:mysql://[数据库ip]:[端口]/[数据库名]?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=[数据库账号]
db.password.0=[数据库密码]
### Connection pool configuration: hikariCP
db.pool.config.connectionTimeout=30000
db.pool.config.validationTimeout=10000
db.pool.config.maximumPoolSize=20
db.pool.config.minimumIdle=2
```

45
pom.xml
View File

@ -15,4 +15,49 @@
<modules>
<module>project-01</module>
</modules>
<properties>
<java.version>1.8</java.version>
<spring-boot.version>2.4.4</spring-boot.version>
<spring-cloud.version>2020.0.2</spring-cloud.version>
<spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
<lombok.version>1.18.26</lombok.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- spring boot 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring cloud 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring cloud alibaba 依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>

View File

@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.skcks.study.springcloud</groupId>
<artifactId>project-01</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>cn.skcks.study.springcloud.project01</groupId>
<artifactId>consumer</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,15 @@
package cn.skcks.study.springcloud.project01;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@ -0,0 +1,18 @@
package cn.skcks.study.springcloud.project01.controller;
import cn.skcks.study.springcloud.project01.feign.ProviderClient;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@AllArgsConstructor
@RestController
public class ConsumerController {
private final ProviderClient providerClient;
@GetMapping("/hi")
public String hi(){
return providerClient.hi("feign");
}
}

View File

@ -0,0 +1,12 @@
package cn.skcks.study.springcloud.project01.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@FeignClient(value = "project-01-provider")
public interface ProviderClient {
@GetMapping("/hi")
String hi(@RequestParam(value = "name",defaultValue = "Shikong",required = false) String name);
}

View File

@ -0,0 +1,11 @@
server:
port: 8763
spring:
application:
name: project-01-consumer
cloud:
nacos:
discovery:
server-addr: 10.10.10.20:8848

View File

@ -11,5 +11,11 @@
</parent>
<artifactId>project-01</artifactId>
<packaging>pom</packaging>
<modules>
<module>provider</module>
<module>consumer</module>
</modules>
</project>

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.skcks.study.springcloud</groupId>
<artifactId>project-01</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>cn.skcks.study.springcloud.project01</groupId>
<artifactId>provider</artifactId>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,27 @@
package cn.skcks.study.springcloud.project01;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@EnableDiscoveryClient
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Value("${server.port}")
String port;
@GetMapping("/hi")
public String hi(@RequestParam(value = "name", defaultValue = "Shikong",required = false) String name) {
return "hello " + name + ", i'm provider ,my port:" + port;
}
}

View File

@ -0,0 +1,10 @@
server:
port: 8762
spring:
application:
name: project-01-provider
cloud:
nacos:
discovery:
server-addr: 10.10.10.20:8848

View File

@ -1,4 +0,0 @@
package cn.skcks.study.springcloud;
public class Main {
}