springcould eureka注册中心的创建及服务的提供和调用

目录

一.springcould eureka注册中心 搭建

1.项目结构

2.maven 依赖 pom.xml

3.配置文件application.yml

4.springboot  启动类,启动eureka服务注册中心

 二、服务提供者搭建

1.项目结构

2.maven 依赖 pom.xml

3.配置文件 application.yml

 4.spring boot 启动类 把服务注册到服务中心

5.controller 提交消费的hello方法

三、服务的消费者搭建

1.项目结构

2.maven 依赖 pom.xml

3.配置文件 application.yml

4.spring boot 启动类 启动消费者服务

5.feign调用实现

6.web层调用远程服务

四:测试


一.springcould eureka注册中心 搭建

1.项目结构

springcould eureka注册中心的创建及服务的提供和调用_第1张图片

2.maven 依赖 pom.xml

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0
    com.kyim
    spring-could-eurekaserver
    0.0.1-SNAPSHOT

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

    
        UTF-8
        UTF-8
        1.8
        Edgware.SR4
        
    

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

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            

        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            

        

    

3.配置文件application.yml

spring: 
  application:
    name: spring-cloud-eureka
server: 
  port:  9000
eureka: 
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl: 
      defaultZone: http://localhost:${server.port}/eureka/

#eureka.client.register-with-eureka :表示是否将自己注册到Eureka Server,默认为true。

#eureka.client.fetch-registry :表示是否从Eureka Server获取注册信息,默认为true。

#eureka.client.serviceUrl.defaultZone :设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。

默认是http://localhost:9000/eureka ;多个地址可使用 , 分隔。

4.springboot  启动类,启动eureka服务注册中心

package com.kyin.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaApplication {

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

}

启动工程后,访问:http://localhost:9000/,可以看到下面的页面,其中还没有发现任何服务

 二、服务提供者搭建

1.项目结构

springcould eureka注册中心的创建及服务的提供和调用_第2张图片

2.maven 依赖 pom.xml


  4.0.0
  com.kyin
  spring-could-provider
  0.0.1-SNAPSHOT
  
 
        org.springframework.boot
        spring-boot-starter-parent
        1.5.7.RELEASE
        
    

    
        UTF-8
        UTF-8
        1.8
        Edgware.SR4
        
    

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

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            

        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            

        

    

3.配置文件 application.yml

spring: 
  application:
    name: spring-cloud-provider
server: 
  port:  9001
eureka: 
  client:
    serviceUrl: 
      defaultZone: http://localhost:9000/eureka/


 4.spring boot 启动类 把服务注册到服务中心

package com.kyin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {

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

    }

}


5.controller 提交消费的hello方法

package com.kyin.controller;

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

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
        return "hello "+name+",this is first messge";
    }
}


三、服务的消费者搭建

1.项目结构

springcould eureka注册中心的创建及服务的提供和调用_第3张图片

2.maven 依赖 pom.xml


  4.0.0
  com.kyin
  spring-could-consumer
  0.0.1-SNAPSHOT
  
 
        org.springframework.boot
        spring-boot-starter-parent
        1.5.7.RELEASE
        
    

    
        UTF-8
        UTF-8
        1.8
        Edgware.SR4
        
    

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

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
           org.springframework.cloud
           spring-cloud-starter-feign
        

    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            

        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            

        

    

3.配置文件 application.yml

spring: 
  application:
    name: spring-cloud-consumer
server: 
  port:  9002
eureka: 
  client:
    serviceUrl: 
      defaultZone: http://localhost:9000/eureka/

4.spring boot 启动类 启动消费者服务

package com.kyin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient//启用服务注册与发现
@EnableFeignClients//启用feign进行远程调用

/**
 * Feign是一个声明式Web Service客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,
 *  然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。
 * Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。
 * Feign可以与Eureka和Ribbon组合使用以支持负载均衡。
 * @author Administrator
 */
public class ConsumerApplication {

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

}

5.feign调用实现

package com.kyin.feign;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
//name:远程服务名,及spring.application.name配置的名称
//此类中的方法和远程服务中contoller中的方法名和参数需保持一致。
@FeignClient(name= "spring-cloud-provider")
public interface HelloRemote {
    @RequestMapping(value = "/hello")
    public String hello(@RequestParam(value = "name") String name);
}


6.web层调用远程服务

package com.kyin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.kyin.feign.HelloRemote;
@RestController
public class ConsumerController {
    @Autowired
    HelloRemote HelloRemote;
    @RequestMapping("/hello/{name}")
    public String index(@PathVariable("name") String name) {
        return HelloRemote.hello(name);
    }
}

四:测试

依次启动spring-cloud-eurekaserver、spring-cloud-provider、spring-cloud-consumer三个项目

先输入:http://localhost:9001/hello?name=neo 检查spring-cloud-producer服务是否正常

返回:hello neo,this is first messge

说明spring-cloud-producer正常启动,提供的服务也正常。

浏览器中输入:http://localhost:9002/hello/neo

返回:hello neo,this is first messge

说明客户端已经成功的通过feign调用了远程服务hello,并且将结果返回到了浏览器。

 

三个项目启动成功后,可以在输入http://localhost:9000/进行查看

springcould eureka注册中心的创建及服务的提供和调用_第4张图片

你可能感兴趣的:(springcould eureka注册中心的创建及服务的提供和调用)