package com.example.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
@SpringBootApplication
//扫描所有包
@ComponentScan("com.test")
//声明为注册服务
@EnableEurekaClient
//把调用注册子模块接口引入到Spring容器中(不加此注解会出现找不到@FeignClient修饰的接口)
@EnableFeignClients("com.test")//包路径解决启动类在别的包下问题
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
1、创建一个空的maven项目!
2、创建一个注册中心模块
3、配置注册中心
package com.example.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
//声明为注册中心
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
配置文件改用yml,配置如下:
server:
#运行端口
port: 8888
eureka:
instance:
#注册ip
hostname: localhost
client:
#禁止自己当做服务注册
register-with-eureka: false
#屏蔽注册信息
fetch-registry: false
#注册url
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
注意pom文件中springcloud与springboot的版本对应问题
启动成功后,访问本地+端口即可看到注册中心页面,说明成功啦!