3. 构建微服务体系
Nacos,是alibaba公司出的一款集服务发现、配置、管理现代应用架构nacos官网
我们不去管怎么架设Nacos,只管怎么讲服务注册到Nacos,并实现服务之间的相互调用即可
1. 注册到Nacos上
在nacos上创建各个微服务对应的配置文件
因为是基础spring cloud来构建的体系,所以直接进行SpringCloud模式下进行实现
引用的包
- 通过 Nacos Server 和 spring-cloud-starter-alibaba-nacos-config 实现配置的动态变更。
- 通过 Nacos Server 和 spring-cloud-starter-alibaba-nacos-discovery 实现服务的注册与发现。
pom.xml文件中添加以下引用
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${latest.version}</version>
</dependency>
bootstrap.yml文件中添加nacos的配置
spring:
cloud:
nacos:
config:
file-extension: yml
namespace: f9432bb6-6ba5-4f5a-998b-c2b2e7815528
group: DEFAULT_GROUP
server-addr: localhost:8848
appliaction.class启动项增加@EnableDiscoveryClient注解
@EnableDiscoveryClient
@SpringBootApplication
public class ThreekingUserApplication {
public static void main(String[] args) {
SpringApplication.run(ThreekingUserApplication.class, args);
}
}
服务列表中即可看到服务
主要解决:nacos配置实现热加载(@value取值的问题),下面的方式不是最优,想办法解决
方案一:
@Autowired
ConfigurableApplicationContext configurableApplicationContext;
@RestController
public class testController {
public String getCommon(){
return configurableApplicationContext.getEnvironment().getProperty("common");
}
}
方案二:在类上增加@RefreshScope注册
@RefreshScope
@RestController
public class testController {
@Value("${common}")
private String common;
}
2. 各微服务之前相互调用
使用feign进行微服务之间的调用
为什么使用feign,其他的还有哪些
Feign是一种负载均衡的HTTP客户端, 使用Feign调用API就像调用本地方法一样,从避免了 调用目标微服务时,需要不断的解析/封装json 数据的繁琐。
引包:
pom.xml添加
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
新增一个接口类
UserFeign.java
@FeignClient(value = "threeking-user")
public interface UserFeign {
@GetMapping("/hello")
String hello();
}
暴露一个接口
@RestController
public class IndexController {
@Autowired
UserFeign userFeign;
@GetMapping("/")
public String hello(){
String u = userFeign.hello();
return "hello! this is content server, and user=" +u ;
}
}
@FeignClient(value = “threeking-user”)中的threeking-user对服务提供方,在nacos控制面板中可以找到
浏览地址http://localhost:6602/
结合swagger
接口介绍:
3.共用配置文件
配置文件扩展,修改各个微服务的配置文件
将bootstrap.yml进行修改,注意是bootstrap.yml,而不能用application.yml
spring:
application:
name: threeking-user
cloud:
nacos:
server-addr: localhost:8848
config:
file-extension: yml
namespace: f9432bb6-6ba5-4f5a-998b-c2b2e7815528
group: DEFAULT_GROUP
extension-configs:
- data-id: common-config.yml
group: GLOBAL_GROUP # 不指定默认为DEFAULT_GROUP
refresh: true
- data-id: database-config.yml
group: GLOBAL_GROUP
refresh: true
file-extension: 服务默认配置文件,{application-name}.{file-extension} : threeking-user.yml
extension-configs: 配置文件扩展组
common-config.yml , 我将一些公共配置,如日志管理,常量设置的放置在这个配置中
#配置日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
timeout: 20
database-config.yml 将一些如数据库、MQ、redis等服务器配置放置在这里,这样一些敏感信息,可以专门的人来负责,修改一次即可完成所有服务的配置
spring:
datasource: //数据库配置
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/tk_user?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
redis: //缓存配置
host: 192.168.110.160
port: 6379
database: 6
jedis:
pool:
max-active: 20
max-idle: 100
min-idle: 1
max-wait: 1000ms
nacos上配置项如此:
下一章 4.快速搭建一个vue项