当前位置: 首页 > news >正文

苏州网站建设哪里好网站首页优化公司

苏州网站建设哪里好,网站首页优化公司,网站开发的现状,零基础企业管理培训课程这里写目录标题 多模块项目的搭建父项目的构建子模块的创建父子模块的意义 将注册服务引入到父子模块中创建子模块用于发现服务和调用供调用的服务接口创建调用子模块 测试一些小问题 在前文中我们实现了微服务的注册参考此文#xff1a; Spring Cloud Alibaba中Nacos的安装 Spring Cloud Alibaba中Nacos的安装Windows平台以及服务的发现 现在我们需要创建另一个项目来发现服务以及调用注册的服务 多模块项目的搭建 为了方便之后的其他组件的引入我这里就不再创建新的工程了而是把之前的注册服务和接下来的nacos发现服务都放到同一个工程里。 也就是从之前的 转为以下这样 父项目的构建 得到的目录结构如图 然后将src目录删除因为父模块不需要运行得到以下的目录结构 子模块的创建 然后我们再新建子模块右键根目录选择new - module 这里的new module创建的照样是一个maven工程不过此时它存在一个父模块也就是我们上面的father_project 创建以后的目录这样的 父子模块的意义 大家可能会问了父子模块的意义是什么为什么不能再创建一个项目而是使用父子模块 在 IntelliJ IDEA 中父子模块的概念与 Maven 或 Gradle 等构建工具中的模块结构概念相关联。 父子模块的意义 模块化管理父子模块的设计有助于将项目分解为更小、更易管理的模块单元听起来像微服务划分的方式。这种模块化管理使得项目更易于开发和维护有助于团队协作和复用。 依赖管理父子模块可以有依赖关系子模块可以依赖于父模块同时子模块也可以互相依赖。 共享配置父模块中定义的配置可以被子模块继承和覆盖比如版本管理、插件配置等。 独立构建子模块可以独立构建、测试和运行同时也可以依赖于其他子模块最终打包成整个项目。 将注册服务引入到父子模块中 按照上述的方式将之前的注册项目引入到父子模块结构的项目中得到如下的项目 这里父项目的pom文件也贴一下 ?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdorg.example/groupIdartifactIdSpringCloudAlibabaDemo/artifactIdversion1.0-SNAPSHOT/versionpackagingpom/packaging!-- 子模块的信息--modulesmodulenacos-project-registrant/module/modulesproperties!-- 项目的源代码的Java版本--maven.compiler.source11/maven.compiler.source!-- 项目编译生成的字节码的目标Java版本--maven.compiler.target11/maven.compiler.target!-- 项目的源代码文件的字符编码--project.build.sourceEncodingUTF-8/project.build.sourceEncoding!-- spring boot的版本--spring-boot.version2.7.4/spring-boot.version!-- spring cloud alibaba的版本--spring-cloud-alibab.version2021.0.5.0/spring-cloud-alibab.version!-- nacos-discovery的版本--spring-nacos.version2021.0.5.0/spring-nacos.version!-- spring cloud的版本--spring-cloud.version2021.0.5/spring-cloud.version/properties!-- 需要集中管理的依赖 子模块可以通过父模块引入而不需要自己导入--dependencyManagementdependencies!-- Nacos服务注册和发现的集成--dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactIdversion${spring-nacos.version}/versiontypepom/typescopeimport/scope/dependency!-- 引入spring boot --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion${spring-boot.version}/versiontypepom/typescopeimport/scope/dependency!-- 引入spring cloud alibaba --dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-alibaba-dependencies/artifactIdversion${spring-cloud-alibab.version}/versiontypepom/typescopeimport/scope/dependency!-- 部分spring cloud功能使用时候需要引入这个--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversion${spring-cloud.version}/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagement!-- 项目的构建配置包括编译、打包、测试、部署等方面--buildplugins!-- spring-boot中用于支持maven打包的插件--plugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project子项目的目录结构与之前的相似 这里子模块的pom文件也照样贴一下除了依赖的引入方式变成从父模块引入别的都一样 ?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionpackagingwar/packagingparentgroupIdorg.example/groupIdartifactIdSpringCloudAlibabaDemo/artifactIdversion1.0-SNAPSHOT/version/parentartifactIdnacos-project-registrant/artifactIdpropertiesmaven.compiler.source11/maven.compiler.sourcemaven.compiler.target11/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/properties!-- 由于父模块中已经引入了所以这里不需要配置版本项目会自动以父模块为准--dependencies !-- spring boot的引入--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- Nacos服务注册和发现的集成--dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependency/dependencies!-- 项目的构建配置包括编译、打包、测试、部署等方面--buildresources!-- 构建项目过程中被复制到输出目录中以便它们可以在应用程序运行时访问--resourcedirectorysrc/main/java/directoryincludes!-- 项目内mybatis的dao配置文件--include**/*.xml/include/includes/resourceresourcedirectorysrc/main/resources/directoryincludes!-- 资源文件--include**/*.*/include/includes/resource/resourcesplugins!-- spring-boot中用于支持maven打包的插件--plugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins!-- 打包出来的名称 一般是项目的缩写外加版本号 --finalNamenpr/finalName/build/project我们再验证一下是否引入成功启动注册服务 如果nacos中对应服务存在则表示成功 如果运行失败可能是nacos没启动导致的 创建子模块用于发现服务和调用 接下来我们再创建一个子模块用于发现注册的服务以及调用该服务的接口 供调用的服务接口 这里我们先在注册服务中创建一些接口供订阅者调用 import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController;RestController public class NacosTest {GetMapping(/echo/{string})public String echo(PathVariable String string) {return 成功调用注册服务的接口代码为 string;}GetMapping(/test)public String test() {return 调用服务的测试;} }在启动类上添加EnableDiscoveryClient注解启用Nacos客户端的发现 创建调用子模块 按照上面的方式创建一个名为nacos-discoverry-call子模块并将其改为spring boot项目如下图所示 pom文件内容为 ?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionpackagingwar/packagingparentgroupIdorg.example/groupIdartifactIdSpringCloudAlibabaDemo/artifactIdversion1.0-SNAPSHOT/version/parentartifactIdnacos-discoverry-call/artifactIdpropertiesmaven.compiler.source11/maven.compiler.sourcemaven.compiler.target11/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/properties!-- 由于父模块中已经引入了所以这里不需要配置版本项目会自动以父模块为准--dependencies!-- spring boot的引入--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- Nacos服务注册和发现的集成--dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependency!-- openfeign提供服务的远程调用--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactId/dependency!-- open-feign在默认情况下依赖于负载均衡 新版本中已经不再强制依赖ribbon 而是推荐loadbalancer--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-loadbalancer/artifactId/dependency/dependencies!-- 项目的构建配置包括编译、打包、测试、部署等方面--buildresources!-- 构建项目过程中被复制到输出目录中以便它们可以在应用程序运行时访问--resourcedirectorysrc/main/java/directoryincludes!-- 项目内mybatis的dao配置文件--include**/*.xml/include/includes/resourceresourcedirectorysrc/main/resources/directoryincludes!-- 资源文件--include**/*.*/include/includes/resource/resourcesplugins!-- spring-boot中用于支持maven打包的插件--plugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins!-- 打包出来的名称 一般是项目的缩写外加版本号 --finalNamendc/finalName/build/projectapplication.yml文件除了application.name以及port其他无变化 server:port: 9001tomcat:uri-encoding: UTF-8spring:application:name: nacos-callcloud:nacos:discovery:server-addr: 192.168.2.102:8848 # 控制应用程序在启动时对配置中心的连接行为 如果没有连接到客户端则会立即启动失败fail-fast: true启动类CallApplication中需要添加以下两个注解启用服务的发现和调用 调用类相关的就以下两个 FeignInterface调用的接口定义了某个服务调用的接口 import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; //feign接口提供服务的调用 name为所调用服务的名称 FeignClient(namenacos-register) public interface FeignInterface {GetMapping(/echo/{str})String echo(PathVariable(str) String str); }NacosTest为测试的控制器 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate;RestController public class NacosTest {private final static String SERVICE_ADDR http://192.168.2.102:9000;//Fegin接口提供的调用方式AutowiredFeignInterface feignInterface;//直接使用Rest风格模板来实现调用 不推荐private RestTemplate template new RestTemplate();GetMapping(/echo/{string})public String echo(PathVariable String string) {//直接使用Rest风格模板来实现调用return template.getForObject(SERVICE_ADDR /echo/ string , String.class);}GetMapping(/test)public String test() {//直接使用Rest风格模板来实现调用return template.getForObject(SERVICE_ADDR/test , String.class);}GetMapping(/iecho/{string})public String iecho(PathVariable String string) {//Fegin接口提供的调用方式return feignInterface.echo(string);}}测试 启动两个子模块进入Nacos控制台 这里可以发现两个已经注册的服务说明没问题然后我们调用一下检查是否调用服务nacos-call能否调用到注册服务nacos-register中的接口 地址栏输入http://localhost:9001/iecho/125 调用注册服务中的echo接口 有以下显示即为成功 此时点开订阅者列表可看到nacos-call服务 还可以输入http://localhost:9001/test测试/test接口 一些小问题 订阅者有时候不显示应用名称而是显示为unknow 自己给环境设置一下即可 ProjectNameConfig代码为 import com.alibaba.nacos.common.utils.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment;//用于设置订阅者一栏中的应用名称 Configuration public class ProjectNameConfig implements EnvironmentAware {Value(${spring.application.name})private String applicationName;Overridepublic void setEnvironment(Environment environment) {if(StringUtils.isBlank(System.getProperty(project.name))){System.setProperty(project.name,applicationName);}} }
http://www.yutouwan.com/news/467189/

