spring:
application:
name: gorit-member
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
server:
port: 8080
- 导入父依赖 xml
<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”>
4.0.0
cn.gorit
AlibabaNacos
pom
1.0-SNAPSHOT
gorit-member-8080
org.springframework.boot
spring-boot-starter-parent
2.3.4.RELEASE
org.springframework.boot
spring-boot-starter-web
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
2.2.3.RELEASE
- 编写 消费类
package cn.gorit.service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
-
@Classname MemberService
-
@Description TODO
-
@Date 2020/12/11 1:42
-
@Created by CodingGorit
-
@Version 1.0
*/
@RestController
public class MemberService {
// 这个借口在下面的 rpc 远程调用会使用到
@GetMapping(“/user”)
public String getUser() {
return “user”;
}
@GetMapping(“/user/{id}”)
public String getUser(@PathVariable(“userId”) Integer userId) {
return “xxx”;
}
}
- 编写主类 (前提保证 nacos 必须运行)
package cn.gorit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
-
@Classname App
-
@Description TODO
-
@Date 2020/12/11 1:52
-
@Created by CodingGorit
-
@Version 1.0
*/
// 服务注册不需要自己加进去
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class);
}
}
可以看到服务自动被注册了
2.3.3 使用 DiscoverClient 实现 RPC 远程调用
编写第二个 module
- 编写配置
spring:
application:
name: gorit-order
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
server:
port: 8081
<