接上篇环境搭建springcloud alibaba 环境搭建
1. 下载启动nacos
下载地址:https://github.com/alibaba/nacos/releases
启动命令:startup.cmd -m standalone
用户名密码:nacos/nacos
2. 服务注册
2.1 在user模块新建启动类并注册一个controller用于测试
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@RestController
class EchoController {
@GetMapping(value = "/hello")
public String hello() {
return "hello from user";
}
}
}
2.2 在user模块新建application.properties
spring.application.name=service-user
server.port=18082
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
2.3 pom文件引入依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloudalibaba</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>alibaba-user</artifactId>
<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>
</project>
3. 服务发现
使用order模块作为消费者,发现user服务
3.1 在order模块引入feign依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloudalibaba</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>alibaba-order</artifactId>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
3.2 编写feign接口,用于调用
package com.zhangwei.learn.alibaba.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(value = "service-user")
public interface IUserService {
@GetMapping(value = "/hello")
String hello();
}
3.3 编写启动类和controller,用于测试
package com.zhangwei.learn.alibaba;
import com.zhangwei.learn.alibaba.feign.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@RestController
class EchoController {
@Autowired
IUserService userService;
@GetMapping(value = "/hello")
public String hello() {
return userService.hello()+": order";
}
}
}
3.4 在order模块新建application.properties
spring.application.name=service-order
server.port=18083
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
4.测试
分别启动user服务和order服务,然后在浏览器输入:http://localhost:18083/hello,得到
hello from user: order
说明服务注册发现成功
同时登陆nacos地址:http://loacalhost:8848/nacos/index.html,可以看到user和order服务注册成功
服务名 | 分组名称 | 集群数目 | 实例数 | 健康实例数 | 触发保护阈值 | 操作 |
---|---|---|---|---|---|---|
service-order | DEFAULT_GROUP | 1 | 1 | 1 | false | 详情|示例代码|订阅者|删除 |
service-user | DEFAULT_GROUP | 1 | 1 | 1 | false | 详情|示例代码|订阅者|删除 |