目录
1.注册中心nacos
2.feign的加入
2.nacos作为配置中心
3.网关搭建
小张这里启动的是三台nacos作为集群~192.168.20.129:8848, 192.168.20.130:9948,192.168.20.131:8848;(使用集群的话最低三台)
在启动nginx之前,启动keepalived,用虚拟ip漂移,搭建两台nginx作为集群,ip:192.168.20.50
并配置nginx的config,方向代理和负载均衡ncaos
启动nacos,和nginx后,访问浏览器192.168.20.50:1111/nacos
注:如果不会nacos和nginx的可以先看下小张笔记:come on 我的宝儿
添加nacos的依赖
直接添加到公共模块
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
#添加alibaba的管控依赖
com.alibaba.cloud
spring-cloud-alibaba-dependencies
2.2.0.RELEASE
pom
import
注意再启动之前一定要配置服务名称
spring:
#数据源
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.20.129:3306/gulimall_sms
username: root
password: root
#nacos
cloud:
nacos:
discovery:
server-addr: 192.168.20.50:1111
#服务名称
application:
name: gulimall-coupon
mybatis-plus:
mapper-locations: classpath*:/mapper/**/*.xml
global-config:
db-config:
id-type: auto
server:
port: 7000
如果启动不成功,查看一下自己的spring boot和cloud的版本号
添加feign的依赖
org.springframework.cloud
spring-cloud-starter-openfeign
编写feign的接口@FeignClient(“要调用服务的名称”)
@Component
@FeignClient("gulimall-coupon")
public interface CouponFeignService {
@RequestMapping("/coupon/coupon/members/list")
public R memberCoupons();
}
在controller层注入刚刚写好的接口,调用即可
@RestController
@RequestMapping("member/member")
public class MemberController {
@Autowired
CouponFeignService couponFeignService;
@RequestMapping("/coupons")
public R test(){
MemberEntity memberEntity = new MemberEntity();
memberEntity.setNickname("小三");
R r = couponFeignService.memberCoupons();
return R.ok().put("member",memberEntity).put("coupons",r.get("coupons"));
}
}
添加@EnableFeignClients作为feign客户端
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients(basePackages="com.xz.gulimall.member.feign")
public class GulimallMemberApplication {
public static void main(String[] args) {
SpringApplication.run(GulimallMemberApplication.class, args);
}
}
如果测试不通过,报jar冲突的话,更换pom文件中的httpcore版本
注:使用nacos的话不需要导入ribbon的jar包,因为nacos自带
在公共模块添加依赖
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config
spring:
#数据源
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.20.129:3306/gulimall_sms
username: root
password: root
#nacos
cloud:
nacos:
discovery:
server-addr: 192.168.20.50:1111
config:
server-addr: 192.168.20.50:1111
file-extension: yml
namespace: ae0280bf-e9c4-4a58-904d-516b63057ff0
group: TEST_GROUP
application:
name: gulimall-coupon
mybatis-plus:
mapper-locations: classpath*:/mapper/**/*.xml
global-config:
db-config:
id-type: auto
server:
port: 7000
spring:
profiles:
active: test
- 1.在父工程加创建网管模块
- 2.注意jdk和maven版本
- 3.注意spring boot和cloud的版本
记得排除spring-boot-starter-web,不然会与gateway冲突
1.8
UTF-8
UTF-8
2.2.2.RELEASE
Hoxton.SR1
com.xz.gulimall
gulimall-common
0.0.1-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-gateway
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
spring:
application:
name: guliamll-gateway
cloud:
nacos:
discovery:
server-addr: 192.168.20.50:1111
config:
server-addr: 192.168.20.50:1111
namespace: 94c7564b-624f-44f9-9567-e512ac68a97c
gateway:
routes:
- id: baidu_route
uri: https://www.baidu.com
predicates:
- Query=url,baidu
server:
port: 88
pom里引入了公共模块,所以排除数据库的相关配置
@EnableDiscoveryClient//开启服务的注册发现
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})//排除数据库相关配置
public class GulimallGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GulimallGatewayApplication.class, args);
}
}
详细了解gateway:就差你了