导入依赖
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.3.12.RELEASEversion>
parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Hoxton.SR12version>
<scope>importscope>
<type>pomtype>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.2.7.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
导入依赖:
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-dubboartifactId>
dependency>
<dependency>
<groupId>com.alibaba.springgroupId>
<artifactId>spring-context-supportartifactId>
<version>1.0.11version>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-lang3artifactId>
dependency>
<dependency>
<groupId>org.examplegroupId>
<artifactId>service_mgartifactId>
<version>1.0-SNAPSHOTversion>
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);
}
}
1.导入依赖:
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-dubboartifactId>
dependency>
<dependency>
<groupId>com.alibaba.springgroupId>
<artifactId>spring-context-supportartifactId>
<version>1.0.11version>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-lang3artifactId>
dependency>
<dependency>
<groupId>org.examplegroupId>
<artifactId>service_mgartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
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!!!
一.分组:
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.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
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