建筑公司企业网站,橘子seo工具,哪个公司的网站做得好,应用公园下载目录
一、SpringCloudAlibaba 项目完整搭建
1.1、初始化项目
1.1.1、创建工程
1.1.2、配置父工程的 pom.xml
1.1.3、创建子模块
1.2、user 微服务
1.2.1、配置 pom.xml
1.2.2、创建 application.yml 配置文件
1.2.3、创建启动类
1.2.4、测试
1.3、product 微服务
1…目录
一、SpringCloudAlibaba 项目完整搭建
1.1、初始化项目
1.1.1、创建工程
1.1.2、配置父工程的 pom.xml
1.1.3、创建子模块
1.2、user 微服务
1.2.1、配置 pom.xml
1.2.2、创建 application.yml 配置文件
1.2.3、创建启动类
1.2.4、测试
1.3、product 微服务
1.3.1、配置 pom.xml
1.3.2、创建 application.yml 配置文件
1.3.3、创建启动类
1.3.4、测试
1.4、引入 OpenFeign进行远程调用
1.4.1、引入依赖
1.4.2、实现远程调用
1.5、user 微服务集成 Sentinel
1.5.1、引入 sentinel 依赖
1.5.2、在 application.yml 中配置 sentinel
1.5.3、测试
1.6、网关微服务
1.6.1、配置 pom.xml
1.6.2、创建 application.yml 并配置网关
1.6.3、创建启动类
1.6.4、测试
1.7、nacos 实现配置中心
1.7.1、建议
1.7.2、引入 nacos 配置中心依赖
1.7.3、在 nacos 上创建配置 一、SpringCloudAlibaba 项目完整搭建 1.1、初始化项目
1.1.1、创建工程
a先创建一个空项目名字自定义. b在刚刚的空项目下创建整个项目的父模块 c父工程下的 src 可以删了没用 d最后直接用 IDEA 打开空项目下的父工程否则 Java 文件不生效 1.1.2、配置父工程的 pom.xml
a引入依赖如果没有语法提示可以这样添加索引 b配置依赖如下
?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/groupIdartifactIdspringcloud_alibaba_parent/artifactIdversion1.0-SNAPSHOT/version!--配置版本信息--propertiesjava.version8/java.versionspring-cloud.versionHoxton.SR8/spring-cloud.versionspring-cloud-alibaba.version2.2.5.RELEASE/spring-cloud-alibaba.version/properties!--parent--parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.3.9.RELEASE/version/parentdependencies!--基本上每个项目都需要日志--dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependency/dependencies!--管理依赖规范--dependencyManagementdependencies!-- springCloud --dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversion${spring-cloud.version}/versiontypepom/typescopeimport/scope/dependency!-- spring-cloud-alibaba --dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-alibaba-dependencies/artifactIdversion${spring-cloud-alibaba.version}/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagement/project PsSpringCloud Alibaba 、SpringCloud、SpringBoot 各版本对应说明 如下 https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E 1.1.3、创建子模块
在父工程下创建 4 个 module
user用户微服务案例.product商品微服务案例.gateway网关微服务.model模组微服务用来管理实体类的. 1.2、user 微服务
1.2.1、配置 pom.xml
用户微服务所需依赖如下按需引入 dependencies!--web--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!--nacos-discovery--dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependency!--openFeign--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactId/dependency!--sentinel--dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-sentinel/artifactId/dependency/dependenciesPs不要一开始就引入这么多按需引入尤其是 nacos config配置 依赖如果引入不使用就会报错 1.2.2、创建 application.yml 配置文件
配置端口号以及 nacos 地址
server:port: 8090spring:application:name: usercloud:nacos:server-addr: localhost:88901.2.3、创建启动类
SpringBootApplication
EnableDiscoveryClient // nacos 服务发现(可以省略)
public class UserApplication {public static void main(String[] args) {SpringApplication.run(UserApplication.class, args);}}启动 user 微服务就可以看到 nacos 中存在该服务了 1.2.4、测试
创建 UserController如下
Slf4j
RestController
RequestMapping
public class UserController {Value(${server.port})private int port;GetMapping(/user)public String user() {log.info(user ok! port{}, port);return user ok! port port;}}postman 测试结果如下 1.3、product 微服务
1.3.1、配置 pom.xml
用户微服务所需依赖如下按需引入 dependencies!--web--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!--nacos-discovery--dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependency!--openFeign--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactId/dependency!--sentinel--dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-sentinel/artifactId/dependency/dependenciesPs不要一开始就引入这么多按需引入尤其是 nacos config配置 依赖如果引入不使用就会报错 1.3.2、创建 application.yml 配置文件
配置端口号以及 nacos 地址
server:port: 8091spring:application:name: productcloud:nacos:server-addr: localhost:88901.3.3、创建启动类
SpringBootApplication
EnableDiscoveryClient //可省略
public class ProductApplication {public static void main(String[] args) {SpringApplication.run(ProductApplication.class, args);}}启动 user 微服务就可以看到 nacos 中存在该服务了 1.3.4、测试
创建 UserController如下
Slf4j
RestController
RequestMapping
public class UserController {Value(${server.port})private int port;GetMapping(/user)public String user() {log.info(user ok! port{}, port);return user ok! port port;}}postman 测试结果如下 1.4、引入 OpenFeign进行远程调用
1.4.1、引入依赖
在 user 微服务中引入 openfeign 依赖这里只是举例实际开发中按需引入. !--openFeign--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactId/dependency 1.4.2、实现远程调用
在 user 微服务中创建一个 product 微服务 feign 接口
FeignClient(product)
public interface ProductClient {GetMapping(/product)String product();}在启动类中开启 feign 客户端
SpringBootApplication
EnableDiscoveryClient // nacos 服务发现(可以省略)
EnableFeignClients // 开启 openfeign 远程调用
public class UserApplication {public static void main(String[] args) {SpringApplication.run(UserApplication.class, args);}}在 UserController 中进行远程调用. GetMapping(/user)public String user() {log.info(user ok! port{}, port);//远程调用 productString result productClient.product();log.info(远程调用 result: {}, result);return user ok! port port;}执行结果如下 1.5、user 微服务集成 Sentinel
1.5.1、引入 sentinel 依赖
在 user 微服务中引入 sentinel 依赖 !--sentinel--dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-sentinel/artifactId/dependency1.5.2、在 application.yml 中配置 sentinel
server:port: 8090spring:application:name: usercloud:nacos:server-addr: localhost:8890sentinel:eager: true # 立即触发 sentineltransport:dashboard: localhost:88911.5.3、测试
a打开 sentinel 控制台 b设置流控规则为 1秒 / 3次点击. c测试后限流生效 1.6、网关微服务
1.6.1、配置 pom.xml dependencies!--gateway--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-gateway/artifactId/dependency!--nacos--dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependency/dependencies1.6.2、创建 application.yml 并配置网关
server:port: 18080spring:application:name: gatewaycloud:nacos:server-addr: localhost:8890gateway:routes:- id: users_router# uri: http://localhost:9090 无负载均衡uri: lb://user # lb 表示负载均衡predicates:- Path/user- id: products_router# uri: http://localhost:9091uri: lb://productpredicates:- Path/product另外如果配置跨域如下
server:port: 18080spring:application:name: gatewaycloud:nacos:server-addr: localhost:8890gateway:routes:- id: users_router# uri: http://localhost:9090 无负载均衡uri: lb://user # lb 表示负载均衡predicates:- Path/user- id: products_router# uri: http://localhost:9091uri: lb://productpredicates:- Path/productglobalcors: # 全局的跨域处理add-to-simple-url-handler-mapping: true # 解决options请求被拦截问题corsConfigurations:[/**]:allowedOrigins: # 允许哪些网站的跨域请求- http://localhost:8090- http://www.cyk.comallowedMethods: # 允许的跨域ajax/axios的请求方式- GET- POST- DELETE- PUT- OPTIONSallowedHeaders: * # 允许在请求中携带的头信息allowCredentials: true # 是否允许携带cookiemaxAge: 360000 # 这次跨域检测的有效期为例减少性能损耗在有效时间内浏览器将不在发起询问直接放行通过1.6.3、创建启动类
SpringBootApplication
EnableDiscoveryClient //(可省略)
public class GatewayApplication {public static void main(String[] args) {SpringApplication.run(GatewayApplication.class, args);}}1.6.4、测试
通过网关访问 user 微服务和 product 微服务 1.7、nacos 实现配置中心
1.7.1、建议
一般来说项目初期开发只用三个组件nacos注册中心、gateway网关、openFeign声明式远程调用。
值得注意的是前期项目开发完成之前也不要使用 nacos 配置中心原因如下
因为一旦需要改什么配置还需要打开 nacos 修改容易分心建议就先用项目中 application.yml 即可引入依赖但不配置会报错. 1.7.2、引入 nacos 配置中心依赖
这里以 user 微服务为例引入 nacos 配置中心依赖. !--nacos config--dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-config/artifactId/dependency1.7.3、在 nacos 上创建配置
a在 nacos 上创建一个命名空间 b在配置管理中选择刚创建的命名空间 demo1 c创建一个新的配置文件接着就可以把 user 微服务 yml 中配置所有信息都放到 nacos 上进行统一管理随后点击发布 Ps配置内容不要有注释 天坑 d创建 bootstrap.yml 配置文件用来拉去 nacos 上的配置文件.
# 远端配置中心是谁
spring:cloud:nacos:server-addr: locahost:8890config:# 使用 nacos 中的哪个 namespacenamespace: 0e48f075-ad88-4700-a422-d315a81f9ced# 使用 nacos 中的哪个组group: user# 使用 nacos 中的哪个 dataId 1.name文件后缀 2.文件名env后缀name: user-prodfile-extension: ymlPs原先的 application.yml 就可以删除了.