一. 安装配置注册中心nacos
1.下载地址:https://github.com/alibaba/nacos/releases ,我下载的版本为nacos-server-2.0.3.tar.gz。
2.上传到服务器/home/soft目录,解压:tar -xvf nacos-server-2.0.3.tar.gz 。
3.启动nacos
cd nacos/bin
启动命令(standalone代表着单机模式运行,非集群模式):
sh startup.sh -m standalone
4.防火墙开放8848端口
firewall-cmd --zone=public --add-port=8848/tcp --permanent
firewall-cmd --reload
5.查看nacos是否启动成功
http://localhost:8848/nacos/index.html
账号密码:nacos/nacos
详见:Nacos 快速开始 。
二.使用Spring Cloud Alibaba开发服务提供者
1.引入spring-cloud、spring-cloud-alibaba依赖,引入 Nacos Discovery 依赖进行服务注册与发现。
配置pom.xml:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>edas-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>edas-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR6</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</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>
</dependencies>
<dependencyManagement>
<dependencies>
<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>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.在工程的配置文件application.yml文件,指定程序名为edas-provider,端口号为8765,服务注册的nacos地址为47.97.112.218:8848。
spring:
application:
name: edas-provider
cloud:
nacos:
discovery:
server-addr: 47.97.112.218:8848
server:
port: 8765
3.启动类加上注解@EnableDiscoveryClient
package com.example.edasserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}
4.新建测试controller
package com.example.edasserver;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Value("${server.port}")
String port;
@RequestMapping(value = "sayHello")
public String sayHello(String name) {
return "你好,我的名字叫:" + name + "," + port;
}
}
5.启动服务提供者
java -Dserver.port=8765 -jar edas-provider-0.0.1-SNAPSHOT.jar &
java -Dserver.port=8767 -jar edas-provider-0.0.1-SNAPSHOT.jar &
这时你会发现:edas-provider在nacos注册了2个实例,这就相当于一个小的集群:
三.使用Spring Cloud Alibaba开发服务消费者
1.服务消费者也要引入spring-cloud、spring-cloud-alibaba、 Nacos Discovery 依赖。我们需要使用Feign去消费服务,所以要引入Feign的依赖。
配置pom.xml:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>edas-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>edas-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR6</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<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>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.在工程的配置文件application.yml文件,指定程序名为edas-consumer,端口号为8766,服务注册的nacos地址为47.97.112.218:8848,以及Feign配置参数。
spring:
application:
name: edas-consumer
cloud:
nacos:
discovery:
server-addr: 47.97.112.218:8848
server:
port: 8766
feign:
client:
config:
default:
connect-timeout: 20000
read-timeout: 20000
3.启动类加上注解@EnableDiscoveryClient,加上@EnableFeignClients注解开启Feign的功能。
package com.example.edasconsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class EdasConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(EdasConsumerApplication.class, args);
}
}
4.定义一个feign接口,通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了edas-provider服务的“/sayHello”接口,代码如下:
package com.example.edasconsumer;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(name = "edas-provider")
public interface EchoService {
@RequestMapping(value = "/sayHello", method = RequestMethod.GET)
String sayHello(@RequestParam(value = "name")String name);
}
5.在Web层的controller层,对外暴露一个"/sayHello"的API接口,通过上面定义的Feign客户端EchoService 来消费服务。代码如下:
package com.example.edasconsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private EchoService echoService;
@RequestMapping(value = "/sayHello", method = RequestMethod.GET)
public String sayHello(@RequestParam(value = "name") String name) {
return echoService.sayHello(name);
}
}
6.启动程序,查看nacos控制台
可以看到服务消费者已经注册到类nacos。
7.多次访问http://localhost:8766/sayHello?name=yangfeng,浏览器交替显示:
你好,我的名字叫:yangfeng,8765
你好,我的名字叫:yangfeng,8767
说明服务消费者成功调用了服务消费者集群。
参考文档:
https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html
https://github.com/alibaba/spring-cloud-alibaba/tree/master/spring-cloud-alibaba-examples