码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Cloud入门程序——注册服务提供者

时间:2018-07-13 20:47:55      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:9.png   mapping   framework   nat   ada   line   out   使用   eth   

1、创建Spring Starter project

 

2、引入依赖

技术分享图片

 点击finish

技术分享图片

 

 

3、创建启动类

package com.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/***
 * 
 * @EnableDiscoveryClient
 * 让服务使用eureka服务器
 * 实现服务注册和发现
 *
 */
@EnableDiscoveryClient
@SpringBootApplication
public class Springmvc012HelloServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springmvc012HelloServiceApplication.class, args);
    }
}

 4、创建controller

 

package com.hello;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    
    @Autowired
    DiscoveryClient discoveryClient;
    
    @RequestMapping("/hello")
    public String hello() {
        List<ServiceInstance> list = discoveryClient.getInstances("STORES");
        
        System.out.println("discoveryClient.getServices().size() = " + discoveryClient.getServices().size());
        
        for( String s :  discoveryClient.getServices()){
            System.out.println("services " + s);
            List<ServiceInstance> serviceInstances =  discoveryClient.getInstances(s);
            for(ServiceInstance si : serviceInstances){
                System.out.println("    services:" + s + ":getHost()=" + si.getHost());
                System.out.println("    services:" + s + ":getPort()=" + si.getPort());
                System.out.println("    services:" + s + ":getServiceId()=" + si.getServiceId());
                System.out.println("    services:" + s + ":getUri()=" + si.getUri());
                System.out.println("    services:" + s + ":getMetadata()=" + si.getMetadata());
            }
            
        }
        
        System.out.println(list.size());
        if (list != null && list.size() > 0 ) {
            System.out.println( list.get(0).getUri()  );
        }

        return "hello,this is hello-service";
        
    }

}

 

5、配置项目的 application.properties

server.port=9090
#设置服务名
spring.application.name=hello-service
#设置服务注册中心的URL,本服务要向该服务注册中心注册自己
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka

 

 

6、浏览器访问

 

(1)访问服务提供者项目 的请求 /hello:

技术分享图片

控制台打印:

技术分享图片

 

 (2)访问服务注册中心:

之前:

技术分享图片

之后,再次访问localhost:1111,发现有服务注册到注册中心了

技术分享图片

 

 7、总结:

 服务治理可以说是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册发现

Spring Cloud Eureka是Spring Cloud Netflix 微服务套件的一部分,主要负责完成微服务架构中的服务治理功能。

本文通过简单的小例子来分享下如何通过Eureka进行服务治理:

  • 搭建服务注册中心
  • 注册服务提供者(本文)
  • 服务发现和消费

实践终极目标:手把手,以上实例实现了基本的服务治理:

  • 通过spring-cloud-starter-eureka-server和@EnableEurekaServer实现服务注册中心
  • 通过spring-cloud-starter-eureka和@EnableDiscoveryClient使用并注册到服务注册中心
  • 通过spring-cloud-starter-eureka和@EnableDiscoveryClient使用注册中心并发现服务,通过spring-cloud-starter-ribbon来实现负载均衡消费服务

Donate捐赠

如果我的文章帮助了你,可以赞赏我 1 元给我支持,让我继续写出更好的内容)

技术分享图片   技术分享图片

  (微信)                                        (支付宝)

微信/支付宝 扫一扫

Spring Cloud入门程序——注册服务提供者

标签:9.png   mapping   framework   nat   ada   line   out   使用   eth   

原文地址:https://www.cnblogs.com/moonsoft/p/9307270.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!