赞
踩
引入依赖
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
启动nacos,这里因为我的虚拟机内存只有2个G启动nacos后,内存不够,所以需要修改startup.sh的jvm参数
修改jvm参数后,发现内存多了不少 (将近1个g)sh bin/startup.sh -m standalone (单机启动命令)
yml配置
cloud:
nacos:
discovery:
server-addr: 10.211.55.3:8848
application:
name: dreammall-coupon
加上注解
@SpringBootApplication
@EnableDiscoveryClient
@MapperScan("com.liubo.coupon.mapper")
public class CouponApplication {
public static void main(String[] args) {
SpringApplication.run(CouponApplication.class,args);
}
}
访问nacos控制台 http://10.211.55.3:8848/nacos/index.html
会员查询优惠卷情况
coupon模块提供查询优惠卷功能给会员模块远程调用
@RestController @RequestMapping("/coupon") public class CouponController { @Autowired private CouponHistoryService couponHistoryService; /** * 模拟会员模块查询优惠卷返回结果 * @return */ @RequestMapping("/coupons") public R coupons(){ Coupon coupon = new Coupon(); coupon.setCouponName("满100减100"); return R.ok().put("coupons", Arrays.asList(coupon)); } }
feign接口定义
@FeignClient("dreammall-coupon")
public interface CouponFeign {
@RequestMapping("/coupon/coupons")
R coupons();
}
引入openfeign依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
启动类加上注解
@EnableFeignClients(basePackages = "com.liubo.member.openfeign") // 声明要扫描的feigin包
调用openfeign查询数据
@RestController @RequestMapping("/member") public class MemberController { @Autowired private CouponFeign couponFeign; /** * openfeign远程调用查询会员所拥有的优惠卷 * @return */ @RequestMapping("/list") public R test(){ Member member = new Member(); member.setNickname("张三"); R r = couponFeign.coupons(); Object coupons = r.get("coupons"); return R.ok().put("member",member).put("coupon",coupons); } }
引入依赖
<!-- 配置中心来做配置管理-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
创建bootstrap.properties文件
spring.application.name=dreammall-coupon
spring.cloud.nacos.config.server-addr=10.211.55.3:8848
controller层实现
@RestController @RefreshScope // 配置中心更新实时刷新 @RequestMapping("/coupon") public class CouponController { @Value("coupon.user.name") private String userName; @Value("coupon.user.age") private String age; /** * 动态配置测试 * @return */ @RequestMapping("/config") public R testConfig(){ return R.ok().put("name",userName).put("age",age); } }
添加配置文件 命令规则:服务名.properties
如果有bootstrap.properties和配置中心同时配置,会优先读取配置中心
命令空间:每个服务都有自已的配置文件
配置分组:区别dev、test、prod环境
properties配置
spring.application.name=dreammall-coupon
spring.cloud.nacos.config.server-addr=10.211.55.3:8848
# 命令空间ID
spring.cloud.nacos.config.namespace=c076c9de-f4ed-4646-a316-8df40b4faf51
# 配置分组
spring.cloud.nacos.config.group=prod
加载多配置集
引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
yaml配置
server: port: 88 spring: cloud: nacos: discovery: server-addr: 10.211.55.3:8848 gateway: routes: - id: query_route uri: https://www.baidu.com predicates: - Query=baz application: name: dreammall-gateway
访问localhost:88?baz 跳转到百度首页
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。