微服务没有在Eureka注册

时间:2016-08-25 09:18:27

标签: spring-boot netflix-eureka

我刚刚关注了以下文章https://spring.io/blog/2015/07/14/microservices-with-spring 但我不能让我的微服务在eureka注册。

客户service.yml

# Spring properties
spring:
  application:
     name: customer-service
  data:
    mongodb:
      host: 192.168.99.100
      port: 32768
      uri: mongodb://192.168.99.100:32768
      database: customer
      repositories:
        enabled: true

# Discovery Server Access
eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:1111/eureka/

# HTTP Server
server:
  port: 2222   # HTTP (Tomcat) port

build.gradle for customer-service

group 'com.company'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.5

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web:1.4.0.RELEASE'
    compile 'org.springframework.data:spring-data-mongodb:1.9.2.RELEASE'
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

CustomerService.java

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class CustomerService {

    @Autowired
    CustomerRepository repository;

    @RequestMapping("/")
    @ResponseBody
    List<Customer> get() {
        return repository.findAll();
    }


    @RequestMapping(value = "/", method = RequestMethod.POST)
    void post(@RequestBody @Valid Customer customer, BindingResult bindingResult, Model model) {
        repository.save(customer);
    }

    public static void main(String[] args) {
        // Will configure using accounts-server.yml
        System.setProperty("spring.config.name", "customer-service");

        SpringApplication.run(CustomerService.class, args);
    }
}

登记-server.yml

# Configure this Discovery Server
eureka:
  instance:
    hostname: 127.0.0.1
  client:  # Not a client, don't register with yourself
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

server:
  port: 1111   # HTTP (Tomcat) port

1 个答案:

答案 0 :(得分:0)

对于可能遇到同样问题的其他人,该服务的build.gradle应如下所示。

version '1.0-SNAPSHOT'

buildscript {
    ext {
        springBootVersion = '1.4.0.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'


sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencyManagement {
    imports {
        mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Brixton.RELEASE'
    }
}
dependencies {
    compile('org.springframework.cloud:spring-cloud-starter-eureka')
    compile 'org.springframework.boot:spring-boot-starter-web:1.4.0.RELEASE'
    compile 'org.springframework.data:spring-data-mongodb:1.9.2.RELEASE'

    testCompile group: 'junit', name: 'junit', version: '4.11'
}
相关问题