《分布式微服务电商》专题(八)-电商项目整合SpringCloud Alibaba

1.SpringCloud Alibaba - Nacos [作为配置中心]

1)修改“goshop-coupon”模块

添加pom依赖:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

创建bootstrap.properties文件,该配置文件会优先于“application.yml”加载。

spring.application.name=gulimall-coupon
spring.cloud.nacos.config.server-addr=192.168.137.14:8848

2)传统方式

为了详细说明config的使用方法,先来看原始的方式

创建“application.properties”配置文件,添加如下配置内容:

coupon.user.name="zhangsan"
coupon.user.age=30

修改“CouponController”文件,添加如下内容:

@Value("${coupon.user.name}")
private String name;
@Value("${coupon.user.age}")
private Integer age;

@RequestMapping("/test")
public R getConfigInfo(){
   return R.ok().put("name",name).put("age",age);
}

启动“goshop-coupon”服务:

访问:http://localhost:8081/coupon/coupon/test
在这里插入图片描述
这样做存在的一个问题,如果频繁的修改application.properties,在需要频繁重新打包部署。下面我们将采用Nacos的配置中心来解决这个问题。

3)nacos config

1、在Nacos注册中心中,点击“配置列表”,添加配置规则:
在这里插入图片描述
在这里插入图片描述
DataID:goshop-coupon

配置格式:properties

文件的命名规则为: s p r i n g . a p p l i c a t i o n . n a m e − {spring.application.name}- spring.application.name{spring.profiles.active}.${spring.cloud.nacos.config.file-extension}

${spring.application.name}:为微服务名

${spring.profiles.active}:指明是哪种环境下的配置,如dev、test或info

${spring.cloud.nacos.config.file-extension}:配置文件的扩展名,可以为properties、yml等

2、查看配置:

在这里插入图片描述

3、修改“CouponController”类,添加“@RefreshScope”注解