相关文章:

  • 用什么软件写网站太原市一页网络科技有限公司
  • 五 网站开发总体进度安排职业技能培训学校
  • wordpress下载站批量宝山做网站公司
  • 南宁网站seo推广公司制作微信商城网站开发
  • 浪网站制作手写代码网站
  • 福建省住房城乡和城乡建设厅网站连接品硕网线做怎么弹网站
  • 网站开发人员的要求聂教练做0网站
  • 大型门户网站建设美丽雅布设计作品
  • 江苏省建设局网站证件查询小程序免费制作平台360
  • 网页设计作品论文seo网站运营
  • 有关网站开发的文献大连网站怎么推广
  • 建立网站编程福州建站模板搭建
  • 网站建设深圳官网孝感网站建设公司
  • 商务网站建设工程师公司简介模板范本
  • 网站排名如何做福州做网站销售公司
  • 浙江省人才网官方网站建设厅招聘遵义市 网站建设
  • 公司网站建设需求分析网站建设服务条款
  • 烟台网站建设多少钱wordpress字体插件
  • 室内装修网站html源码 企业网站建设整体设计流程
  • 电脑网站素材广州企业网站建设方案
  • wordpress评论生成中卫网站推广优化公司
  • primefaces做网站汉阳做网站
  • 软件公司网站建设做网站哪些公司
  • 网站文案标准格式怎么做网站挣钱
  • 常宁网站开发兰州有制作网站
  • vps 网站异常沈阳做网站有名公司
  • 厦门建设工程招标中心的网站上海市建设合同信息表网站
  • 怎么做网站盗号泗县网站建设
  • 海淀教育人才网站wordpress排行榜模板
  • 白酒营销网站哪家网络么司做网站好