Spring Cloud服务注册与发现

Spring Cloud服务注册与发现,需要使用到Eureka组件。
1.创建maven主工程
创建maven主工程springcloud-demo,在pom文件中引入相关依赖,Spring Boot版本为2.0.3.RELEASE,Spring Cloud版本为Finchley.RELEASE,此pom文件作为整个项目的父pom文件,具有依赖版本控制的作用,其他module工程继承此pom。pom文件代码如下:




  4.0.0

  com.wunian.springcloud
  springcloud-demo
  1.0-SNAPSHOT
  pom

  springcloud-demo Maven Webapp

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

  
    eureka-server
    service-hi
  

  
    UTF-8
    1.8
    1.8
    Finchley.RELEASE
  

  
    
      junit
      junit
      4.11
      test

    
    
      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
        
      
    
  

2.创建两个module工程
一个module工程作为Eureka Server,另一个作为Eureka Client,在Intellij IDEA中分别创建SpringBoot工程分别为eureka-serverservice-hi
Eureka Server中需要引入spring-cloud-starter-netflix-eureka-server依赖,pom文件代码如下:



    4.0.0

    com.wunian.server
    eureka-server
    0.0.1-SNAPSHOT
    jar

    eureka-server
    Demo project for Spring Boot

    
        com.wunian.springcloud
        springcloud-demo
        1.0-SNAPSHOT
    

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

Eureka Client中需要引入spring-cloud-starter-netflix-eureka-client依赖,pom文件代码如下:



    4.0.0
    
        com.wunian.springcloud
        springcloud-demo
        1.0-SNAPSHOT
    
    com.wunian.client
    service-hi
    0.0.1-SNAPSHOT
    service-hi
    Demo project for Spring Boot

    
        1.8
    

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

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

3.启动服务注册中心
在Spring Boot工程eureka-server的启动类上添加@EnableEurekaServer注解即可,代码如下:

@SpringBootApplication
@EnableEurekaServer//启用服务注册中心
public class EurekaServerApplication {

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

4.配置Eureka Server
在默认情况下Eureka Server也是一个Eureka Client ,因此必须指定一个 Server。eureka-server的配置文件appication.yml如下:

server:
  port: 8081
eureka:
  instance:
    hostname: localhost
  client:
    #通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/  #eurekaclient中注册的地址需配置成此
spring:
  application:
    name: eureka-server #服务与服务之间相互调用一般都是根据这个name

配置完成后,启动工程打开浏览器,访问:http://localhost:8081,可以看到eureka server的可视化界面。
5.启动Eureka Client
service-hi的启动类上,通过@EnableEurekaClient注解表明自己是一个Eureka Client,代码如下:

@SpringBootApplication
@EnableEurekaClient //表明自己是一个eurekaclient
@RestController  //@Controller和@ResponseBody注解的组合
public class ServiceHiApplication {

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

    @Value("${server.port}")
    String port;

    //http://localhost:8082/hi?name=jack
    @RequestMapping("/hi")
    public String home(@RequestParam(value = "name",defaultValue = "wunian")String name){
        return "Hello "+name+",I am from port:"+port;
    }
}

6.配置Eureka Client
仅凭@EnableEurekaClient注解还不够,还需在配置文件中配置自己的服务注册中心的地址,application.yml文件如下:

server:
  port: 8082
spring:
  application:
    name: service-hi  #服务与服务之间相互调用一般都是根据这个name
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8081/eureka/   #注册地址与eureka-server中配置的要一致

启动工程,在浏览器中访问:http://localhost:8081,可以发现已经有一个名叫SERVICE-HI,端口为8082的服务已经注册进服务中了,此时在浏览器中访问:http://localhost:8082/hi?name=wunian,可以在浏览器中看到如下信息:

Hello wunian,I am from port:8082

你可能感兴趣的:(Spring Cloud服务注册与发现)