@RestController
@RequestMapping("coupon/coupon")
@RefreshScope
public class CouponController {

这样都会动态的从配置中心读取配置.

4、访问:http://localhost:8081/coupon/coupon/test

在这里插入图片描述

能够看到读取到了nacos 中的最新的配置信息,并且在指明了相同的配置信息时,配置中心中设置的值优先于本地配置。

2.SpringCloud Alibaba - Nacos [作为配置中心进阶]

2.1、核心概念

命名空间

用于进行租户粒度的配置隔离。不同的命名空间下,可以存在相同的 GroupDatalD 的配置。Namespace 的常用场景之一是不同环境的配置的区分隔离,例如开发测试环境和生产环境的资源(如配置、服务)隔离等。

配置集

一组相关或者不相关的配置项的集合称为配置集。在系统中,一个配置文件通常就是一个配置集,包含了系统各个方面的配置。例如,一个配置集可能包含了数据源、线程池、日志级别等配置项。

配置集ID:

Nacos 中的某个配置集的 ID,配置集 ID 是组织划分配置的维度之一,Data ID 通常用于组织划分系统的配置集,一个系统或者应用可以包含多个配置集,一个系统应用可以包含多个配置集,每个配置集都可以被一个有意义的名称标识,Data ID 通常采用类 Java 包 如 ( com.taobao.tc.refund.log.level ) 的命名规则保证全局唯一性,此命名规则非强制

配置分组:

Nacos 中的一组配置集,是组织配置的维度之一,通过一个有意义的字符串,(如 Buy 或 Trade ) 对配置集进行分组,从而区分 Data ID 相同的配置集,当您在 Nacos 上创建一个配置时,如果未填写配置分组的名称,则配置分组的名称默认采用,DEFAULT_GROUP 配置分组的常见场景,不同的应用或组件采用了相同的配置类型,如 database_url 配置和 MQ_topic 配置

2.2、Nacos支持三种配置加载方方案

Nacos支持“Namespace+group+data ID”的配置解决方案。

详情见:https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc

2.2.1 Namespace方案

通过命名空间实现环境区分

下面是配置实例:

1、创建命名空间:

“命名空间”—>“创建命名空间”:
在这里插入图片描述

创建三个命名空间,分别为devtestprop

2、回到配置列表中,能够看到所创建的三个命名空间

在这里插入图片描述

下面我们需要在dev命名空间下,创建“goshop-coupon.properties”配置规则:

在这里插入图片描述

3、访问:http://localhost:8001/coupon/coupon/test
在这里插入图片描述

并没有使用我们在dev命名空间下所配置的规则,而是使用的是public命名空间下所配置的规则,这是怎么回事呢?

查看“goshop-coupon”服务的启动日志:

G:\softDevelopment\JDK8\bin\java.exe -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:G:\softInstall\IDEA20190903\IntelliJ IDEA 2019.3.2\lib\idea_rt.jar=9574:G:\softInstall\IDEA20190903\IntelliJ IDEA 2019.3.2\bin" -Dfile.encoding=UTF-8 -classpath G:\softDevelopment\JDK8\jre\lib\charsets.jar;G:\softDevelopment\JDK8\jre\lib\deploy.jar;G:\softDevelopment\JDK8\jre\lib\ext\access-bridge-64.jar;G:\softDevelopment\JDK8\jre\lib\ext\cldrdata.jar;G:\softDevelopment\JDK8\jre\lib\ext\dnsns.jar;G:\softDevelopment\JDK8\jre\lib\ext\jaccess.jar;G:\softDevelopment\JDK8\jre\lib\ext\jfxrt.jar;G:\softDevelopment\JDK8\jre\lib\ext\localedata.jar;G:\softDevelopment\JDK8\jre\lib\ext\nashorn.jar;G:\softDevelopment\JDK8\jre\lib\ext\sunec.jar;G:\softDevelopment\JDK8\jre\lib\ext\sunjce_provider.jar;G:\softDevelopment\JDK8\jre\lib\ext\sunmscapi.jar;G:\softDevelopment\JDK8\jre\lib\ext\sunpkcs11.jar;G:\softDevelopment\JDK8\jre\lib\ext\zipfs.jar;G:\softDevelopment\JDK8\jre\lib\javaws.jar;G:\softDevelopment\JDK8\jre\lib\jce.jar;G:\softDevelopment\JDK8\jre\lib\jfr.jar;G:\softDevelopment\JDK8\jre\lib\jfxswt.jar;G:\softDevelopment\JDK8\jre\lib\jsse.jar;G:\softDevelopment\JDK8\jre\lib\management-agent.jar;G:\softDevelopment\JDK8\jre\lib\plugin.jar;G:\softDevelopment\JDK8\jre\lib\resources.jar;G:\softDevelopment\JDK8\jre\lib\rt.jar;E:\Server\goshop\goshop-coupon\target\classes;E:\Server\goshop\goshop-common\target\classes;D:\maven20210104\repository\com\baomidou\mybatis-plus-boot-starter\3.2.0\mybatis-plus-boot-starter-3.2.0.jar;D:\maven20210104\repository\com\baomidou\mybatis-plus\3.2.0\mybatis-plus-3.2.0.jar;D:\maven20210104\repository\com\baomidou\mybatis-plus-extension\3.2.0\mybatis-plus-extension-3.2.0.jar;D:\maven20210104\repository\com\baomidou\mybatis-plus-core\3.2.0\mybatis-plus-core-3.2.0.jar;D:\maven20210104\repository\com\baomidou\mybatis-plus-annotation\3.2.0\mybatis-plus-annotation-3.2.0.jar;D:\maven20210104\repository\com\github\jsqlparser\jsqlparser\2.1\jsqlparser-2.1.jar;D:\maven20210104\repository\org\mybatis\mybatis\3.5.2\mybatis-3.5.2.jar;D:\maven20210104\repository\org\mybatis\mybatis-spring\2.0.2\mybatis-spring-2.0.2.jar;D:\maven20210104\repository\org\springframework\boot\spring-boot-autoconfigure\2.1.8.RELEASE\spring-boot-autoconfigure-2.1.8.RELEASE.jar;D:\maven20210104\repository\org\springframework\boot\spring-boot-starter-jdbc\2.1.8.RELEASE\spring-boot-starter-jdbc-2.1.8.RELEASE.jar;D:\maven20210104\repository\com\zaxxer\HikariCP\3.2.0\HikariCP-3.2.0.jar;D:\maven20210104\repository\org\springframework\spring-jdbc\5.1.9.RELEASE\spring-jdbc-5.1.9.RELEASE.jar;D:\maven20210104\repository\org\springframework\spring-tx\5.1.9.RELEASE\spring-tx-5.1.9.RELEASE.jar;D:\maven20210104\repository\org\projectlombok\lombok\1.18.8\lombok-1.18.8.jar;D:\maven20210104\repository\org\apache\httpcomponents\httpcore\4.4.12\httpcore-4.4.12.jar;D:\maven20210104\repository\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;D:\maven20210104\repository\mysql\mysql-connector-java\8.0.17\mysql-connector-java-8.0.17.jar;D:\maven20210104\repository\com\alibaba\cloud\spring-cloud-starter-alibaba-nacos-discovery\2.1.0.RELEASE\spring-cloud-starter-alibaba-nacos-discovery-2.1.0.RELEASE.jar;D:\maven20210104\repository\com\alibaba\cloud\spring-cloud-alibaba-nacos-discovery\2.1.0.RELEASE\spring-cloud-alibaba-nacos-discovery-2.1.0.RELEASE.jar;D:\maven20210104\repository\com\alibaba\nacos\nacos-client\1.1.1\nacos-client-1.1.1.jar;D:\maven20210104\repository\com\alibaba\nacos\nacos-common\1.1.1\nacos-common-1.1.1.jar;D:\maven20210104\repository\org\apache\commons\commons-lang3\3.8.1\commons-lang3-3.8.1.jar;D:\maven20210104\repository\com\alibaba\nacos\nacos-api\1.1.1\nacos-api-1.1.1.jar;D:\maven20210104\repository\com\alibaba\fastjson\1.2.47\fastjson-1.2.47.jar;D:\maven20210104\repository\commons-codec\commons-codec\1.11\commons-codec-1.11.jar;D:\maven20210104\repository\io\prometheus\simpleclient\0.5.0\simpleclient-0.5.0.jar;D:\maven20210104\repository\org\springframework\cloud\spring-cloud-starter-netflix-ribbon\2.1.3.RELEASE\spring-cloud-starter-netflix-ribbon-2.1.3.RELEASE.jar;D:\maven20210104\repository\org\springframework\cloud\spring-cloud-starter-netflix-archaius\2.1.3.RELEASE\spring-cloud-starter-netflix-archaius-2.1.3.RELEASE.jar;D:\maven20210104\repository\com\netflix\ribbon\ribbon\2.3.0\ribbon-2.3.0.jar;D:\maven20210104\repository\com\netflix\ribbon\ribbon-transport\2.3.0\ribbon-transport-2.3.0.jar;D:\maven20210104\repository\io\reactivex\rxnetty-contexts\0.4.9\rxnetty-contexts-0.4.9.jar;D:\maven20210104\repository\io\reactivex\rxnetty-servo\0.4.9\rxnetty-servo-0.4.9.jar;D:\maven20210104\repository\javax\inject\javax.inject\1\javax.inject-1.jar;D:\maven20210104\repository\io\reactivex\rxnetty\0.4.9\rxnetty-0.4.9.jar;D:\maven20210104\repository\com\netflix\ribbon\ribbon-core\2.3.0\ribbon-core-2.3.0.jar;D:\maven20210104\repository\com\netflix\ribbon\ribbon-httpclient\2.3.0\ribbon-httpclient-2.3.0.jar;D:\maven20210104\repository\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar;D:\maven20210104\repository\org\apache\httpcomponents\httpclient\4.5.9\httpclient-4.5.9.jar;D:\maven20210104\repository\com\sun\jersey\jersey-client\1.19.1\jersey-client-1.19.1.jar;D:\maven20210104\repository\com\sun\jersey\jersey-core\1.19.1\jersey-core-1.19.1.jar;D:\maven20210104\repository\javax\ws\rs\jsr311-api\1.1.1\jsr311-api-1.1.1.jar;D:\maven20210104\repository\com\sun\jersey\contribs\jersey-apache-client4\1.19.1\jersey-apache-client4-1.19.1.jar;D:\maven20210104\repository\com\netflix\servo\servo-core\0.12.21\servo-core-0.12.21.jar;D:\maven20210104\repository\com\netflix\netflix-commons\netflix-commons-util\0.3.0\netflix-commons-util-0.3.0.jar;D:\maven20210104\repository\com\netflix\ribbon\ribbon-loadbalancer\2.3.0\ribbon-loadbalancer-2.3.0.jar;D:\maven20210104\repository\com\netflix\netflix-commons\netflix-statistics\0.1.1\netflix-statistics-0.1.1.jar;D:\maven20210104\repository\com\alibaba\cloud\spring-cloud-starter-alibaba-nacos-config\2.1.0.RELEASE\spring-cloud-starter-alibaba-nacos-config-2.1.0.RELEASE.jar;D:\maven20210104\repository\com\alibaba\cloud\spring-cloud-alibaba-nacos-config\2.1.0.RELEASE\spring-cloud-alibaba-nacos-config-2.1.0.RELEASE.jar;D:\maven20210104\repository\org\springframework\boot\spring-boot-starter-web\2.1.8.RELEASE\spring-boot-starter-web-2.1.8.RELEASE.jar;D:\maven20210104\repository\org\springframework\boot\spring-boot-starter\2.1.8.RELEASE\spring-boot-starter-2.1.8.RELEASE.jar;D:\maven20210104\repository\org\springframework\boot\spring-boot\2.1.8.RELEASE\spring-boot-2.1.8.RELEASE.jar;D:\maven20210104\repository\org\springframework\boot\spring-boot-starter-logging\2.1.8.RELEASE\spring-boot-starter-logging-2.1.8.RELEASE.jar;D:\maven20210104\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\maven20210104\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\maven20210104\repository\org\apache\logging\log4j\log4j-to-slf4j\2.11.2\log4j-to-slf4j-2.11.2.jar;D:\maven20210104\repository\org\apache\logging\log4j\log4j-api\2.11.2\log4j-api-2.11.2.jar;D:\maven20210104\repository\org\slf4j\jul-to-slf4j\1.7.28\jul-to-slf4j-1.7.28.jar;D:\maven20210104\repository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;D:\maven20210104\repository\org\yaml\snakeyaml\1.23\snakeyaml-1.23.jar;D:\maven20210104\repository\org\springframework\boot\spring-boot-starter-json\2.1.8.RELEASE\spring-boot-starter-json-2.1.8.RELEASE.jar;D:\maven20210104\repository\com\fasterxml\jackson\core\jackson-databind\2.9.9.3\jackson-databind-2.9.9.3.jar;D:\maven20210104\repository\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;D:\maven20210104\repository\com\fasterxml\jackson\core\jackson-core\2.9.9\jackson-core-2.9.9.jar;D:\maven20210104\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.9\jackson-datatype-jdk8-2.9.9.jar;D:\maven20210104\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.9\jackson-datatype-jsr310-2.9.9.jar;D:\maven20210104\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.9\jackson-module-parameter-names-2.9.9.jar;D:\maven20210104\repository\org\springframework\boot\spring-boot-starter-tomcat\2.1.8.RELEASE\spring-boot-starter-tomcat-2.1.8.RELEASE.jar;D:\maven20210104\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.24\tomcat-embed-core-9.0.24.jar;D:\maven20210104\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.24\tomcat-embed-el-9.0.24.jar;D:\maven20210104\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.24\tomcat-embed-websocket-9.0.24.jar;D:\maven20210104\repository\org\hibernate\validator\hibernate-validator\6.0.17.Final\hibernate-validator-6.0.17.Final.jar;D:\maven20210104\repository\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;D:\maven20210104\repository\org\jboss\logging\jboss-logging\3.3.3.Final\jboss-logging-3.3.3.Final.jar;D:\maven20210104\repository\com\fasterxml\classmate\1.4.0\classmate-1.4.0.jar;D:\maven20210104\repository\org\springframework\spring-web\5.1.9.RELEASE\spring-web-5.1.9.RELEASE.jar;D:\maven20210104\repository\org\springframework\spring-beans\5.1.9.RELEASE\spring-beans-5.1.9.RELEASE.jar;D:\maven20210104\repository\org\springframework\spring-webmvc\5.1.9.RELEASE\spring-webmvc-5.1.9.RELEASE.jar;D:\maven20210104\repository\org\springframework\spring-aop\5.1.9.RELEASE\spring-aop-5.1.9.RELEASE.jar;D:\maven20210104\repository\org\springframework\spring-context\5.1.9.RELEASE\spring-context-5.1.9.RELEASE.jar;D:\maven20210104\repository\org\springframework\spring-expression\5.1.9.RELEASE\spring-expression-5.1.9.RELEASE.jar;D:\maven20210104\repository\org\springframework\cloud\spring-cloud-starter-openfeign\2.1.3.RELEASE\spring-cloud-starter-openfeign-2.1.3.RELEASE.jar;D:\maven20210104\repository\org\springframework\cloud\spring-cloud-starter\2.1.3.RELEASE\spring-cloud-starter-2.1.3.RELEASE.jar;D:\maven20210104\repository\org\springframework\cloud\spring-cloud-context\2.1.3.RELEASE\spring-cloud-context-2.1.3.RELEASE.jar;D:\maven20210104\repository\org\springframework\security\spring-security-rsa\1.0.7.RELEASE\spring-security-rsa-1.0.7.RELEASE.jar;D:\maven20210104\repository\org\bouncycastle\bcpkix-jdk15on\1.60\bcpkix-jdk15on-1.60.jar;D:\maven20210104\repository\org\bouncycastle\bcprov-jdk15on\1.60\bcprov-jdk15on-1.60.jar;D:\maven20210104\repository\org\springframework\cloud\spring-cloud-openfeign-core\2.1.3.RELEASE\spring-cloud-openfeign-core-2.1.3.RELEASE.jar;D:\maven20210104\repository\org\springframework\cloud\spring-cloud-netflix-ribbon\2.1.3.RELEASE\spring-cloud-netflix-ribbon-2.1.3.RELEASE.jar;D:\maven20210104\repository\org\springframework\cloud\spring-cloud-netflix-archaius\2.1.3.RELEASE\spring-cloud-netflix-archaius-2.1.3.RELEASE.jar;D:\maven20210104\repository\org\springframework\boot\spring-boot-starter-aop\2.1.8.RELEASE\spring-boot-starter-aop-2.1.8.RELEASE.jar;D:\maven20210104\repository\org\aspectj\aspectjweaver\1.9.4\aspectjweaver-1.9.4.jar;D:\maven20210104\repository\io\github\openfeign\form\feign-form-spring\3.8.0\feign-form-spring-3.8.0.jar;D:\maven20210104\repository\io\github\openfeign\form\feign-form\3.8.0\feign-form-3.8.0.jar;D:\maven20210104\repository\commons-fileupload\commons-fileupload\1.4\commons-fileupload-1.4.jar;D:\maven20210104\repository\commons-io\commons-io\2.2\commons-io-2.2.jar;D:\maven20210104\repository\org\springframework\cloud\spring-cloud-commons\2.1.3.RELEASE\spring-cloud-commons-2.1.3.RELEASE.jar;D:\maven20210104\repository\org\springframework\security\spring-security-crypto\5.1.6.RELEASE\spring-security-crypto-5.1.6.RELEASE.jar;D:\maven20210104\repository\io\github\openfeign\feign-core\10.2.3\feign-core-10.2.3.jar;D:\maven20210104\repository\io\github\openfeign\feign-slf4j\10.2.3\feign-slf4j-10.2.3.jar;D:\maven20210104\repository\org\slf4j\slf4j-api\1.7.28\slf4j-api-1.7.28.jar;D:\maven20210104\repository\io\github\openfeign\feign-hystrix\10.2.3\feign-hystrix-10.2.3.jar;D:\maven20210104\repository\com\netflix\archaius\archaius-core\0.7.6\archaius-core-0.7.6.jar;D:\maven20210104\repository\com\google\code\findbugs\jsr305\3.0.1\jsr305-3.0.1.jar;D:\maven20210104\repository\commons-configuration\commons-configuration\1.8\commons-configuration-1.8.jar;D:\maven20210104\repository\com\google\guava\guava\16.0\guava-16.0.jar;D:\maven20210104\repository\com\netflix\hystrix\hystrix-core\1.5.18\hystrix-core-1.5.18.jar;D:\maven20210104\repository\io\reactivex\rxjava\1.3.8\rxjava-1.3.8.jar;D:\maven20210104\repository\org\hdrhistogram\HdrHistogram\2.1.9\HdrHistogram-2.1.9.jar;D:\maven20210104\repository\org\springframework\spring-core\5.1.9.RELEASE\spring-core-5.1.9.RELEASE.jar;D:\maven20210104\repository\org\springframework\spring-jcl\5.1.9.RELEASE\spring-jcl-5.1.9.RELEASE.jar com.itxiongmao.goshop.coupon.GoshopCouponApplication
2021-04-27 16:37:39.566  INFO 12740 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$99a5e5b2] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.8.RELEASE)

