一、阿里微服务简介1、基础描述
2、核心功能
- 服务限流降级
- 服务注册与发现
- 分布式配置管理
- 消息驱动能力
- 分布式事务
- 分布式任务调度
3、功能组件
- Nacos
- Sentinel
- RocketMQ
- Dubbo
- Seata
- OSS
- SchedulerX
4、环境和依赖
- Nacos服务
参考文章:Nacos组件,环境搭建和入门案例详解
- 版本关系
二、服务中心管理1、案例结构
- 服务端:node08-nacos-server
- 客户端:node08-nacos-clien
2、配置文件
my: name: cloud info: alibabaspring: datasource: type: com.alibaba.druid.pool.DruidDataSource druid: driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/data_one username: root password: 123
3、服务注册发现
- 核心依赖
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.1.1.RELEASE</version></dependency>
- 配置文件
spring: application: name: node08-nacos-server cloud: nacos: discovery: server-addr: 127.0.0.1:8848
- 提供服务接口
@RestControllerpublic class ServerWeb { private static Logger logger = LoggerFactory.getLogger(ServerWeb.class) ; @RequestMapping(value = "/web/getMsg",method = RequestMethod.GET) public String getMsg (@RequestParam("name") String name){ logger.info("8001 服务被调用..."); return "Hello:" + name ; }}
- 启动类注解
@SpringBootApplication// SpringCloud原生注解@EnableDiscoveryClient开启服务注册发现功能@EnableDiscoveryClientpublic class ApplicationServer { public static void main(String[] args) { SpringApplication.run(ApplicationServer.class,args) ; }}
- 核心依赖
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.1.1.RELEASE</version></dependency><!-- Feign组件 --><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.1.3.RELEASE</version></dependency>
- 基于Rest接口访问
@Configurationpublic class RestConfig { @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); }}
@RestControllerpublic class ClientWeb { @Resource private RestTemplate restTemplate ; @RequestMapping(value = "/web/getMsgV1/{name}",method = RequestMethod.GET) public String getMsgV1 (@PathVariable String name){ String reqUrl = "http://node08-nacos-server:8001/web/getMsg/"+name ; return restTemplate.getForObject(reqUrl,String.class) ; }}
- 基于Feign接口访问
@FeignClient("node08-nacos-server")public interface MsgFeign { @GetMapping("/web/getMsg") String getMsg (@RequestParam(name = "name") String name);}
@RestControllerpublic class ClientWeb { @Resource private MsgFeign msgFeign ; @RequestMapping(value = "/web/getMsgV2/{name}",method = RequestMethod.GET) public String getMsgV2 (@PathVariable String name){ return msgFeign.getMsg(name) ; }}
- 启动类配置
@SpringBootApplication@EnableDiscoveryClient@EnableFeignClients(basePackages={"cloud.nacos.client.feign"})public class ApplicationClient { public static void main(String[] args) { SpringApplication.run(ApplicationClient.class,args) ; }}三、配置中心管理
1、核心依赖<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2.1.1.RELEASE</version></dependency>2、服务端配置spring: application: name: node08-nacos-server cloud: nacos: discovery: server-addr: 127.0.0.1:8848 config: server-addr: 127.0.0.1:8848 file-extension: yaml
3、配置读取方式
@RestController@RefreshScopepublic class ValueWeb { @Value("${my.name:}") private String myName ; @Value("${my.info:}") private String myInfo ; @RequestMapping("/getNameInfo") public String getNameInfo (){ return myName+":"+myInfo ; }}4、数据库连接
@Configurationpublic class DruidConfig { @Value("${spring.datasource.druid.url}") private String dbUrl; @Value("${spring.datasource.druid.username}") private String username; @Value("${spring.datasource.druid.password}") private String password; @Value("${spring.datasource.druid.driverClassName}") private String driverClassName; /** * Druid 连接池配置 */ @Bean public DruidDataSource dataSource() { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(dbUrl); datasource.setUsername(username); datasource.setPassword(password); datasource.setDriverClassName(driverClassName); return datasource; } /** * JDBC操作配置 */ @Bean(name = "jdbcTemplate") public JdbcTemplate jdbcTemplate (@Autowired DruidDataSource dataSource){ return new JdbcTemplate(dataSource) ; }}
@RestControllerpublic class JdbcWeb { @Resource private JdbcTemplate jdbcTemplate ; @RequestMapping("/getJdbc") public List<String> getJdbc (){ String sql = "select phone from d_phone" ; List<String> phoneEntityList = jdbcTemplate.queryForList(sql,String.class) ; return phoneEntityList ; }}四、案例总结