文章目录
hello world
创建父工程parent
导入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<!-- spring cloud 总体依赖版本管理 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<!-- spring cloud alibaba 总体依赖版本管理 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.7.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
创建子工程: nacosserver(被远程调用调用方)
导入依赖:
<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>
<!-- dubbo技术在spring cloud alibaba技术栈中的必要依赖
且需要另外一个必要依赖。commons-lang3
-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<!-- 过往版本中必要,现版本中已修补此BUG -->
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>1.0.11</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>service_mg</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
2.yml配置
server:
port: 8003
spring:
application:
name: applicationserver
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #注册到nacos中是项目中控制层
dubbo:
#application:
name: dubboserver #dubbo服务的名称 也是放到注册中心中名字 如果这个名字不指定 默认和spring.application.name保持一致
registry:
address: nacos://127.0.0.1:8848 #注册到注册中心是项目的 业务层
protocol:
name: dubbo #采用协议
port: -1 #协议端口 默认是20880 如果是-1实现自动递增 从20880开始递增 直到可以使用端口
3.单独编写一个模块:server_mg,方业务层接口(为后面使用dubbo做准备)
service:
public interface ServerService {
String show();
}
4.在nacosserver模块依赖server_mg模块后,编写具体实现类
@Service
@DubboService
public class ServiceServiceImpl implements ServerService {
@Override
public String show() {
return "hello world!!!";
}
}
5.编写控制层调用业务层: 为后面Openfeign做准备
@RestController
public class ServerController {
@Autowired
private ServerService serverService;
@RequestMapping("/abc")
public String show(){
return serverService.show();
}
}
6.编写启动类
@SpringBootApplication
@EnableDubbo
@EnableDubboConfig
public class ServerApplicatinStarter {
public static void main(String[] args) {
SpringApplication.run(ServerApplicatinStarter.class,args);
}
}
创建子工程: applicationclient(远程调用方)
1.导入依赖:
<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>
<!-- dubbo技术在spring cloud alibaba技术栈中的必要依赖
且需要另外一个必要依赖。commons-lang3
-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<!-- 过往版本中必要,现版本中已修补此BUG -->
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>1.0.11</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>service_mg</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--openfeign远程调用 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
2.yml配置
server:
port: 8002
spring:
application:
name: applicationclient
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #注册到nacos中是项目中控制层
dubbo:
#application:
name: dubboclient #dubbo服务的名称 也是放到注册中心中名字 如果这个名字不指定 默认和spring.application.name保持一致
registry:
address: nacos://127.0.0.1:8848 #注册到注册中心是项目的 业务层
protocol:
name: dubbo #采用协议
port: -1 #协议端口 默认是20880 如果是-1实现自动递增 从20880开始递增 直到可以使用端口
3.创建业务层接口ServerService2(为使用Openfeign做准备)
@FeignClient("applicationserver")
public interface ServerService2 {
@RequestMapping("/abc")
public String show();
}
4.三种方式的远程调用:
@RestController
public class ClientController {
@DubboReference(check = false)
private ServerService serverService;
@Autowired
private RestTemplate restTemplate;
@Autowired
private ServerService1 serverService2;
@RequestMapping("/fs")
public String agc(){
//RestTemplate
String restTemplateTest = restTemplate.getForObject("http://applicationserver/abc", String.class);
System.out.println(restTemplateTest);
//dubbo
String dubboTest = serverService.show();
System.out.println(dubboTest);
//Openfeign
String openfeignTest = serverService2.show();
System.out.println(openfeignTest);
return openfeignTest;
}
}
5.访问http://127.0.0.1:8002/fs,控制台打印
hello world!!!
hello world!!!
hello world!!!
分组详解 && Nacos作为配置中心
一.分组:
server:
port: 8002
spring:
application:
name: applicationclient
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
group: controller123 # 控制层分组
dubbo:
#application:
name: dubboclient
registry:
address: nacos://127.0.0.1:8848
group: service123 # 业务层分组
protocol:
name: dubbo
port: -1
-------------------------------------------------------------------------------
spring:
application:
name: nacos-config
cloud:
nacos:
config:
name: xy
group: fs # 配置文件所属分组。默认是DEFAULT_GROUP
server-addr:
file-extension: yaml
二.Nacos作为配置中心
1.导入依赖
<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>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
2.yml配置
spring:
application:
name: nacos-config
cloud:
nacos:
config:
name: testconfig # 配置文件的 data id。 默认是${spring.application.name}
group: fs # 配置文件所属分组。默认是DEFAULT_GROUP
server-addr: 127.0.0.1:8848 # config配置中心地址。默认是${spring.cloud.nacos.server-addr}
file-extension: yaml # 文件类型。默认是properties