zoukankan      html  css  js  c++  java
  • Atitit SpringCloud 使用总结 目录 1.1. 启动一个服务注册中心EurekaServer 1 1.2. 三、创建一个服务提供者 (eureka client) 2 1.3. 创建

    Atitit SpringCloud 使用总结

     

    目录

    1.1. 启动一个服务注册中心EurekaServer 1

    1.2. 三、创建一个服务提供者 (eureka client) 2

    1.3. 创建消费者 5

     

      1. 启动一个服务注册中心EurekaServer

    ,只需要一个注解@EnableEurekaServer,这个注解需要在springboot工程的启动application类上加:

     

     

    @EnableEurekaServer

    @SpringBootApplication

    public class EurekaserverApplication {

     

    public static void main(String[] args) {

    SpringApplication.run(EurekaserverApplication.class, args);

    }

    }

    ————————————————

    **eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以在内存中完成),在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。eureka server的配置文件application.yml:

     

    server:

      port: 8761

     

    eureka:

      instance:

        hostname: localhost

      client:

        registerWithEureka: false

        fetchRegistry: false

        serviceUrl:

          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

          

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server.

     

    2.5 eureka server 是有界面的,启动工程,打开浏览器访问:

    http://localhost:8761 ,界面如下:

     

     

     

    No application available 没有服务被发现 ……_

    因为没有注册服务当然不可能有服务被发现了。

     

     

      1. 三、创建一个服务提供者 (eureka client)

    当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。

     

    过注解@EnableEurekaClient 表明自己是一个eurekaclient.

     

    @SpringBootApplication

    @EnableEurekaClient

    @RestController

    public class ServiceHiApplication {

     

    public static void main(String[] args) {

    SpringApplication.run(ServiceHiApplication.class, args);

    }

     

    @Value("${server.port}")

    String port;

    @RequestMapping("/hi")

    public String home(@RequestParam String name) {

    return "hi "+name+",i am from port:" +port;

    }

     

    }

    ————————————————

    仅仅@EnableEurekaClient是不够的,还需要在配置文件中注明自己的服务注册中心的地址,application.yml配置文件如下:

     

    eureka:

      client:

        serviceUrl:

          defaultZone: http://localhost:8761/eureka/

    server:

      port: 8762

    spring:

      application:

        name: service-hi

     

     

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 。

    启动工程,打开http://localhost:8761 ,即eureka server 的网址:

    ————————————————

    你会发现一个服务已经注册在服务中了,服务名为SERVICE-HI ,端口为7862

    这时打开 http://localhost:8762/hi?name=forezp ,你会在浏览器上看到 :

    hi forezp,i am from port:8762

     

      1. 创建消费者

    在工程的配置文件指定服务的注册中心地址为http://localhost:8761/eureka/,程序名称为 service-ribbon,程序端口为8764。配置文件application.yml如下:

     

    eureka:

      client:

        serviceUrl:

          defaultZone: http://localhost:8761/eureka/

    server:

      port: 8764

    spring:

      application:

        name: service-ribbon

    1

    2

    3

    4

    5

    6

    7

    8

    9

    在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。

     

    @SpringBootApplication

    @EnableDiscoveryClient

    public class ServiceRibbonApplication {

     

    public static void main(String[] args) {

    SpringApplication.run(ServiceRibbonApplication.class, args);

    }

     

    @Bean

    @LoadBalanced

    RestTemplate restTemplate() {

    return new RestTemplate();

    }

     

    }

     

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    写一个测试类HelloService,通过之前注入ioc容器的restTemplate来消费service-hi服务的“/hi”接口,在这里我们直接用的程序名替代了具体的url地址,在ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名,代码如下:

     

    @Service

    public class HelloService {

     

        @Autowired

        RestTemplate restTemplate;

     

        public String hiService(String name) {

            return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);

        }

     

    }

     

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    写一个controller,在controller中用调用HelloService 的方法,代码如下:

     

     

    /**

     * Created by fangzhipeng on 2017/4/6.

     */

    @RestController

    public class HelloControler {

     

        @Autowired

        HelloService helloService;

        @RequestMapping(value = "/hi")

        public String hi(@RequestParam String name){

            return helloService.hiService(name);

        }

     

     

    }

     

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    在浏览器上多次访问http://localhost:8764/hi?name=forezp,浏览器交替显示:

     

    hi forezp,i am from port:8762

     

    hi forezp,i am from port:8763

     

    这说明当我们通过调用restTemplate.getForObject(“http://SERVICE-HI/hi?name=”+name,String.class)方法时,已经做了负载均衡,访问了不同的端口的服务实例。

     

    四、此时的架构

     

     

    一个服务注册中心,eureka server,端口为8761

    service-hi工程跑了两个实例,端口分别为8762,8763,分别向服务注册中心注册

    sercvice-ribbon端口为8764,向服务注册中心注册

    当sercvice-ribbon通过restTemplate调用service-hi的hi接口时,因为用ribbon进行了负载均衡,会轮流的调用service-hi:8762和8763 两个端口的hi接口;

  • 相关阅读:
    ClickOnce發布經驗
    reporting Server組件不全引起的致命錯誤
    異步調用
    Usercontrol Hosted in IE
    MATLAB命令大全(转载)
    一种保护眼睛的好方法
    关于oracle自动编号
    An Algorithm Summary of Programming Collective Intelligence (1)
    An Algorithm Summary of Programming Collective Intelligence (3)
    An Algorithm Summary of Programming Collective Intelligence (4)
  • 原文地址:https://www.cnblogs.com/attilax/p/15197095.html
Copyright © 2011-2022 走看看