2021-04-27 16:37:40.270  INFO 12740 --- [           main] c.a.c.n.c.NacosPropertySourceBuilder     : Loading nacos data, dataId: 'goshop-coupon.properties', group: 'DEFAULT_GROUP'
2021-04-27 16:37:40.271  INFO 12740 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='NACOS', propertySources=[NacosPropertySource {name='goshop-coupon.properties'}]}

"goshop-coupon.properties",默认就是public命名空间中的内容中所配置的规则。

4、指定命名空间

如果想要使得我们自定义的命名空间生效,需要在“bootstrap.properties”文件中,指定使用哪个命名空间:

spring.cloud.nacos.config.namespace=e292a839-1854-479c-b7ff-e3c96d30a561

这个命名空间ID来源于我们在第一步所创建的命名空间

“goshop-coupon.properties”**,默认就是public命名空间中的内容中所配置的规则。

5、重启“goshop-coupon”,再次访问:http://localhost:8001/coupon/coupon/test

在这里插入图片描述

但是这种命名空间的粒度还是不够细化,对此我们可以为项目的每个微服务module创建一个命名空间。

6、为所有微服务创建命名空间

在这里插入图片描述

7、回到配置列表选项卡,克隆pulic的配置规则到coupon命名空间下

在这里插入图片描述
在这里插入图片描述

