Skip to content

Commit

Permalink
Eureka配置
Browse files Browse the repository at this point in the history
  • Loading branch information
gongxings committed Mar 30, 2019
1 parent e1b1ca5 commit ef0460d
Show file tree
Hide file tree
Showing 23 changed files with 821 additions and 111 deletions.
Binary file added images/CAP理论.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/eureka注册中心.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/eureka注册成功.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/eureka自我保护机制.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/eureka集群成功.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/eureka集群配置.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/hosts映射修改.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<module>springcloud-study-provider-dept-8001</module>
<module>springcloud-study-consumer-dept-80</module>
<module>springcloud-study-euraka-7001</module>
<module>springcloud-study-euraka-7002</module>
<module>springcloud-study-euraka-7003</module>
</modules>
<packaging>pom</packaging>
<properties>
Expand Down Expand Up @@ -76,4 +78,28 @@
</dependency>
</dependencies>
</dependencyManagement>

<build>
<!--finalName 父工程名称-->
<finalName>spring-cloud-study</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>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,11 @@ public List findAll(){
REST_URL_PREFIX+"/dept/findAll",
List.class);
}

@RequestMapping(value = "/consumer/dept/discovery")
public Object discovery(){
return restTemplate.getForObject(
REST_URL_PREFIX+"/dept/discovery",
Object.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.gxs.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
* @author GongXings
* @createTime 30 18:04
* @description
*/
@SpringBootApplication
/**
* 启用eureka服务,接收其他服务注册
*/
@EnableEurekaServer
public class EurekaServer7001App {
public static void main(String[] args) {
SpringApplication.run(EurekaServer7001App.class,args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ eureka:
register-with-eureka: false #false表示不向注册中心注册自己
fetch-registry: false #false 表示自己就是注册中心,职责就是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

# 单机版配置defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka #设置与eureka server 交互的地址查询服务和注册服务都需要依赖的地址
#集群配置
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
12 changes: 12 additions & 0 deletions springcloud-study-euraka-7001/target/classes/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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 交互的地址查询服务和注册服务都需要依赖的地址
#集群配置
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka
32 changes: 32 additions & 0 deletions springcloud-study-euraka-7002/pom.xml
Original file line number Diff line number Diff line change
@@ -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">

<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.gxs.springcloud</groupId>
<artifactId>spring-cloud-study</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>springcloud-study-euraka-7002</artifactId>

<dependencies>
<!--eureka-server 服务端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!--热部署 修改后立即生效-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.gxs.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
* @author GongXings
* @createTime 30 18:04
* @description
*/
@SpringBootApplication
/**
* 启用eureka服务,接收其他服务注册
*/
@EnableEurekaServer
public class EurekaServer7002App {
public static void main(String[] args) {
SpringApplication.run(EurekaServer7002App.class,args);
}
}
12 changes: 12 additions & 0 deletions springcloud-study-euraka-7002/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
server:
port: 7002
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 交互的地址查询服务和注册服务都需要依赖的地址
#集群配置
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
32 changes: 32 additions & 0 deletions springcloud-study-euraka-7003/pom.xml
Original file line number Diff line number Diff line change
@@ -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">

<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.gxs.springcloud</groupId>
<artifactId>spring-cloud-study</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>springcloud-study-euraka-7003</artifactId>

<dependencies>
<!--eureka-server 服务端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!--热部署 修改后立即生效-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.gxs.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
* @author GongXings
* @createTime 30 18:04
* @description
*/
@SpringBootApplication
/**
* 启用eureka服务,接收其他服务注册
*/
@EnableEurekaServer
public class EurekaServer7003App {
public static void main(String[] args) {
SpringApplication.run(EurekaServer7003App.class,args);
}
}
12 changes: 12 additions & 0 deletions springcloud-study-euraka-7003/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
server:
port: 7003
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 交互的地址查询服务和注册服务都需要依赖的地址
#集群配置
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
16 changes: 16 additions & 0 deletions springcloud-study-provider-dept-8001/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@
<artifactId>springcloud-study-api</artifactId>
<version>${project.version}</version>
</dependency>
<!--将微服务provider注册进eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.gxs.springcloud.entities.DeptEntity;
import com.gxs.springcloud.service.DeptService;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
Expand All @@ -20,6 +22,8 @@ public class DeptController {

@Autowired
private DeptService deptService;
@Autowired
private DiscoveryClient discoveryClient;

@RequestMapping(value = "/dept/add",method = RequestMethod.POST)
public boolean addDept(@RequestBody DeptEntity deptEntity) {
Expand All @@ -35,4 +39,27 @@ public List<DeptEntity> findAll() {
return deptService.findAll();
}

/**
* 增加自己服务描述的接口
* @return
*/
@RequestMapping(value = "/dept/discovery",method = RequestMethod.GET)
public Object discovery(){
List<String> list = discoveryClient.getServices();
System.out.println("总共有多少个微服务"+list.size());

//查询STUDY-SPRINGCLOUD-DEPT 服务
List<ServiceInstance> instances = discoveryClient.getInstances("STUDY-SPRINGCLOUD-DEPT");

//打印STUDY-SPRINGCLOUD-DEPT服务信息
for (ServiceInstance element :instances){
System.out.println(element.getServiceId());
System.out.println(element.getHost());
System.out.println(element.getPort());
System.out.println(element.getUri());
}
return this.discoveryClient;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
* @author GongXings
* @createTime 30 15:14
* @description
*/
@SpringBootApplication
//本服务启动后会自动注册进eureka服务
@EnableEurekaClient
@EnableDiscoveryClient
public class DeptProvider8001App {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,19 @@ mybatis:
configuration:
map-underscore-to-camel-case: true #开启驼峰命名
cache-enabled: true #开启二级缓存
#客户端注册进eureka服务列表
eureka:
client:
service-url:
defaultZone: eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
instance:
#服务实例名称修改
instance-id: study-springcloud-dept8001
#访问路径显示IP地址
prefer-ip-address: true


info:
app.name: study-springcloud-micoservices
company.name: www.gxs.com
build.artifactId: ${project.artifactId}
build.version: ${project.version}
Loading

0 comments on commit ef0460d

Please sign in to comment.