版本选择
父工程pom文件:
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.1.17.RELEASE</spring-boot.version>
<!--spring cloud 版本-->
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
<spring-cloud-alibaba.version>2.1.2.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<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>
</dependencies>
</dependencyManagement>
通用依赖:
<dependencies>
<!--springboot最基本的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--nacos 服务注册与发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
服务端(Provider)模块:
添加@EnableDiscoveryClient注解在启动类上
/**
* @description:
* @项目 www.fhadmin.org
* @create 2021-04
**/
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication.class, args);
}
}
//java项目www.fhadmin.org
@RestController
@RequestMapping("/goods")
public class GoodsController {
@Value("${server.port}")
private int port;
@GetMapping("/findOne3")
public Goods findGoodsById3() {
//当前线程睡2秒
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Goods goods = new Goods().setId(1).setPrice(123.123).setCount(1200).setTitle("标题");
return goods.setTitle(goods.getTitle() + ":" + port);
}
}
application.yml:
配置服务名和nacos地址和端口,最下面的配置不用管
server:
port: 8000
spring:
application:
name: nacos-provider # 服务名称
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 # 配置nacos 服务端地址
management:
endpoints:
web:
exposure:
include: '*'
消费端(Consumer)模块
添加@EnableDiscoveryClient注解在启动类上
/**
* @description:
* java项目www.fhadmin.org
* @create 2021-04
**/
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApplication.class, args);
}
}
application.yml:
配置服务名和nacos地址和端口,最下面的配置不用管
server:
port: 9000
spring:
application:
name: nacos-consumer # 服务名称
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 # 配置nacos 服务端地址
management:
endpoints:
web:
exposure:
include: '*'
启动测试:
用户名密码都是 nacos