springboot eureka 入门


1.eureka服务器端

2.eureka客户端

3.客户端间的通信

1.eureka服务器端

 1.引入依赖

2.eureka配置

3.启动,加注解

4.浏览器打开:

localhost:6868/

<?xml version="1.0" encoding="UTF-8"?>

         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0
    com.ligy
    eurekademo1
    0.0.1-SNAPSHOT
    eurekademo1
    Demo project for Spring Boot
    
        1.8
        UTF-8
        UTF-8
        2.3.7.RELEASE
        Hoxton.SR9
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
        
    
    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
    

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

server.port=6868
#eureka配置
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:${server.port}/eureka/
package com.ligy.demo;

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

@SpringBootApplication
@EnableEurekaServer
public class DemoApplication {

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

}
package com.ligy.demo.controller;

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

@RestController
@RequestMapping("/student")
public class StudentController {
    @RequestMapping("/test")
    public String test() {
        return "你好,世界";
    }
}

 

2.eureka客户端

 1.引入依赖

2.eureka配置

3.启动,加注解

4.浏览器打开:

localhost:6868/

<?xml version="1.0" encoding="UTF-8"?>

         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0
    com.ligy
    eurekademo2
    0.0.1-SNAPSHOT
    eurekademo2
    Demo project for Spring Boot
    
        1.8
        UTF-8
        UTF-8
        2.3.7.RELEASE
        Hoxton.SR9
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
        
    
    
        
            org.springframework.boot
            spring-boot-starter
        

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

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

server.port=8085
spring.application.name=eurekademo2
#eureka配置
#eureka.client.registerWithEureka=false
#eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:6868/eureka/
eureka.instance.prefer-ip-address=true
server.port=8086
spring.application.name=eurekademo3
#eureka配置
#eureka.client.registerWithEureka=false
#eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:6868/eureka/
eureka.instance.prefer-ip-address=true
server.port=8087
spring.application.name=eurekademo4
#eureka配置
#eureka.client.registerWithEureka=false
#eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:6868/eureka/
eureka.instance.prefer-ip-address=true
package com.ligy.demo;

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

@SpringBootApplication
@EnableEurekaClient
public class DemoApplication {

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

}

 

3.客户端间的通信

1.注解

@EnableDiscoveryClient
@EnableFeignClients

2.新增接口

package com.ligy.demo.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient("eurekademo2")
public interface Demo2Serive {

    @RequestMapping("/demo2/test")
    String test();
}

3.新增控制器方法

package com.ligy.demo.controller;

import com.ligy.demo.service.Demo2Serive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/demo4")
public class StudentController {
    @Autowired
    Demo2Serive demo2Serive;

    @RequestMapping("/demo2test")
    public String demo2test() {

        return demo2Serive.test();
    }

    @RequestMapping("/test")
    public String test() {
        return "你好,世界demo4";
    }
}


package com.ligy.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class DemoApplication {

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

}