切换到coupon命名空间下,查看所克隆的规则:

在这里插入图片描述

8、修改“go-coupon”下的bootstrap.properties文件,添加如下配置信息

spring.cloud.nacos.config.namespace=42af420b-202c-48c8-9cd1-b045d7853fc7

这里指明的是,读取时使用coupon命名空间下的配置。

9、重启“goshop-coupon”,访问:http://localhost:8081/coupon/coupon/test
在这里插入图片描述

2.2.2 DataID方案

通过指定spring.profile.active和配置文件的DataID,来使不同环境下读取不同的配置,读取配置时,使用的是默认命名空间public,默认分组(default_group)下的DataID。

默认情况,Namespace=publicGroup=DEFAULT GROUP,默认Cluster是DEFAULT

通过制定spring.profiles.active=dev可以制定xxx-dev.properties的配置文件

2.2.3 Group方案

通过Group实现环境区分

实例:通过使用不同的组,来读取不同的配置,还是以上面的goshop-coupon微服务为例

1、新建“goshop-coupon.properties”,将它置于“tmp”组下

在这里插入图片描述

2、修改“bootstrap.properties”配置,添加如下的配置

spring.cloud.nacos.config.group=tmp

3、重启“gulimall-coupon”,访问:http://localhost:7000/coupon/coupon/test
在这里插入图片描述

