1.Spring Cloud Eureka 服务注册中心 Eureka原理 参考原文
2.项目工具及环境
maven下载资源包的时候会遇到无法下载的问题;更改maven镜像为国外的地址
3.开始创建项目
开始搭建
1.先打建一个最基础的maven项目,然后就自己next,再依次填写即可
2.删除最该maven项目下的src文件夹,新建moudel
新建spring boot项目
先不选择依赖
然后搭建项目基本目录
4.进行配置
1.. 配置项目的公共pom.xml
<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.0modelVersion>
<groupId>cn.qxgroupId>
<artifactId>eurekaartifactId>
<packaging>pompackaging>
<version>1.0-SNAPSHOTversion>
<modules>
<module>demo-server1module>
<module>demo-clent1module>
modules>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.0.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
<spring-cloud.version>Finchley.M8spring-cloud.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-cacheartifactId>
dependency>
<dependency>
<groupId>org.springframework.sessiongroupId>
<artifactId>spring-session-coreartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
<repositories>
<repository>
<id>spring-milestonesid>
<name>Spring Milestonesname>
<url>https://repo.spring.io/milestoneurl>
<snapshots>
<enabled>falseenabled>
snapshots>
repository>
repositories>
project>
2.. eureka-server的pom.xml文件的配置
<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>eurekaartifactId>
<groupId>cn.qxgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>demo-sever1artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
dependencies>
project>
3.. eureka-server的application.yml
server:
port: 8001
spring:
application:
name: demo-server1
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
#defaultZone: http://peer2:8002/eureka,http://peer3:8003/eureka
defaultZone: http://localhost:8001/eureka/
4.. eureka-server的启动注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class DemoServer1Application {
public static void main(String[] args) {
SpringApplication.run(DemoServer1Application.class, args);
}
}
配置好后启动即可。
5.eureka-client客户端,可以业务工程可以理解为服务提供者
<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>eurekaartifactId>
<groupId>cn.qxgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>demo-client1artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
project>
spring:
application:
name: demo-clent
server:
port: 9001
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
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;
@SpringBootApplication
@EnableEurekaClient
public class DemoClent1Application {
public static void main(String[] args) {
SpringApplication.run(DemoClent1Application.class, args);
}
}
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/info")
public String Hello(){
return "hello xxx,this is demo-client1 messge";
}
/*
服务提供者
*/
@RequestMapping("/producerHello")
public String Hello(@RequestParam("name") String name){
return "hello " + name + ",this is demo-client1 messge";
}
}
spring:
application:
name: demo-clent
server:
port: 9002
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
ss
<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>eurekaartifactId>
<groupId>cn.qxgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>demo-consumer1artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
project>
spring:
application:
name: demo-consumer1
server:
port: 7001
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class DemoConsumer1Application {
public static void main(String[] args) {
SpringApplication.run(DemoConsumer1Application.class, args);
}
}
//@EnableDiscoveryClient :启用服务注册与发现
//@EnableFeignClients:启用feign进行远程调用
eureka-consumer的feign调用实现
//name:远程服务名,即spring.application.name配置的名称
@Service
@FeignClient(name= "demo-clent")
public interface HelloRemote {
//需要匹配服务提供者接口名称
@RequestMapping(value = "/producerHello")
public String sayHello(@RequestParam(value="name") String name);
}
eureka-consumer的web层调用实现
@RestController
public class ConsumerController {
@Autowired
HelloRemote helloRemote;
/*
消费者的接口,去调用服务提供者
问题:只能使用@RequestMapping("/consumerHello/{name}") @PathVariable("name")方法 不知道有没有其它方式代替?
*/
@RequestMapping("/consumerHello/{name}")
public String index(@PathVariable("name") String name){
return helloRemote.sayHello(name);
}
//有问题的
// @RequestMapping("/consumerHello2")
// public String index2(@RequestParam("name") String name){
// return helloRemote.sayHello(name);
// }
@RequestMapping("/info")
public String info(){
return " Hi,I am a consumer!";
}
}
测试
分别启动DemoServer1,DemoClient1,DemoClient2,DemoConsumer工程
Eureka-Server集群的配置参考Eureka-Server集群
问题集合
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
ss