zoukankan      html  css  js  c++  java
  • springcloud微服务总结三 服务客户端

    一 springcloud服务理解:

      dubbo中服务注册和调用都是都过注解来进行的,dubbo中在service层中调用服务是通过将@service注解改变为dubbo代码架包中的service注解,这样就可以应用

    而在springcloud注册中心如果是eureka的话是通过工程客户端进行调用,如果是consol也是的,dubbo的服务是通过rpc引用的,springcloud使用的http来进行注册引用的,所以在很大程度的是区别很大的,需要注意的dubbo的是一个service,而springcloud是一个web工程,本身就是一个模块

    二 eureka工程搭建

    引入依赖:

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <!--千万注意,不要写成spring-cloud-netflix-eureka-client,写成这样不会报错,但是注册不了服务-->
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
     
    </dependencies>

    配置:

    eureka:
      client:
        serviceUrl:
         defaultZone: http://localhost:8761/eureka/  #eureka注册中心地址
    spring:
      application:
        name: person-service #应用名

    启动类:

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
     
    @SpringBootApplication
    @EnableEurekaClient  //开启注解,注册服务
    public class PersonApplication {
     
        public static void main(String[] args) {
            SpringApplication.run(PersonApplication.class,args);
        }
    }

    controller

    这个别忘记,是提供服务的重要点:

     
    import com.wendao.course.pojo.Course;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
     
     
    @RestController
    public class PersonController {
     
        @RequestMapping("/Person/{personid}")
        public Course findCourse(@PathVariable("personid") Integer personid){
            return new Person("微服务架构",300,1);
        }
    }

    此处代码无法直接运行,需要进行新建person类

    三:zookeeper注册中心的服务调用与注册

    在服务提供者方需要加入配置:

    spring:
      application:
        name: HelloWorld
      cloud:
        zookeeper:
          connect-string: localhost:2181
          discovery:
            enabled: true
    server:
      port: 8081
    endpoints:
      restart:
        enabled: true
    logging:
      level:
        org.apache.zookeeper.ClientCnxn: WARN

    在主程序上加入:

    使用@EnableDiscoveryClient注释我们的主类,这将使HelloWorld 应用程序自动发布

    服务消费者,引入依赖:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        <exclusions>
            <exclusion>
                <artifactId>commons-logging</artifactId>
                <groupId>commons-logging</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-feign</artifactId>
    </dependency>
    同服务提供者一样在主程序中增加@EnableDiscoveryClient 注解。

    controller类:

    @RestController
    public class GreetingController {
        @Autowired
        private HelloWorldClient helloWorldClient;
    
        @GetMapping("/get-greeting")
        public String greeting() {
    
            return helloWorldClient.HelloWorld();
    
        }
    }

    声明式服务调用客户端

    通过引入spring-cloud-starter-feign组件包使用声明式服务调用方式调用远程服务,使用@FeignClient(“service-name”)注释一个接口并将它自动连接到我们的应用程序中,以便我们以编程方式访问此服务。

    @Configuration
    @EnableFeignClients
    @EnableDiscoveryClient
    public class HelloWorldClient {
        @Autowired
        private TheClient theClient;
    
        @FeignClient(name = "HelloWorld")
        interface TheClient {
    
            @RequestMapping(path = "/helloworld", method = RequestMethod.GET)
            @ResponseBody
            String HelloWorld();
        }
    
        public String HelloWorld() {
            return theClient.HelloWorld();
        }
    }
    

    配置文件

    spring:
      application:
        name: Greeting
      cloud:
        zookeeper:
          connect-string: localhost:2181
    server:
      port: 8083
    logging:
      level:
        org.apache.zookeeper.ClientCnxn: WARN
    

    运行

    HelloWorld REST服务在Zookeeper中注册了自己,Greeting服务通过声明式客户端发现和调用HelloWorld 服务。

    现在我们可以运行这两个服务,然后在浏览器中访问 http://localhost:8083/get-greeting,将返回




  • 相关阅读:
    oracle客户端plsql设置字符集
    命令导入导出oracle库
    java初级开发一系列的工具安装配置
    docker学习笔记-5.数据共享
    docker学习笔记-4.harbor
    flask学习笔记2
    socket编程学习
    docker学习笔记-3.docker镜像制作
    docker学习笔记-2.docker镜像制作
    docker学习笔记-1.docker基础
  • 原文地址:https://www.cnblogs.com/xiufengchen/p/10430509.html
Copyright © 2011-2022 走看看