2.3.同时加载多个配置集

当微服务数量很庞大时,将所有配置都书写到一个配置文件中,显然不是太合适。对此我们可以将配置按照功能的不同,拆分为不同的配置文件。

如下面的配置文件:

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://127.0.0.1:3306/goshop_sms
    driver-class-name: com.mysql.jdbc.Driver
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
  application:
    name: goshop-coupon

mybatis-plus:
  mapper-locations: classpath:/mapper/**/*.xml
  global-config:
    db-config:
      id-type: auto
      logic-delete-value: 1
      logic-not-delete-value: 0
server:
  port: 8081

我们可以将,

数据源有关的配置写到一个配置文件中:

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://127.0.0.1:3306/goshop_sms?useSSL=false&serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver

和框架有关的写到另外一个配置文件中:

mybatis-plus:
  global-config:
    db-config:
      id-type: auto
  mapper-locations: classpath:/mapper/**/*.xml

也可以将上面的这些配置交给nacos来进行管理。

实例:将“goshop-coupon”的“application.yml”文件拆分为多个配置,并放置到nacos配置中心

1、创建“datasource.yml”,用于存储和数据源有关的配置
在这里插入图片描述
2、将和mybatis相关的配置,放置到“mybatis.yml”中
在这里插入图片描述
3、创建“other.yml”配置,保存其他的配置信息
在这里插入图片描述
在这里插入图片描述
4、修改“goshop-coupon”的“bootstrap.properties”文件,加载“mybatis.yml”、“datasource.yml”和“other.yml”配置

