eureka案例demo演示

pull/254/head
xjs 3 years ago
parent 2015e88eec
commit 1879bbe504

@ -15,6 +15,7 @@
<module>netty-project</module> <module>netty-project</module>
<module>dubbo-project</module> <module>dubbo-project</module>
<module>java-spi</module> <module>java-spi</module>
<module>springcloud-project</module>
</modules> </modules>
<artifactId>xjs-study</artifactId> <artifactId>xjs-study</artifactId>

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>eureka</artifactId>
<groupId>com.xjs</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>Eureka客户端消费者</name>
<artifactId>eureka-client-consumer</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<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-web</artifactId>
</dependency>
</dependencies>
</project>

@ -0,0 +1,30 @@
package com.xjs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* Eureka
* @author xiejs
* @since 2022-05-24
*/
@SpringBootApplication
//@EnableEurekaClient //开启Eureka client
@EnableDiscoveryClient //开启注册中心客户端
//从springCloud的Edgware版本开始不加[@EnableDiscoveryClient]注解也行,建议加上
public class EurekaClientConsumerApp {
public static void main(String[] args) {
SpringApplication.run(EurekaClientConsumerApp.class, args);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

@ -0,0 +1,47 @@
package com.xjs.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.Map;
/**
* @author xiejs
* @since 2022-05-25
*/
@RestController
@RequestMapping("time")
public class TimeController {
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
private RestTemplate restTemplate;
@GetMapping
public String getTime() {
List<ServiceInstance> instances = discoveryClient.getInstances("EUREKA-CLIENT-PROVIDER");
ServiceInstance serviceInstance = instances.get(0);
String host = serviceInstance.getHost();
int port = serviceInstance.getPort();
//获取生产者定义的元数据
Map<String, String> metadata = serviceInstance.getMetadata();
String now = metadata.get("now");
System.out.println(now);
String url = "http://" + host + ":" + port + "/time";
return restTemplate.getForObject(url, String.class);
}
}

@ -0,0 +1,19 @@
#eureka server端口
server:
port: 8759
spring:
application:
name: eureka-client-consumer #应用名称在Eureka中作为服务名称
#注册到Eureka服务中心
eureka:
client:
service-url:
# 注册到集群就把多个Eureka Server 地址使用逗号连接起来即可,注册到单实例,就写一个
defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka,http://localhost:8763/eureka
instance:
prefer-ip-address: true #服务实例中显示ip而不是显示主机名
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port} #实例名称

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>eureka</artifactId>
<groupId>com.xjs</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>Eureka客户端生产者</name>
<artifactId>eureka-client-provider</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<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-web</artifactId>
</dependency>
</dependencies>
</project>

@ -0,0 +1,22 @@
package com.xjs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* Eureka
* @author xiejs
* @since 2022-05-24
*/
@SpringBootApplication
//@EnableEurekaClient //开启Eureka client
@EnableDiscoveryClient //开启注册中心客户端
//从springCloud的Edgware版本开始不加[@EnableDiscoveryClient]注解也行,建议加上
public class EurekaClientProviderApp {
public static void main(String[] args) {
SpringApplication.run(EurekaClientProviderApp.class, args);
}
}

@ -0,0 +1,20 @@
package com.xjs.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author xiejs
* @since 2022-05-25
*/
@RestController
@RequestMapping("time")
public class TimeController {
@GetMapping
public String getTime() {
return "2022-05-25 09:41:34";
}
}

@ -0,0 +1,23 @@
#eureka server端口
server:
port: 8760
spring:
application:
name: eureka-client-provider #应用名称在Eureka中作为服务名称
#注册到Eureka服务中心
eureka:
client:
service-url:
# 注册到集群就把多个Eureka Server 地址使用逗号连接起来即可,注册到单实例,就写一个
defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka,http://localhost:8763/eureka
instance:
prefer-ip-address: true #服务实例中显示ip而不是显示主机名
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port} #实例名称
#自定义元数据
metadata-map:
now: 2022

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>eureka</artifactId>
<groupId>com.xjs</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>Eureka服务端8761</name>
<artifactId>eureka-server-8761</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>

