• SpringCloud微服务初体验


    SpringCloud初体验

    Springcloud流应用程序启动器是基于SpringBoot的Spring集成应用程序,提供与外部系统的集成。一个生命周期短暂的微服务框架,用于快速构建执行有限数据处理的应用程序。

    一、服务的注册与发现-Nacos

    Nocas官网 https://nacos.io/zh-cn/docs/what-is-nacos.html

    解压即可使用。/bin/startup.cmd

    启动完成访问Nacos主页http://localsot/8848/nacos/index.html

    账号:nacso

    密码:nacos

    1.1 Nacos的作用

    1.注册中心Nacos可以作为注册中心,实现服务的发现和注册,服务的管理

    2.配置中心Nacos可以作为系统的统一配置中心,实现某些配置的统一管理,和动态发布

    1.2 Nacos的快速入门(微服务初体验)

    1.2.1 创建一个父工程SptingBoot项目,并且在父工程中指定SpringCloudRouting

    1603963403572

    1.2.2 创建Maven子项目Cloud_Provider - 服务提供者

    内层项目添加启动类和配置文件。启动类上添加注解(注册和发现服务)。

    1. 依赖jar包
    <!--依赖nacos的jar 实现服务的注册和发现-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
        <version>2.1.0.RELEASE</version>
    </dependency>
    
    1. 创建启动类,添加注册服务注解@EnableFeignClients
    @SpringBootApplication
    @EnableFeignClients     //发现注册和服务
    public class ProviderApplication {
        public static void main(String[] args) {
            SpringApplication.run(ProviderApplication.class, args);
        }
    }
    
    1. 修改配置文件,配置Nacos的服务地址和服务名
    server:
      port: 8081
    spring:
      application:
        name: HelloProvider #注册服务名
      cloud:
        nacos:
          discovery:
            server-addr: 10.8.154.84:8848  #Nacos的IP地址
    
    1. 实现要注册的服务,SpringBoot项目流程 Controller - Service - Dao - Mybatis - JDBC数据库
    @RestController
    @RequestMapping("/provider/user/")
    public class UserController {
        @Autowired
        private UserService service;
        @GetMapping("find.do")
        public String find() {
            return service.find();
        }
    }
    

    1.2.3 创建Maven子项目Cloud_Customer - 消费服务者

    调用服务提供者接口,根据服务名找到注册中心 ,服务名对应的配置IP和端口,拼接上Service的接口,调用服务提供者。

    1. 依赖jar
    <!--实现nacos的注册服务和发现-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
        <version>2.1.0.RELEASE</version>
    </dependency>
    <!--实现服务的调用-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
        <version>2.2.5.RELEASE</version>
    </dependency>
    
    1. 创建启动类Application,添加注解
    @SpringBootApplication
    @EnableDiscoveryClient  //注册服务
    @EnableFeignClients  //启动服务的发现
    public class CustomerApplication {
        public static void main(String[] args) {
            SpringApplication.run(CustomerApplication.class, args);
        }
    }
    
    1. 修改yml配置文件,配置Nacos的注册服务

    如果多个接口调用同一个服务的提供者,需要额外添加配置文件

    server:
      port: 8082
    spring:
      application:
        name: HelloCustomer
      cloud:
        nacos:
          discovery:
            server-addr: 10.8.154.84:8848
      main:
        allow-bean-definition-overriding: true #同一个服务可以被使用多次
    
    1. 创建调用服务接口Service

    如何有参数的调用需要添加注解@RequestionParam,否则无法传递参数到提供者

    Service接口

    @FeignClient("HelloProvider")  //找到服务名 ,得到ip和端口。拼接接口的路径找到实现者的接口进行请求
    public interface UserService {
        @GetMapping("/provider/user/find.do")
        String find();
        @GetMapping("/provider/user/findid.do")
        List<String> findById(@RequestParam int a);
    }
    

    Controller控制层

    @RestController
    @RequestMapping("/api/user/")
    public class UserController {
        @Autowired
        private UserService service;
        @GetMapping("find.do")
        public String find() {
            return service.find();
        }
        @GetMapping("findid.do")
        public List<String> finda(int a) {
            return service.findById(a);
        }
    

    1.2.4 微服务启动

    1.先保证注册中心启动
    2.启动提供者
    3.启动消费者
    通过访问服务消费者localhost:8080/api/user/find.do 找到服务这的Service接口,在接口中根据注解@FeignClient("HelloProvider")找到指定注册服务名,在注册服务中根据配置的IP和端口找到具体的服务提供者的请求URL路径,之后就会进行操作。

    image-20201029192231398

  • 相关阅读:
    Codeforces 877 C. Slava and tanks
    Codeforces 877 D. Olya and Energy Drinks
    2017 10.25 NOIP模拟赛
    2017 国庆湖南 Day1
    UVA 12113 Overlapping Squares
    学大伟业 国庆Day2
    51nod 1629 B君的圆锥
    51nod 1381 硬币游戏
    [JSOI2010]满汉全席
    学大伟业 2017 国庆 Day1
  • 原文地址:https://www.cnblogs.com/MonkeySun/p/13898724.html
Copyright © 2020-2023  润新知