建立eureka服务和客户端(客户端获取已经注册服务)
2024-10-19 03:38:59
1. 新建sping boot eureka server
新建立spring starter project
修改pom.xml文件
在parent后追加
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
追加
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
应用
package com.example; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication
@EnableEurekaServer
public class SpringBootEurekaServerApplication { public static void main(String[] args) {
SpringApplication.run(SpringBootEurekaServerApplication.class, args);
}
}
属性文件
server.port=8761 spring.application.name=Spring-Boot-Admin-Web
eureka.instance.hostname=localhost eureka.client.registerWithEureka=true
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
2. 建立spring boot eureka client
pom.xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
应用
package com.example; import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @SpringBootApplication
@RestController
@EnableEurekaClient
public class SpringBootEurekaClientApplication { @Autowired
private DiscoveryClient discoveryClient; @RequestMapping("/")
String home() {
return "Hello World!";
} @RequestMapping("/registered")
public String getRegistered(){
List<ServiceInstance> list = discoveryClient.getInstances("STORES");
System.out.println(discoveryClient.getLocalServiceInstance());
System.out.println("discoveryClient.getServices().size() = " + discoveryClient.getServices().size()); for( String s : discoveryClient.getServices()){
System.out.println("services " + s);
List<ServiceInstance> serviceInstances = discoveryClient.getInstances(s);
for(ServiceInstance si : serviceInstances){
System.out.println(" services:" + s + ":getHost()=" + si.getHost());
System.out.println(" services:" + s + ":getPort()=" + si.getPort());
System.out.println(" services:" + s + ":getServiceId()=" + si.getServiceId());
System.out.println(" services:" + s + ":getUri()=" + si.getUri());
System.out.println(" services:" + s + ":getMetadata()=" + si.getMetadata());
} } System.out.println(list.size());
if (list != null && list.size() > 0 ) {
System.out.println( list.get(0).getUri() );
}
return null;
} public static void main(String[] args) {
SpringApplication.run(SpringBootEurekaClientApplication.class, args);
}
}
配置文件
spring.application.name=cloud-simple-service
server.port=8011 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
最新文章
- 玩转spring boot——结合jQuery和AngularJs
- js加密的密文让PHP解密(AES算法)
- [Solution] Microsoft Windows 服务(1) C#创建Windows服务
- 04-树6 Complete Binary Search Tree
- Windows下搭建MySql Master-Master Replication
- 桂电在线-php-提取菜单到配置文件2
- 关于unsigned int和int的加法
- cf C. Matrix
- memcached介绍及基本使用
- spark install
- ubuntu系统安装FTP
- /home 和 /root
- js实现搜索记录列表
- vue中的$route和$router的区别
- Python协程(真才实学,想学的进来)
- 【嵌入式】Arduino编程基础到应用全解析
- 设计模式之备忘录模式(Memento )
- Goldbach`s Conjecture LightOJ - 1259 (素数打表 哥德巴赫猜想)
- C#连接数据库open函数失败
- Unity调用Windows弹框、提示框(确认与否,中文)