<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>
server:
port: 7001
eureka:
instance:
hostname: localhost // Eureka 服务端的实例名称
client:
register-with-eureka: false // false 表示不向注册中心注册自己
fetch-registry: false // false 表示自己端就是注册中心, 职责就是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ //设置与Eureka Server交互的地址,用于查询和注册服务
@SpringBootApplication @EnableEurekaServer public class EurekaServer7001Application { public static void main(String[] args) { SpringApplication.run(EurekaServer7001Application.class, args); } }
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
eureka:
client: // 客户端注册进Eureka服务列表内
service-url:
defaultZone: http://localhost:7001/eureka
instance:
instance-id: microservice-dept-provider
prefer-ip-address: true
@SpringBootApplication @EnableEurekaClient public class DeptProviderApplication { public static void main(String[] args) { SpringApplication.run(DeptProviderApplication.class, args); } }
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
instance:
instance-id: microservice-dept-provider // 自定义服务名称信息
prefer-ip-address: true // 访问路径显示IP地址
info: // 需要开启actuator : management.endpoints.web.exposure.include=‘*‘
app.name: microservicecloud
company.name: www.bearpx.com
build.artifactId: ${project.artifactId}
build.version: ${project.version}
<build> <finalName>microservicecloud</finalName> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <delimiters> <delimit>$</delimit> </delimiters> </configuration> </plugin> </plugins> </build>
@RestController @RequestMapping("/dept") public class DeptController { @Autowired private DeptService deptService; @Autowired private DiscoveryClient discoveryClient; @RequestMapping("/discovery") public Object discovery(){ List<String> list = discoveryClient.getServices(); System.out.println("------>" + list); List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances("microservicecloud-dept-provider"); for(ServiceInstance srvInst : serviceInstanceList){ System.out.println(srvInst.getServiceId() + "\t" + srvInst.getHost() + "\t"
+ srvInst.getPort() + "\t" + srvInst.getUri()); } return this.discoveryClient; } // 省略其他代码(add/get/list 方法) }
@SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient public class DeptProviderApplication { public static void main(String[] args) { SpringApplication.run(DeptProviderApplication.class, args); } }
------>[microservicecloud-dept-provider] MICROSERVICECLOUD-DEPT-PROVIDER 192.168.100.1 8001 http://192.168.100.1:8001
修改application.yml
eureka: client: service-url: defaultZone: http://localhost:7001/eureka/ instance: instance-id: microservice-dept-consumer prefer-ip-address: true
修改连接地址
@RestController public class DeptConsumerController { // private final static String REST_DEPT_URL_PREFIX ="http://localhost:8001"; private final static String REST_DEPT_URL_PREFIX ="http://MICROSERVICECLOUD-DEPT-PROVIDER"; }
修改主启动类
@SpringBootApplication @EnableEurekaClientpublic class DeptConsumerApplication {}
eureka:
instance:
hostname: eureka7001.com // 修改宿主机hosts文件: 127.0.0.1 eureka7001.com
client:
register-with-eureka: true
fetch-registry: false
service-url:
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
server:
enable-self-preservation: false
eureka:
instance:
hostname: eureka7002.com
client:
register-with-eureka: true
fetch-registry: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
server:
enable-self-preservation: false
eureka:
instance:
hostname: eureka7003.com
client:
register-with-eureka: true
fetch-registry: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
server:
enable-self-preservation: false
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
原文:https://www.cnblogs.com/kingdomer/p/14247129.html