SpringCloud服务注册

SpringCloud服务发现Eureka

    • Spring Cloud Netflix的简介
    • 核心代码
      • 创建eureka-server项目
        • 依赖注入
        • 配置文件application.properties
        • 创建启动类
      • 创建项目eureka-client
        • 导入依赖
        • 添加配置文件
        • 创建启动类
        • controller层
      • 创建测试项目(客户端)
        • 依赖注入
        • 配置文件
        • 启动类
        • controller层

Spring Cloud Netflix的简介

Spring Cloud Netflix项目是Spring Cloud的子项目之一,主要内容是对Netflix公司一系列开源产品的包装,它为Spring Boot应用提供了自配置的Netflix OSS整合。通过一些简单的注解,开发者就可以快速的在应用中配置一下常用模块并构建庞大的分布式系统。它主要提供的模块包括:服务发现(Eureka),断路器(Hystrix),智能路由(Zuul),客户端负载均衡(Ribbon)等。
SpringCloud Eureka的两个重要模块及其功能:

  1. eureka-server:服务注册中心提供服务注册功能
  2. eureka-client:注册服务到服务注册中心

核心代码

创建eureka-server项目

依赖注入

	<dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eureka-serverartifactId>
        dependency>
    dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Dalston.SR1version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

配置文件application.properties

#指定端口号
server.port=9001
#指定服务名称,一般与项目名称相同
spring.application.name=eureka-server
eureka.instance.hostname=localhost
#不让自己注册自己
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

创建启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer //启用Eureka服务
public class Eureka {
    public static void main(String[] args) {
        SpringApplication.run(Eureka.class,args);
    }
}

运行结果如下:
SpringCloud服务注册_第1张图片

创建项目eureka-client

导入依赖

与上面一样的依赖

添加配置文件

server.port=9002
spring.application.name=eureka-client1
eureka.client.service-url.defaultZone=http://localhost:9001/eureka/

创建启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class Client1Application {
    public static void main(String[] args) {
        SpringApplication.run(Client1Application.class,args);
    }
}

运行结果:
SpringCloud服务注册_第2张图片

controller层

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ClientController {
    @RequestMapping(value = "/clent1",method = RequestMethod.GET)
    public Object getInfo(){
        return "aaaaaaaaa";
    }
}

创建测试项目(客户端)

依赖注入

	<parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.1.RELEASEversion>
    parent>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Finchley.M7version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>
    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>

    dependencies>
    
    <repositories>
        <repository>
            <id>spring-milestonesid>
            <name>Spring Milestonesname>
            <url>https://repo.spring.io/libs-milestoneurl>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        repository>
    repositories>

配置文件

server.port=9003
spring.application.name=euraka-consumer
eureka.client.service-url.defaultZone=http://localhost:9001/eureka/

启动类


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableEurekaClient
@SpringBootApplication
public class ConsumerApplication {

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

    @Bean
    @LoadBalanced//负载均衡,提供服务的eurekaServer压力较大,需要使用负载均衡
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

controller层

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@CrossOrigin
public class ConsumerController {

    @Autowired//远程调用的工具类
    private RestTemplate restTemplate;

    @RequestMapping("/consumer")
    public Object getInfo(){
        //不是自己返回数据,而是调用其他工程服务,得到数据再返回出去http://其他工程配置文件中的spring.application.name/controller层路径
        String forObject = restTemplate.getForObject("http://EUREKA-CLIENT1/clent1", String.class);
        System.out.println("远程调用数据得到的数据是"+forObject);
        return forObject;
    }
}

运行结果:
SpringCloud服务注册_第3张图片

你可能感兴趣的:(框架)