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

天津网站建设推广百度网盘客服电话

天津网站建设推广,百度网盘客服电话,凌风科技wordpress,业网站建设将配置交给Nacos管理的步骤 1、在Nacos中添加配置文件 2、在微服务中引入nacos的config依赖 3、在微服务中添加bootstrap.yml,配置nacos地址、当前环境、服务名称、文件后缀名。这些决定了程序启动时去nacos读取哪个文件 Nacos配置更改后,微服务可以实…

将配置交给Nacos管理的步骤
1、在Nacos中添加配置文件
2、在微服务中引入nacos的config依赖
3、在微服务中添加bootstrap.yml,配置nacos地址、当前环境、服务名称、文件后缀名。这些决定了程序启动时去nacos读取哪个文件
 

Nacos配置更改后,微服务可以实现热更新,方式:
1、通过@Value注解注入,结合@RefreshScope来刷新
2、通过@ConfigurationProperties注入,自动刷新
注意事项:
·不是所有的配置都适合放到配置中心,维护起来比较麻烦
·建议将一些关键参数,需要运行时调整的参数放到nacos配置中心,一般都是自定义配置

微服务会从nacos读取的配置文件:
[服务名]-[spring.profile.active].yaml,环境配置

[服务名].yaml,默认配置,多环境共享
优先级:[服务名]-[环境].yaml >[服务名].yaml >本地配置
 

Nacos实现配置管理

点击配置管理

点击加号

设置好这些属性:

Data ID:一般不能重复,建议写服务名称

描述:描述是用来写这个配置作用的

配置格式用yaml

配置内容:这里我写的是时间格式,在后面java中可以使用这个配置

 发布完成后:可以得到一个配置

微服务配置拉取

引入Nacos的配置管理客户端依赖:

<! --nacos配置管理依赖--><dependency>
<groupid>com.alibaba.cloud</ groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></ dependency> 

这里我用userservice的pom文件 

<?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><artifactId>cloud-demo</artifactId><groupId>cn.itcast.demo</groupId><version>1.0</version></parent><modelVersion>4.0.0</modelVersion><artifactId>user-service</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!--mybatis--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId></dependency><!--eureka客户依赖-->
<!--        <dependency>-->
<!--            <groupId>org.springframework.cloud</groupId>-->
<!--            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>-->
<!--        </dependency>--><!--nacos客户端依赖包--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency></dependencies><build><finalName>app</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>

在userservice中的resource目录添加一个bootstrap.yml文件,这个文件是引导文件,优先级高于application.yml;

spring:
    application:
        name: userservice #服务名称

    profiles:
        active: dev#开发环境,这里是dev

    cloud:
        nacos:
            server-addr: localhost:8848 #Nacos地址

            config:
                file-extension: yaml #文件后缀名

spring:application:name: userservice #服务名称profiles:active: dev #开发环境,这里是devcloud:nacos:server-addr: localhost:8848 #nacos地址config:file-extension: yaml #文件后缀名

把application的一些重复配置注释掉

server:port: 8081
spring:datasource:url: jdbc:mysql://localhost:3306/cloud_user?useSSL=falseusername: rootpassword: 1234driver-class-name: com.mysql.jdbc.Driver
#  application:
#    name: userservice #user的微服务名称
#  cloud:
#    nacos:
#      server-addr: localhost:8848 #nacos服务地址
#      discovery:
#        cluster-name: SH #集群名称
mybatis:type-aliases-package: cn.itcast.user.pojoconfiguration:map-underscore-to-camel-case: true
logging:level:cn.itcast: debugpattern:dateformat: MM-dd HH:mm:ss:SSS
#eureka:
#  client:
#    service-url: #eureka地址信息
#      defaultZone: http://127.0.0.1:10086/eureka

在controller中写新的代码,判断前面配的配置文件是否有效

@Value("${pattern.dateformat}")
//注解读取该配置
private String dateformat;@GetMapping("now")
public String now(){return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat));//通过该配置改变时间格式
}
package cn.itcast.user.web;import cn.itcast.user.pojo.User;
import cn.itcast.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;import java.text.DateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {@Autowiredprivate UserService userService;@Value("${pattern.dateformat}")//注解读取该配置private String dateformat;@GetMapping("now")public String now(){return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat));//通过该配置改变时间格式}/*** 路径: /user/110** @param id 用户id* @return 用户*/@GetMapping("/{id}")public User queryById(@PathVariable("id") Long id) {return userService.queryById(id);}
}

 重启服务后,访问,可以看到我们设置好的时间格式

