一、项目框架选型
SpringBoot : 版本 2.1.4.RELEASE
Spring Cloud :版本 Greenwich.RELEASE
Spring Cloud Alibaba: 版本 2.1.1.RELEASE
二、注册中心服务端搭建
本项目注册中心使用的是 spring cloud alibaba中的nacos.
nacos官网:https://nacos.io/zh-cn/docs/what-is-nacos.html
linux环境服务端搭建过程(参考https://github.com/alibaba/Nacos):
a.下载nacos-server
# wget https://github.com/alibaba/nacos/releases/download/1.2.0/nacos-server-1.2.0.tar.gz
b.解压
# unzip nacos-server-1.2.0.zip
# cd nacos/bin
c.启动服务(特别注意非集群模式下不能直接启动)
sh startup.sh -m standalone #单节点启动
启动完成后访问:ip:8848,默认账号密码为nacos/nacos,端口等基本信息可以进入conf/application.properties下修改
三、项目搭建
项目架构:
POM配置文件
ms-demo pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath />
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<modules>
<module>ms-demo-a</module>
<module>ms-demo-b</module>
</modules>
ms-demo-a、ms-demo-b pom.xml
<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>
Spring Boot Starter
ms-demo-a:
@SpringBootApplication
@EnableDiscoveryClient //开启服务发现
public class MicroserviceAStarter {
public static void main(String[] args) throws Exception {
SpringApplication.run(MicroserviceAStarter.class, args);
}
@Bean
@LoadBalanced //ribbo负载均衡
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
ms-demo-b:
@SpringBootApplication
@EnableDiscoveryClient //开启服务发现
public class MicroserviceBStarter {
public static void main(String[] args) throws Exception {
SpringApplication.run(MicroserviceBStarter.class, args);
}
@Bean
@LoadBalanced //ribbo负载均衡
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
application.yml配置文件
ms-demo-a:
server:
port: 8001
spring:
application:
name: demo-a
cloud:
nacos:
server-addr: localhost:8848 #写你自己的nacos服务地址
discovery:
group: ms-demo
ms-demo-b:
server:
port: 8002
spring:
application:
name: demo-b
cloud:
nacos:
server-addr: localhost:8848 #写你自己的nacos服务地址
discovery:
group: ms-demo
测试用例:
ms-demo-a:
@RestController
public class TestAController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/sayHello/{word}")
public String sayHello(@PathVariable("word") String word) {
return restTemplate.getForObject("http://demo-b/callback/" + word, String.class);
}
}
ms-demo-b:
@RestController
public class TestBController {
@GetMapping("/callback/{word}")
public String sayHello(@PathVariable("word") String word) {
return "hello : " + word;
}
}
分别启动微服务demo-a、demo-b
Nacos服务列表:
测试调用:
微服务注册中心到此就搭建好了。