栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringCloud之Eureka

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringCloud之Eureka

SpringCloud之Eureka的使用

文章目录
  • SpringCloud之Eureka的使用
  • 一、创建Eureka注册中心
    • 1.pom文件引入相关依赖
    • 2.yml文件配置
    • 3.编写启动类并且启动
    • 4.启动结果
  • 二、创建Eureka提供服务方
    • 1.pom文件引入相关依赖
    • 2.yml文件配置
    • 3.编写SpringBoot启动类
    • 4.启动后查看注册中心网页
    • 5.编写测试类进行测试
      • 1.测试类代码
      • 2.结果
  • 三、创建Eureka服务消费方
    • 1.pom文件引入相关依赖
    • 2.yml文件配置
    • 3.编写SpringBoot启动类
    • 4.启动后查看注册中心网页
    • 5.编写消费方代码
      • 1.消费方代码
      • 2.重新启动进行消费调用



一、创建Eureka注册中心 1.pom文件引入相关依赖



  4.0.0

  com.yun
  springcloud-euerka-service
  1.0-SNAPSHOT

  springcloud-euerka-service
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
  

  
  
    org.springframework.boot
    spring-boot-starter-parent
    1.5.7.RELEASE
  

  

    
      org.springframework.cloud
      spring-cloud-starter-netflix-eureka-server
    


    
      junit
      junit
      4.11
      test
    
  

  
  
    
      
        org.springframework.cloud
        spring-cloud-dependencies
        Edgware.SR5
        pom
        import
      
    
  


2.yml文件配置
server:
  port: 8700
eureka:
  client:
    service-url:
        defaultZone: http://${eureka.instance.hostname}:${server.port}/euerka
    register-with-eureka: false #自身,不在向euerka注册
    fetch-registry: false #启动时禁用client的注册

  instance:
    hostname: localhost
spring:
  application:
    name: eureka-server





3.编写启动类并且启动
package com.yun;

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


@SpringBootApplication
@EnableEurekaServer  //当前使用Euerka的 server
public class EuerkaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EuerkaServerApplication.class);
    }
}

4.启动结果


二、创建Eureka提供服务方 1.pom文件引入相关依赖



  4.0.0

  com.yun
  springcloud-euerka-servicesupport
  1.0-SNAPSHOT

  springcloud-euerka-servicesupport
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
  

  
  
    org.springframework.boot
    spring-boot-starter-parent
    1.5.7.RELEASE
  

  
    
    
      org.springframework.cloud
      spring-cloud-starter-netflix-eureka-client
    

    
      junit
      junit
      4.11
      test
    
  

  
  
    
      
        org.springframework.cloud
        spring-cloud-dependencies
        Edgware.SR5
        pom
        import
      
    
  


**注意此处应该是:spring-cloud-starter-netflix-eureka-client**
2.yml文件配置
server:
  port: 8701
eureka:
  client:
    service-url:
        defaultZone: http://${eureka.instance.hostname}:8700/eureka  #这个地址为注册中心地址
  instance:
    hostname: localhost

#当前服务名称
spring:
  application:
    name: eureka-service
3.编写SpringBoot启动类
package com.yun;

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


@SpringBootApplication
@EnableDiscoveryClient //代表自己是一个服务提供方,注意这里注解的区别
public class EuerkaServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(EuerkaServiceApplication.class);
    }
}

4.启动后查看注册中心网页
    这里可以看到已经注册成功

5.编写测试类进行测试 1.测试类代码
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/hello")
public class HelloController {
    @RequestMapping("/world")
    public String helloWorld(String s){
        System.out.println("传入的值:"+s);
        return "传入的值为:"+s;
    }
}
2.结果

三、创建Eureka服务消费方 1.pom文件引入相关依赖



  4.0.0

  com.yun
  springcloud-euerka-serviceconsume
  1.0-SNAPSHOT

  springcloud-euerka-serviceconsume
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
  
  
  
    org.springframework.boot
    spring-boot-starter-parent
    1.5.7.RELEASE
  

  
    
    
      org.springframework.cloud
      spring-cloud-starter-netflix-eureka-client
    

    
      junit
      junit
      4.11
      test
    

  

  
  
    
      
        org.springframework.cloud
        spring-cloud-dependencies
        Edgware.SR5
        pom
        import
      
    
  


2.yml文件配置
server:
  port: 8702 #服务消费方
eureka:
  client:
    service-url:
        defaultZone: http://${eureka.instance.hostname}:8700/eureka
  instance:
    hostname: localhost

#当前服务名称
spring:
  application:
    name: eureka-consumer
3.编写SpringBoot启动类
package com.yun;

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


@SpringBootApplication
@EnableDiscoveryClient  //当前使用eureka的server
public class EuerkaConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EuerkaConsumerApplication.class);
    }
}

4.启动后查看注册中心网页

5.编写消费方代码 1.消费方代码
package com.yun;

import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
@RequestMapping("/Hello")
public class ConsumerController {

    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private RestTemplate restTemplate; //这里使用Bean注入一下

    @RequestMapping("/consumer")
    public String helloWorld(String s){
        System.out.println("传入的值:"+s);
        String forObject3 =restTemplate.getForObject("http://EUREKA-SERVICE/hello/world?s="+s,String.class);
        return forObject3;
    }
}

package com.yun;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;


@Configuration
public class Beans {
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

2.重新启动进行消费调用

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1013820.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号