spring.application.name=goshop-coupon
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.namespace=e292a839-1854-479c-b7ff-e3c96d30a561

spring.cloud.nacos.config.ext-config[0].data-id=mybatis.yml
spring.cloud.nacos.config.ext-config[0].group=DEFAULT_GROUP
spring.cloud.nacos.config.ext-config[0].refresh=true

spring.cloud.nacos.config.ext-config[1].data-id=datasource.yml
spring.cloud.nacos.config.ext-config[1].group=DEFAULT_GROUP
spring.cloud.nacos.config.ext-config[1].refresh=true


spring.cloud.nacos.config.ext-config[2].data-id=other.yml
spring.cloud.nacos.config.ext-config[2].group=DEFAULT_GROUP
spring.cloud.nacos.config.ext-config[2].refresh=true

"spring.cloud.nacos.config.ext-config"已经被废弃,建议使用“spring.cloud.nacos.config.extension-configs”,根据自己的版本选择配置。

5、注释“application.yml”文件中的所有配置

6、访问:http://localhost:8001/coupon/coupon/list,查看是否能够正常的访问数据库
在这里插入图片描述

3.SpringCloud Alibaba - gateway[服务网关]

3.1.网关概览

在这里插入图片描述

3.2.注册“goshop-gateway”到Nacos