@ -0,0 +1,21 @@
package com.xjs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* eureka
* @author xiejs
* @since 2022-05-24
*/
@SpringBootApplication
//声明当前项目为为Eureka服务
@EnableEurekaServer
public class EurekaServerApp8761 {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApp8761.class, args);
System.out.println("localhost:8761");
}
}

@ -0,0 +1,21 @@
#eureka server端口
server:
port: 8761
spring:
application:
name: eureka-server #应用名称在Eureka中作为服务名称
#eureka客户端配置和Server交互 ,Eureka Server 其实也是一个Client
eureka:
instance:
hostname: localhost #当前eureka实例的主机名
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port} #实例名称
client:
service-url:
# 集群模式下 defaultZone应该指向其他 Eureka server
defaultZone: http://localhost:8762/eureka/,http://localhost:8763/eureka/
register-with-eureka: true #当前自己就是server不需要注册自己 集群模式可以改成true
fetch-registry: true #查询获取注册中心的服务信息自己就是Server不需要从Eureka server获取服务信息

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>eureka</artifactId>
<groupId>com.xjs</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>Eureka服务端8762</name>
<artifactId>eureka-server-8762</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>

@ -0,0 +1,21 @@
package com.xjs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* eureka
* @author xiejs
* @since 2022-05-24
*/
@SpringBootApplication
//声明当前项目为为Eureka服务
@EnableEurekaServer
public class EurekaServerApp8762 {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApp8762.class, args);
System.out.println("localhost:8762");
}
}

@ -0,0 +1,20 @@
#eureka server端口
server:
port: 8762
spring:
application:
name: eureka-server #应用名称在Eureka中作为服务名称
#eureka客户端配置和Server交互 ,Eureka Server 其实也是一个Client
eureka:
instance:
hostname: localhost #当前eureka实例的主机名
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port} #实例名称
client:
service-url:
defaultZone: http://localhost:8761/eureka/,http://localhost:8763/eureka/
register-with-eureka: true #当前自己就是server不需要注册自己
fetch-registry: true #查询获取注册中心的服务信息自己就是Server不需要从Eureka server获取服务信息

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>eureka</artifactId>
<groupId>com.xjs</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>Eureka服务端8763</name>
<artifactId>eureka-server-8763</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>

@ -0,0 +1,21 @@
package com.xjs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* eureka
* @author xiejs
* @since 2022-05-24
*/
@SpringBootApplication
//声明当前项目为为Eureka服务
@EnableEurekaServer
public class EurekaServerApp8763 {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApp8763.class, args);
System.out.println("localhost:8763");
}
}

@ -0,0 +1,20 @@
#eureka server端口
server:
port: 8763
spring:
application:
name: eureka-server #应用名称在Eureka中作为服务名称
#eureka客户端配置和Server交互 ,Eureka Server 其实也是一个Client
eureka:
instance:
hostname: localhost #当前eureka实例的主机名
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port} #实例名称
client:
service-url:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
# register-with-eureka: true #当前自己就是server不需要注册自己
# fetch-registry: true #查询获取注册中心的服务信息自己就是Server不需要从Eureka server获取服务信息

@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud-project</artifactId>
<groupId>com.xjs</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<name>Eureka注册中心</name>
<modules>
<module>eureka-server-8761</module>
<module>eureka-server-8762</module>
<module>eureka-server-8763</module>
<module>eureka-client-provider</module>
<module>eureka-client-consumer</module>
</modules>
<artifactId>eureka</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<!--引⼊Jaxb开始-->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<!--引⼊Jaxb结束-->
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
<version>3.0.4</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>xjs-study</artifactId>
<groupId>com.xjs</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<name>微服务组件</name>
<modules>
<module>eureka</module>
</modules>
<artifactId>springcloud-project</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
Loading…
Cancel
Save