持续学习&持续更新中…
学习态度:守破离
SpringCloudAlibaba官网:https://spring.io/projects/spring-cloud-alibaba/
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.1.0.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
官网:https://nacos.io/zh-cn/
下载nacos-server1.1.3:
https://objects.githubusercontent.com/github-production-release-asset-2e65be/137451403/d6c1ee80-b87c-11e9-8f32-79d0ba6f9c89?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20221129%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20221129T110414Z&X-Amz-Expires=300&X-Amz-Signature=609c8b94c94e42fa5463ef109f3ea2df6a86e9c292822be96b99ce4c7d89a915&X-Amz-SignedHeaders=host&actor_id=46433032&key_id=0&repo_id=137451403&response-content-disposition=attachment%3B%20filename%3Dnacos-server-1.1.3.zip&response-content-type=application%2Foctet-stream
运行nacos-server:
依赖:
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
application.yml:
spring:
cloud:
nacos:
discovery:
server-addr: localhost:8848
application:
name: gulimall-coupon
注解:
@EnableDiscoveryClient
@SpringBootApplication
public class GulimallCouponApplication {
public static void main(String[] args) {
SpringApplication.run(GulimallCouponApplication.class, args);
}
}
可视化界面:
依赖:【feign被闭源了,SpringCloud开源了一个OpenFeign。属于SpringCloud,不属于SpringCloudAlibaba】
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Greenwich.SR3version>
<type>pomtype>
<scope>importscope>
dependency>
dependencyManagement>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
开启Feign的使用:
@EnableFeignClients(basePackages = "l.p.gulimall.member.feign")
@EnableDiscoveryClient
@SpringBootApplication
public class GulimallMemberApplication {
public static void main(String[] args) {
SpringApplication.run(GulimallMemberApplication.class, args);
}
}
声明式远程调用:
gulimall-coupon:
@RestController
@RequestMapping("coupon/coupon")
public class CouponController {
@Autowired
private CouponService couponService;
/**
* 测试feign
*/
@RequestMapping("/memberCoupon")
public R memberCoupon() {
CouponEntity coupon = new CouponEntity();
coupon.setCouponName("买一送一");
return R.ok().put("coupon", coupon);
}
}
gulimall-member:
/*
这是一个声明式的远程调用
*/
@FeignClient("gulimall-coupon")
public interface CouponFeignService {
@RequestMapping("/coupon/coupon/memberCoupon")
R memberCoupon();
}
MemberController:
@RestController
@RequestMapping("member/member")
public class MemberController {
@Autowired
private CouponFeignService couponFeignService;
@RequestMapping("/coupons")
public R test() {
MemberEntity member = new MemberEntity();
member.setUsername("lp");
return R.ok().put("member", member).put("coupons", couponFeignService.memberCoupon());
}
}
引入依赖:
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
新建bootstrap.properties:【.properties与.yml文件都可以】
spring.application.name=gulimall-coupon
spring.cloud.nacos.config.server-addr=localhost:8848
Nacos中添加配置:
微服务中使用配置:
@RefreshScope
@RestController
@RequestMapping("coupon/coupon")
public class CouponController {
@Value("${coupon.user.name}")
private String cun;
@Value("${coupon.user.age}")
private Integer cua;
@RequestMapping("/test")
public R test() {
return R.ok().put("name", cun).put("age", cua);
}
}
默认:public(保留空间);默认新增的所有配置都在public命名空间,默认使用的也是public。
举例:
注意:在bootstrap.properties配置上,需要使用哪个命名空间下的配置:spring.cloud.nacos.config.namespace=9de62e44-cd2a-4a82-bf5c-95878bd5e871
默认所有的配置集都属于:DEFAULT_GROUP
项目中的使用:
bootstrap.yml:
spring:
application:
name: gulimall-coupon
cloud:
nacos:
discovery:
server-addr: localhost:8848
config:
server-addr: localhost:8848
namespace: ddecbef0-996d-48bd-ab3c-910af2291344
group: dev
ext-config:
- dataId: datasource.yaml
group: dev
refresh: true
- dataId: mybatis.yaml
group: dev
refresh: true
- dataId: others.yaml
group: dev
refresh: true
datasource.yaml:
others.yaml:
mybatis.yaml:
gulimall-coupon.properties:
总结:
引入依赖:
<dependencies>
<dependency>
<groupId>l.p.gulimallgroupId>
<artifactId>gulimall-commonartifactId>
<version>1.0.0version>
<exclusions>
<exclusion>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-gatewayartifactId>
dependency>
dependencies>
Nacos配置:
server:
port: 88
spring:
cloud:
gateway:
routes:
- id: test_route
uri: https://www.baidu.com
predicates:
- Query=url,baidu
- id: qq_route
uri: https://www.qq.com
predicates:
- Query=url,qq
bootstrap.yml:
spring:
application:
name: gulimall-gateway
cloud:
nacos:
discovery:
server-addr: localhost:8848
config:
server-addr: localhost:8848
namespace: a46c9ea5-c78b-4191-bb0c-14da3991c2f7
group: dev
file-extension: yaml
启动gateway网关服务:
@EnableDiscoveryClient
// 父项目中有依赖MySQL的starter,因此需要在这儿排除一下【或者在pom.xml中使用标签】
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class GulimallGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GulimallGatewayApplication.class, args);
}
}
访问测试:
雷丰阳: Java项目《谷粒商城》Java架构师 | 微服务 | 大型电商项目).
本文完,感谢您的关注支持!