一、运行nacos
下载nacos:
https://github.com/alibaba/nacos/releases
选用 2.x 任一版本即可
我这里直接在windows环境下运行
配置文件路径 nacos\conf\application.properties,默认配置即可:
server.servlet.contextPath=/nacos
### Include message field
server.error.include-message=ALWAYS
### Default web server port:
server.port=8848
启动nacos
使用命令行进入到nacos的bin目录,输入以下命令启动,这种启动方式是以单个nacos启动
startup.cmd -m standalone
启动成功后,登录: http://localhost:8848/nacos
默认用户名:nacos
密码:nacos
可以做服务管理、集群管理、持久化配置等等
二、搭建工程
1、搭建父工程:spring-cloud-alibaba-test,模板选用 maven-archetype-quickstart
创建之后,删除src,配置pom.xml
必备:
spring-boot-dependencies
spring-cloud-dependencies
spring-cloud-alibaba-dependencies
其他的依赖可以不要
4.0.0
org.example
spring-cloud-alibaba-test
pom
1.0-SNAPSHOT
spring-cloud-alibaba-test
UTF-8
1.8
1.8
4.12
1.2.17
1.16.18
5.1.47
1.1.16
3.4.0
org.springframework.boot
spring-boot-dependencies
2.3.2.RELEASE
pom
import
org.springframework.cloud
spring-cloud-dependencies
Hoxton.SR9
pom
import
com.alibaba.cloud
spring-cloud-alibaba-dependencies
2.2.6.RELEASE
pom
import
mysql
mysql-connector-java
${mysql.version}
com.alibaba
druid
${druid.version}
com.baomidou
mybatis-plus-boot-starter
${mybatis-plus.boot.version}
junit
junit
${junit.version}
log4j
log4j
${log4j.version}
org.projectlombok
lombok
${lombok.version}
true
org.apache.maven.plugins
maven-surefire-plugin
true
注意这三个依赖的版本关系,存在兼容问题,推荐关系如下:
2、创建server子工程:nacos-server
模板依然是maven-archetype-quickstart,缺什么自己再添加
配置pom.xml
spring-cloud-alibaba-test
org.example
1.0-SNAPSHOT
4.0.0
nacos-server
jar
nacos-server
UTF-8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.boot
spring-boot-maven-plugin
添加资源文件夹 main/resources
添加application.yml,内容简单点:
#端口默认9001,如果启动时指定参数 -D9002,就会启动9002端口
server:
port: ${port:9001}
#端口默认9001,如果启动时指定参数 -D9002,就会启动9002端口
spring:
application:
name: nacos-server
cloud:
nacos:
discovery:
server-addr: localhost:8848
username: nacos
password: nacos
namespace: public
启动类Application.java配置:
package org.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
随便写个服务controller:
package org.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(path = "/server")
public class ServerController {
@RequestMapping(path = "get")
public String get(){
System.out.println("hello~");
return "hello~我是返回信息";
}
}
然后启动Application,可以启动两个实例。一个9001 一个9002
一个正常启动,一个在启动配置中,添加一个VM options,添加参数:-Dport=9002
3、创建client子工程:nacos-client
模板依然是maven-archetype-quickstart,缺什么自己再添加
配置pom.xml
spring-cloud-alibaba-test
org.example
1.0-SNAPSHOT
4.0.0
nacos-client
jar
nacos-client
UTF-8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.boot
spring-boot-maven-plugin
添加资源文件夹 main/resources
添加application.yml,端口9100,内容简单点:
server:
port: 9100
spring:
application:
name: nacos-client
cloud:
nacos:
discovery:
server-addr: localhost:8848
username: nacos
password: nacos
namespace: public
添加启动类ClientApplication
package org.example.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
配置Ribbon:
package org.example.client.config;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RibbonConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(RestTemplateBuilder builder) {
RestTemplate restTemplate = builder.build();
return restTemplate;
}
}
随便写个Controller:
package org.example.client.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping(path = "/client")
public class ClientController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping(path = "test")
public String get(){
String message = restTemplate.getForObject("http://nacos-server/server/get", String.class);
return "调用成功!返回信息:" + message;
}
}
注意:这里的请求URL:http://nacos-server/server/get,其中nacos-server是上面工程的名字,也是他注册到nacos上的服务名:
启动两个server,一个client
三 、测试
首先,确保nacos上的服务正常:
nacos-server 2个实例
nacos-client 1个实例
然后调用nacos-client的服务:http://localhost:9100/client/test
返回信息
该服务调用了nacos-server,而nacos-server我们启动了两个实例,观察console,已经实现了负载均衡:
我们刷新了5次页面,3次请求到nacos-server1,2次请求到nacos-server2
nacos-server1
nacos-server2