zoukankan      html  css  js  c++  java
  • Spring Cloud微服务的服务治理组件eureka(三)

    目录

    1. 背景

    2. eureka服务消费者搭建

    3. 服务整合

     

    一、背景

      在eureka服务治理体系中,主要分为服务端和客户端两个不同的角色,服务端为服务注册中心,客户端为各个提供接口的微服务应用。

       客户端的配置主要分为两个方面:

    • 服务注册相关的配置信息:包括服务注册中心的地址,服务获取的时间间隔、可用区域等。
    • 服务实例相关的配置信息,包括服务实例的名称、地址、端口、健康检查路径等。

    二、eureka服务消费者搭建

      开发工具:idea

      1、File---> New ---> Project---->spring Initialzr  

     

    2、填写项目包名、项目名,继续NEXT

     3、选中Discovery Client和web模块、Next-->(选择项目目录)Finish

    4、项目生成后目录结构如下,其中controller、service、api为新建的包,其他文件及目录是默认生成的

     

     5、我们来看看pom.xml

    1 <dependency>
    2        <groupId>org.springframework.cloud</groupId>
    3        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    4 </dependency>
    5 <dependency>
    6         <groupId>org.springframework.boot</groupId>
    7         <artifactId>spring-boot-starter-web</artifactId>
    8 </dependency>

    6、启动类加上EnableDiscoveryClient注解,自动化配置为服务治理的客户端

      并注入restTemple,用于rest请求;

    @EnableDiscoveryClient
    @SpringBootApplication
    public class EurekaClientApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaClientApplication.class, args);
        }
    
      
      @Bean
      RestTemplate restTemplate(){
       return new RestTemplate();
      }
    }

    7、application.properities配置

    1 server.port=8089
    2 spring.application.name=eureka-ribbon
    #注册的服务中心的路径,这里注册了多个代表注册了一个服务中心集群 3 eureka.client.service-url.defaultZone=http://admin:123456@localhost:1111/eureka/,http://admin:123456@localhost:1112/eureka/ 4 #IP进行注册 5 eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port} 6 eureka.instance.preferIpAddress=true

     

    8、Controller提供服务请求入口,处理参数。

     1 @RestController
     2 public class SysController {
     3 
     4     @Autowired
     5     SysService sysService;
     6 
     7     @ResponseBody
     8     @RequestMapping("/hi")
     9     public String greetService(@RequestParam("service_name")String service_name){
    10         return sysService.hiService(service_name);
    11     }
    12 }

     

    service用于编写业务代码:

    eureka-client为服务实例
    @Service
    public class SysService {
    
        @Autowired
        RestTemplate restTemplate;
    
        public String hiService(String service_name){
            return restTemplate.getForObject("http://eureka-client/demo?name="+service_name,String.class);
        }
    
    }

    9、点击以下图标启动服务

     出现以下日志代码启动成功

    10、访问注册中心localhost:1111,如图就是我们的实例

    三、服务整合

    1. 启动注册中心集群、
    2. 启动eureka服务提供者集群
    3. 启动eureka服务消费者
    4. 访问http://localhost:8089/hi?service_name=%27sdas%27为消费者的入口访问eureka服务方的接口,如图,成功的返回eureka服务者提供的服务接口。

     

       

    << 返回页首 >>

     

     


    我是 啧啧啧花儿不谢呀! ,一只有理想,有故事的程序员,欢迎说出你的故事

  • 相关阅读:
    Redis基本数据结构总结之SET、ZSET和HASH
    C# .Net计算函数执行的时间
    千万级规模高性能、高并发的网络架构经验分享
    c#单元测试:使用Moq框架Mock对象
    spring bean的构造器参数
    Java并发编程:ThreadLocal
    redis过期策略
    JAVA线程的生命周期状态
    jdk1.8新特性之Optional
    对Java中interrupt、interrupted和isInterrupted的理解
  • 原文地址:https://www.cnblogs.com/flyPenguinblog/p/13278498.html
Copyright © 2011-2022 走看看