对象篇
模块化编程-自研模块加载器
开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】
com.alibaba.cloud
spring-cloud-alibaba-dependencies
2.1.0.RELEASE
pom
import
org.springframework.cloud
spring-cloud-dependencies
Greenwich.RELEASE
pom
import
org.springframework.boot
spring-boot-dependencies
2.1.3.RELEASE
pom
import
org.springframework.boot
spring-boot-maven-plugin
创建生产者模块
以下步骤演示了如何将一个服务生产者注册到 Nacos。
- 1、pom.xml的配置。
包括Spring Cloud Feign组件、Spring Cloud Alibaba Nacos Discovery组件以及Spring boot web相关组件依赖。
<?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”>
nacos-discovery
com.demo.nacos
1.0-SNAPSHOT
4.0.0
quickstart-provider
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-openfeign
- 2、application.yml配置。一些关于 Nacos 基本的配置也必须在application.yml(也可以是application.properties)
配置,如下所示: application.yml
server:
port: 56010 # 启动端口
spring:
application:
name: quickstart-provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
logging:
level:
root: info
org.springframework: info
Note: spring.cloud.nacos.discovery.server-addr 指定了Nacos Server的网络地址和端口号。
- 3、启动Provider(生产者)
package com.demo.nacos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
-
Description:
-
DATE: 2022/1/26 17:14
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosProviderApp {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApp.class,args);
}
}
- 4、Provider(生产者)服务实现
package com.demo.nacos.controller;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.logging.Logger;
/**
-
Description:
-
DATE: 2022/1/26 17:15
*/
@RestController
public class ProviderController {
private static final Logger LOG = (Logger) LoggerFactory.getLogger(ProviderController.class);
@GetMapping(“/servicre”)
public String service(){
LOG.info(“provider invoke”);
return “provider invoke”;
}
}
1、pom.xml的配置。
<?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”>
nacos-discovery
com.demo.nacos
1.0-SNAPSHOT
4.0.0
quickstart-consumer
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-openfeign
2、application.yml配置
server:
port: 56020 # 启动端口
spring:
application:
name: quickstart-consumer
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
logging:
level:
root: info
org.springframework: info
3、Consumer(消费者)启动类
package com.demo.nacos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
-
Description:
-
DATE: 2022/1/26 17:42
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosConsumerApp {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApp.class, args);
}
}
4、Provider(生产者)远程代理定义
package com.demo.nacos.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
/**
-
Description:
-
DATE: 2022/1/26 17:36
*/
@FeignClient(value = “quickstart-provider”)
public interface ProviderClient {
@GetMapping(“service”)
String service();
}
5、Consumer(消费者)服务实现
package com.demo.nacos.controller;
import com.demo.nacos.client.ProviderClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
-
Description:
-
DATE: 2022/1/26 17:39
*/
@RestController
public class ConsumerController {
private static final Logger log = LoggerFactory.getLogger(ConsumerController.class);
@Autowired
ProviderClient providerClient;
@GetMapping(“/service”)
public String service(){
log.info(“consumer invoke”);
//远程调用
String providerResult = providerClient.service();
return “consumer invoke”+“|”+providerResult;
}
}
浏览器访问 http://127.0.0.1:8848/nacos ,打开nacos控制台,并点击菜单服务管理->服务列表,可展示出刚刚注
册的两个服务:
访问地址:http://127.0.0.1:56020/service
总结
秋招即将开始,校招的朋友普遍是缺少项目经历的,所以底层逻辑,基础知识要掌握好!
而一般的社招,更是神仙打架。特别强调,项目经历不可忽视;几乎简历上提到的项目都会被刨根问底,所以项目应用的技术要熟练,底层原理必须清楚。
这里给大家提供一份汇集各大厂面试高频核心考点前端学习资料。涵盖 HTML,CSS,JavaScript,HTTP,TCP协议,浏览器,Vue框架,算法等高频考点238道(含答案)!
开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】
资料截图 :
高级前端工程师必备资料包