配置热更新

每次更新配置后,都需要重启服务才能生效。配置热更新后,不需要重启服务也能使用新配置

Nacos中的配置文件变更后,微服务无需重启就可以感知。不过需要通过下面两种配置实现:

●方式一:在@Value注入的变量所在类上添加注解@RefreshScope

@RefreshScope
package cn.itcast.user.web;import cn.itcast.user.pojo.User;
import cn.itcast.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;import java.text.DateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;@Slf4j
@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {@Autowiredprivate UserService userService;@Value("${pattern.dateformat}")//注解读取该配置private String dateformat;@GetMapping("now")public String now(){return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat));//通过该配置改变时间格式}/*** 路径: /user/110** @param id 用户id* @return 用户*/@GetMapping("/{id}")public User queryById(@PathVariable("id") Long id) {return userService.queryById(id);}
}

 重启服务后,访问网页

把配置文件修改:

不重启,再次访问

可以发现,/变成了年月日了,但我们并没有重启服务。所以完成了热更新

●方式二:使用@ConfigurationProperties注解

我们把上个方法的注解注释掉,并注入新的类,并改变时间格式的参数

package cn.itcast.user.web;import cn.itcast.user.config.PatternProperties;
import cn.itcast.user.pojo.User;
import cn.itcast.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;import java.text.DateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;@Slf4j
@RestController
@RequestMapping("/user")
//@RefreshScope
public class UserController {@Autowiredprivate UserService userService;//    @Value("${pattern.dateformat}")
//    //注解读取该配置
//    private String dateformat;@Autowiredprivate PatternProperties properties;//注入新的java类@GetMapping("now")public String now(){return LocalDateTime.now().format(DateTimeFormatter.ofPattern(properties.getDateformat()));//通过该配置改变时间格式}/*** 路径: /user/110** @param id 用户id* @return 用户*/@GetMapping("/{id}")public User queryById(@PathVariable("id") Long id) {return userService.queryById(id);}
}

新建一个包,并新建一个PatternProperties.java

package cn.itcast.user.web;import cn.itcast.user.pojo.User;
import cn.itcast.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;import java.text.DateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;@Slf4j
@RestController
@RequestMapping("/user")
//@RefreshScope
public class UserController {@Autowiredprivate UserService userService;@Value("${pattern.dateformat}")//注解读取该配置private String dateformat;@GetMapping("now")public String now(){return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat));//通过该配置改变时间格式}/*** 路径: /user/110** @param id 用户id* @return 用户*/@GetMapping("/{id}")public User queryById(@PathVariable("id") Long id) {return userService.queryById(id);}
}

访问网页:

改变配置文件:

再次访问:发现没有重启服务也能发生改变

多环境配置共享

再新建一个配置文件

 可以看到:上面的是dev环境的,下面是多环境共享的

修改PatternProperties.java,多读取一个变量

package cn.itcast.user.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Data
@Component
@ConfigurationProperties(prefix = "pattern")//前缀名后下面的dateformat拼接,和配置一样就可以使用配置
public class PatternProperties {private String dateformat;private String envSharedValue;
}

 在controller.java中新增一个getmapping

package cn.itcast.user.web;import cn.itcast.user.config.PatternProperties;
import cn.itcast.user.pojo.User;
import cn.itcast.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;import java.text.DateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;@Slf4j
@RestController
@RequestMapping("/user")
//@RefreshScope
public class UserController {@Autowiredprivate UserService userService;//    @Value("${pattern.dateformat}")
//    //注解读取该配置
//    private String dateformat;@Autowiredprivate PatternProperties properties;//注入新的java类@GetMapping("prop")public  PatternProperties properties(){return properties;}@GetMapping("now")public String now(){return LocalDateTime.now().format(DateTimeFormatter.ofPattern(properties.getDateformat()));//通过该配置改变时间格式}/*** 路径: /user/110** @param id 用户id* @return 用户*/@GetMapping("/{id}")public User queryById(@PathVariable("id") Long id) {return userService.queryById(id);}
}

