SpringCloud:(一)服务注册与发现

最近跟着方志明老师学习SpringCloud,博客地址如下:

https://blog.csdn.net/forezp/article/details/81040925

自己也跟着撸了一遍,纸上得来终觉浅,绝知此事要躬行,以下是自己撸的时候的总结&注意事项:

(一)创建maven项目

打开idea

File—new—project—maven

创建maven项目

Pom文件导入相应服务的依赖

<?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>

<groupId>springcloud</groupId>
<artifactId>clouddemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<name>clouddemo</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/>
</parent>

<modules>
<module>eureka-server</module>
<module>service-hi</module>
</modules>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
注:
version版本升级到了2.1.1,不然会报

java.lang.AbstractMethodError: null at org.springframework.core.OrderComparator.findOrder等错误
<version>2.1.1.RELEASE</version>
(二)创建2个model工程作为服务注册中心:Eureka server & Eureka client

2.1Eureka server

2.1.1

父项目—new—module—spring initializr—cloud discovery—eureka server

 

继承父pom文件,引入spring—cloud—starter—Netflix—eureka—server的依赖

<?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>springcloud</groupId>
<artifactId>clouddemo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<groupId>com.springcloud</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>eureka-server</name>
<description>Demo project for Spring cloud</description>

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

</dependencies>

</project>
2.1.2
启动一个服务注册中心,需要一个注解@EnableEurekaServer,这个注解需要在springboot工程的启动application类上加:
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

public static void main(String[] args) {

SpringApplication.run(EurekaServerApplication.class, args);
}

}
2.1.3配置文件appication.yml,指定server:
server:
port: 8761

eureka:
instance:
hostname: localhost
client:
#通过eureka.client.registerWithEureka:false和fetchRegistry
#false来表明自己是一个eureka server
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

spring:
application:
name: eurka-server
完成以后,启动服务,打开启动工程,打开浏览器访问:

http://localhost:8761
页面,显示没有服务--没有注册服务当然不可能有服务被发现了。。。。

 


2.2 Eureka client--server-hi

步骤同2.1,创建module项目(服务提供者)

pom文件

<?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>springcloud</groupId>
<artifactId>clouddemo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<groupId>com.eureka.client</groupId>
<artifactId>service-hi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>service-hi</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

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


<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
2.2.1启动类添加注解,注解@EnableEurekaClient 表明自己是一个eurekaclient

@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceHiApplication {

public static void main(String[] args) {
SpringApplication.run( ServiceHiApplication.class, args );
}

@Value("${server.port}")
String port;

@RequestMapping("/hi")
public String home(@RequestParam(value = "name", defaultValue = "clouddemo") String name) {
return "hi " + name + " ,i am from port:" + port;
}

}


2.2.2在配置文件中注明自己的服务注册中心的地址

server:
port: 8762

spring:
application:
name: service-hi

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/

3.1启动eureka-server服务,打开 http://localhost:8761
3.2启动service-hi服务,访问service-hi页面,http://localhost:8762/hi?name=clouddemo

 



3.3刷新eureka-server访问的页面
服务已经注册在服务中了,服务名为SERVICE-HI ,端口为7862

 

posted @ 2019-04-24 09:41  Alice口袋有糖  阅读(553)  评论(0编辑  收藏  举报