Spring Cloud 从H版本之后命名方式改为2020.x.x,话说为毛最新版本不是2021命名,来自强迫症的难受
下载地址:https://github.com/alibaba/nacos
创建mysql数据库nacos,导入 conf/nacos-mysql.sql
修改 conf/application.properties
#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:
spring.datasource.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
将nacos设为单机模式启动,编辑 bin/startup.cmd 将MODE 修改为:
set MODE="standalone"
登陆页面:http://localhost:8848/nacos/,登陆用户nacos,登陆密码为nacos
工程案例源码:https://github.com/java-aodeng/springcloud2020-demo-1
工程案例包括2个,一个服务提供者provider 、服务消费者consumer
在父pom文件引入相关的依赖,如下:
<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>
</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>
</dependencies>
</dependencyManagement>
在pom文件引入以下依赖:
<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>
配置文件:
server:
port: 8762
spring:
application:
name: provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
启动入口添加注解:
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class,args);
}
}
写个接口:
@RestController
public class DemoController {
@Value("${server.port}")
String port;
@GetMapping("getInfo")
public String getInfo(@RequestParam(value = "name",defaultValue = "nacosConfig",required = false)String name){
return "my name is "+name+",my port is "+port;
}
}
在pom文件引入以下依赖:
<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>
<!--引入openfeign,必须要引入loadbalancer,否则无法启动-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
配置文件
server:
port: 8763
spring:
application:
name: consumer
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
在工程的启动入口开启FeignClient的功能
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
}
写一个FeignClient,去调用provider服务的接口:
@FeignClient(value = "provider")
public interface ProviderClient {
@GetMapping("getInfo")
String getInfo(@RequestParam(value = "name",defaultValue = "nacosConfig",required = false)String name);
}
Copy
写一个接口,让consumer去调用provider服务的接口:
@RestController
public class DemoController {
@Autowired
ProviderClient providerClient;
@GetMapping("getInfoByFeign")
public String getInfoByFeign(){
return providerClient.getInfo("consumer feign");
}
}
启动两个工程,在nacos服务列表页面出现,consumer,provider2个服务表示都已经注册成功
在浏览器上输入http://localhost:8763/getInfoByFeign,浏览器返回响应
my name is consumer feign,my port is 8761
可见浏览器的请求成功调用了consumer服务的接口,consumer服务也成功地通过feign成功的调用了provider服务的接口。
使用spring cloud loadbanlancer作为负载均衡器。通过修改provider的端口,再在本地启动一个新的provider服务,那么本地有2个provider 服务,端口分别为8761 和8762。在浏览器上多次调用http://localhost:8763/getInfoByFeign,浏览器会交替显示:
my name is consumer feign,my port is 8761
my name is consumer feign,my port is 8762
注册中心provider服务也会显示两个示例
父工程添加nacos配置版本,另一个是用来解决最新版本导致的问题:
<alibaba.nacos.version>2.0.3</alibaba.nacos.version>
<spring-cloud.bootstrap.version>3.0.4</spring-cloud.bootstrap.version>
<!--Alibaba Nacos 配置-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${spring-cloud-alibaba.version}</version>
</dependency>
<!--引入这个依赖解决SpringCloud2020整合Nacos-Bootstrap配置不生效的问题-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>${spring-cloud.bootstrap.version}</version>
</dependency>
服务提供者provider添加依赖:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
在bootstrap.yml(一定是bootstrap.yml文件,不是application.yml文件)文件配置以下内容:
server:
port: 8762
spring:
application:
name: provider
cloud:
nacos:
config:
enabled: true
server-addr: 127.0.0.1:8848
file-extension: yml
prefix: provider
profiles:
active: dev
在上面的配置中,配置了nacos config server的地址,配置的扩展名是ymal(目前仅支持yml和properties)。注意是没有配置server.port的,sever.port的属性在nacos中配置。上面的配置是和Nacos中的dataId 的格式是对应的,nacos的完整格式如下:
${prefix}-${spring.profile.active}.${file-extension}
网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。
添加我为好友,拉您入交流群!
请使用微信扫一扫!