重启服务:注意userservice的bootstrap.yml文件配置的环境是dev,所以访问页面是dev环境的

在userservice2中,设置环境,让他跟userservice1不是同一个环境

   

使用test环境,表示测试环境

重启userservice2

观察两个userservice

可以发现:userservice1可以读到时间,而userservice2无法读到时间。因为时间是dev环境的,而envSharedValue是共享环境

配置的优先级

在application.yml配置pattern

pattern:name: 本地环境local
server:port: 8081
spring:datasource:url: jdbc:mysql://localhost:3306/cloud_user?useSSL=falseusername: rootpassword: 1234driver-class-name: com.mysql.jdbc.Driver
#  application:
#    name: userservice #user的微服务名称
#  cloud:
#    nacos:
#      server-addr: localhost:8848 #nacos服务地址
#      discovery:
#        cluster-name: SH #集群名称
mybatis:type-aliases-package: cn.itcast.user.pojoconfiguration:map-underscore-to-camel-case: true
logging:level:cn.itcast: debugpattern:dateformat: MM-dd HH:mm:ss:SSS
#eureka:
#  client:
#    service-url: #eureka地址信息
#      defaultZone: http://127.0.0.1:10086/eurekapattern:name: 本地环境local

 在PatternProperties.java加上name属性

package cn.itcast.user.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Data
@Component
@ConfigurationProperties(prefix = "pattern")//前缀名后下面的dateformat拼接,和配置一样就可以使用配置
public class PatternProperties {private String dateformat;private String envSharedValue;private String name;
}

重启服务:name一定是:本地环境local,因为此时只有本地配置了该属性

在userservice.yml配置name属性:

访问:name为:环境共享属性default,可以看出userservice的配置环境比本地的高 

在userservice-dev.yml配置中添加属性name 

访问:name为环境配置dev,可以看出dev环境配置文件是优先级最高的

 得出结论:优先级:[服务名]-[环境].yaml >[服务名].yaml >本地配置

代码文件点击下载icon-default.png?t=N7T8https://pan.baidu.com/s/1E1QHRnmoGYkUFNI7VXjflQ?pwd=wdf3 上一篇:Nacos和Eureka的区别