3.2.1.创建“goshop-gateway”

在这里插入图片描述

3.2.2.添加“goshop-common”依赖和“spring-cloud-starter-gateway”依赖
<?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">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.itxiongmao.goshop</groupId>
    <artifactId>goshop-gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>goshop-gateway</name>
    <description>商城网关</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.itxiongmao.goshop</groupId>
            <artifactId>goshop-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>
3.2.3.“GoShopGatewayApplication”类上加上“@EnableDiscoveryClient”注解
package com.itxiongmao.goshop.gateway;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * 1、开启服务注册发现
 *  (配置nacos的注册中心地址)
 * 2、编写网关配置文件
 */
@EnableDiscoveryClient
public class GoShopGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GoShopGatewayApplication.class, args);
    }

}

3.2.4.在Nacos中创建“gateway”命名空间,同时在该命名空间中创建“goshop-gateway.yml”

在这里插入图片描述

3.2.5.创建“bootstrap.properties”文件,添加如下配置,指明配置中心地址和所属命名空间
spring.application.name=goshop-gateway
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.namespace=b8bef52f-c974-41e3-8468-f14f80bf96ca
6)创建“application.properties”文件,指定服务名和注册中心地址
spring.application.name=goshop-gateway
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
server.port=88
7)启动“goshop-gateway”

启动报错:

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

解决方法:在“GoShopGatewayApplication”中排除和数据源相关的配置

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

重新启动

访问:http://10.7.163.52:8848/nacos/index.html,查看到该服务已经注册到了Nacos中
在这里插入图片描述

4.案例

现在想要实现针对于“http://localhost:88/hello?url=baidu”,转发到“https://www.baidu.com”,针对于“http://localhost:88/hello?url=qq”的请求,转发到“https://www.qq.com/

4.1.创建“application.yml”

server:
  port: 88
spring:
  application:
    name: goshop-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    gateway:
      routes:
        - id: test_route
          uri: http://baidu.com:80/
          predicates:
            - Query=url,baidu
        - id: qq_route
          uri: http://qq.com:80/
          predicates:
            - Query=url,qq

logging:
  level:
    org.springframework.cloud.gateway: TRACE
    org.springframework.http.server.reactive: DEBUG
    org.springframework.web.reactive: DEBUG
    reactor.ipc.netty: DEBUG

4.2.启动“goshop-gateway”

在这里插入图片描述

4.3.测试

访问:http://localhost:88/hello?url=baidu
在这里插入图片描述

访问:http://localhost:88/hello?url=qq

Gateway官方文档

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

熊猫-IT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值