




com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config
shop-user com.zlz 1.0 4.0.0 shop-user-server com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery org.springframework.boot spring-boot-starter-web org.projectlombok lombok com.zlz shop-sms-api 1.0 com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config
config:server-addr: 127.0.0.1:8848 #配置中心地址
server:port: 8010 #801开头 表示用户集群 用户服务
spring:application:name: shop-user #项目名称 作为微服务名cloud:nacos:server-addr: 127.0.0.1:8848 #注册中心地址config:server-addr: 127.0.0.1:8848 #配置中心地址
@Value("${jdbc.username}")private String yhm;@Value("${jdbc.password}")private String password;@RequestMapping("config")public String config(){return "读取的用户名: "+yhm+" ,密码:"+password;}
package com.zlz.controller;import com.zlz.shop.sms.api.dto.SmsDTO;
import com.zlz.shop.sms.api.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;@RestController
public class UserController {@AutowiredRestTemplate restTemplate;@RequestMapping("test")public String t(){//http://shop-sms/sendString s=restTemplate.postForObject("http://shop-sms/send", null, String.class);
// String s=restTemplate.postForObject("http://127.0.0.1:8021/send", null, String.class);return "用户服务调用短信服务 结果:"+s;}//使用oepnFeign的方式发送短信@AutowiredSmsService smsService;@RequestMapping("test2")public String t2(){//http://shop-sms/sendString s=smsService.send();return "用户服务调用短信服务 结果:"+s;}@RequestMapping("test3")public String t3(String tel){SmsDTO smsDTO = new SmsDTO();smsDTO.setTel(tel);String s=smsService.send2(smsDTO);return "用户服务调用短信服务 结果:"+s;}//spring环境里面的所有数据都可以读取,配置文件都行/*这里的jdbc.username需要和配置文件的key保持一致*/@Value("${jdbc.username}")private String yhm;@Value("${jdbc.password}")private String password;@RequestMapping("config")public String config(){return "读取的用户名: "+yhm+" ,密码:"+password;}
}




@RefreshScope
package com.zlz.controller;import com.zlz.shop.sms.api.dto.SmsDTO;
import com.zlz.shop.sms.api.service.SmsService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;@RestController
@RefreshScope//动态刷新config
public class UserController {@AutowiredRestTemplate restTemplate;@RequestMapping("test")public String t(){//http://shop-sms/sendString s=restTemplate.postForObject("http://shop-sms/send", null, String.class);
// String s=restTemplate.postForObject("http://127.0.0.1:8021/send", null, String.class);return "用户服务调用短信服务 结果:"+s;}//使用oepnFeign的方式发送短信@AutowiredSmsService smsService;@RequestMapping("test2")public String t2(){//http://shop-sms/sendString s=smsService.send();return "用户服务调用短信服务 结果:"+s;}@RequestMapping("test3")public String t3(String tel){SmsDTO smsDTO = new SmsDTO();smsDTO.setTel(tel);String s=smsService.send2(smsDTO);return "用户服务调用短信服务 结果:"+s;}//spring环境里面的所有数据都可以读取,配置文件都行(这些username会在启动项目的时候去读取)/*这里的jdbc.username需要和配置文件的key保持一致*/@Value("${jdbc.username}")private String yhm;@Value("${jdbc.password}")private String password;@RequestMapping("config")public String config(){return "读取的用户名: "+yhm+" ,密码:"+password;}
}







profiles:active: dev #环境,不写读取的就是无环境配置文件 如shop-user.properties,加了啥就会读取啥
server:port: 8010 #801开头 表示用户集群 用户服务
spring:application:name: shop-user #项目名称 作为微服务名cloud:nacos:server-addr: 127.0.0.1:8848 #注册中心地址config:server-addr: 127.0.0.1:8848 #配置中心地址profiles:active: dev #环境,不写读取的就是无环境配置文件 如shop-user.properties,加了啥就会读取啥


下一篇:贪心算法(基础)