文章转载自:
http://dinncoescap.ydfr.cn
http://dinncoskep.ydfr.cn
http://dinncoquadrangularly.ydfr.cn
http://dinncosao.ydfr.cn
http://dinncosensitize.ydfr.cn
http://dinncocarrel.ydfr.cn
http://dinncoincommodity.ydfr.cn
http://dinncounnamable.ydfr.cn
http://dinncotechnetronic.ydfr.cn
http://dinncohypochlorite.ydfr.cn
http://dinncofougasse.ydfr.cn
http://dinncoaustria.ydfr.cn
http://dinncoultraist.ydfr.cn
http://dinncodetainment.ydfr.cn
http://dinncooxydation.ydfr.cn
http://dinncoreliquary.ydfr.cn
http://dinncopermanency.ydfr.cn
http://dinncoherniate.ydfr.cn
http://dinncocashomat.ydfr.cn
http://dinncotracheoesophageal.ydfr.cn
http://dinncoclustering.ydfr.cn
http://dinncopolypod.ydfr.cn
http://dinncononperson.ydfr.cn
http://dinncoemigratory.ydfr.cn
http://dinncoflytable.ydfr.cn
http://dinncooxycarpous.ydfr.cn
http://dinncoentirety.ydfr.cn
http://dinncophysiotherapeutic.ydfr.cn
http://dinncoapiarian.ydfr.cn
http://dinncosneaky.ydfr.cn
http://dinncomicawberism.ydfr.cn
http://dinncovavasour.ydfr.cn
http://dinncoeparchy.ydfr.cn
http://dinncocate.ydfr.cn
http://dinncohydropic.ydfr.cn
http://dinncoafforce.ydfr.cn
http://dinncoex.ydfr.cn
http://dinncooxychloride.ydfr.cn
http://dinncolegitimately.ydfr.cn
http://dinncophyletic.ydfr.cn
http://dinncosatai.ydfr.cn
http://dinncodawdler.ydfr.cn
http://dinncolateral.ydfr.cn
http://dinncofistic.ydfr.cn
http://dinncotransgressor.ydfr.cn
http://dinncogradus.ydfr.cn
http://dinncodefogger.ydfr.cn
http://dinncouniversalism.ydfr.cn
http://dinncochilled.ydfr.cn
http://dinncobibliophile.ydfr.cn
http://dinncofrogface.ydfr.cn
http://dinncopowerpoint.ydfr.cn
http://dinncomanteltree.ydfr.cn
http://dinnconeuropsychical.ydfr.cn
http://dinncothiophosphate.ydfr.cn
http://dinncononsuit.ydfr.cn
http://dinncotelecast.ydfr.cn
http://dinncoteleost.ydfr.cn
http://dinncozoanthropy.ydfr.cn
http://dinncochummage.ydfr.cn
http://dinncoconjunctiva.ydfr.cn
http://dinncoviolently.ydfr.cn
http://dinncocellular.ydfr.cn
http://dinnconoumenally.ydfr.cn
http://dinncoyurt.ydfr.cn
http://dinncobata.ydfr.cn
http://dinnconeedless.ydfr.cn
http://dinncoberber.ydfr.cn
http://dinncoimmunoreactive.ydfr.cn
http://dinncomeagerly.ydfr.cn
http://dinncodisappear.ydfr.cn
http://dinncodiamorphine.ydfr.cn
http://dinncoprecocity.ydfr.cn
http://dinncopylorus.ydfr.cn
http://dinncogitana.ydfr.cn
http://dinncovagotonia.ydfr.cn
http://dinncoswaddle.ydfr.cn
http://dinncotowerless.ydfr.cn
http://dinncoingenuity.ydfr.cn
http://dinncoploughhead.ydfr.cn
http://dinncocorticotrophin.ydfr.cn
http://dinncogenialise.ydfr.cn
http://dinncofulgent.ydfr.cn
http://dinncosarcophagous.ydfr.cn
http://dinncoepicardial.ydfr.cn
http://dinncoyrast.ydfr.cn
http://dinncoflatly.ydfr.cn
http://dinncocreedal.ydfr.cn
http://dinncorusty.ydfr.cn
http://dinncoimpicture.ydfr.cn
http://dinncopushover.ydfr.cn
http://dinncopraseodymium.ydfr.cn
http://dinncorebekah.ydfr.cn
http://dinncoonus.ydfr.cn
http://dinncoplaymaker.ydfr.cn
http://dinncotripoli.ydfr.cn
http://dinncoraisin.ydfr.cn
http://dinncoleatherneck.ydfr.cn
http://dinncoocclusion.ydfr.cn
http://dinncocalceolaria.ydfr.cn
http://www.dinnco.com/news/139177.html

相关文章:

  • 建材网站制作百度中心人工电话号码
  • 做学分网站怎么去营销自己的产品
  • 网站建设的软件平台免费自学电商教程
  • 南京网站建设与维护电商热门关键词
  • 哪个全球购网站做的好建站系统推荐
  • 做陶瓷公司网站seo的优点
  • 大学生个人网站制作百度咨询电话 人工客服
  • 网站初期做几个比较好企业网络推广的方法
  • 建设个人网站需要多少钱seo的含义
  • 如何申请个人网站域名新闻媒体发稿平台
  • 完善爱心服务网站建设的意义网站移动端优化工具
  • 重庆哪里有做淘宝网站推广的南京seo网站优化
  • 外贸网站如何推广出去百度收录查询工具
  • wordpress the_category_id庆云网站seo
  • 怎么百度做网站子域名大全查询
  • 佛山手机网站建设提高百度搜索排名工具
  • icp网站建设国外网站推广公司
  • 有了域名怎样做淘客网站企业门户网站
  • 佛山网站制作建设互联网运营推广
  • 网站平台怎么做的天津网络推广公司
  • 自己做的网站有排名吗搜索风云榜
  • 网站如何在百度四川网站制作
  • 建e网手机版网站推广优化业务
  • 互联网之光博览会资阳市网站seo
  • 公司做的网站计入什么推广赚钱项目
  • 网站m3u8链接视频怎么做的南宁百度快速排名优化
  • 建设网站怎么知道真假谷歌站长平台
  • 黄石建委网工程建设城建网站企业管理咨询
  • 网站面包屑导航怎么做的青岛seo网站关键词优化
  • 在线做ppt的网站有哪些上海免费关键词排名优化