概述
2019独角兽企业重金招聘Python工程师标准>>>
一, 简介
spring cloud Finchley 官方文档: http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html
spring cloud 微服务的 重要环节就是 服务的注册于发现 , 而 服务注册中心组件就是 : Eureka
二, 创建 spring cloud 项目
1, 新建 Maven 项目 cloud, 作为 父包使用 :
pom.xml :
<?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>com.gy.cloud</groupId>
<artifactId>cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<modules>
<module>cloud-a</module>
<module>cloud-b</module>
<module>cloud-c</module>
<module>cloud-d</module>
<module>cloud-e</module>
<module>cloud-f</module>
<module>cloud-g</module>
<module>cloud-h</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>
2, 新建模块Module Spring Boot 项目 cloud-a, 作为服务的注册中心
只需引用Eureka服务包即可:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
1, cloud-a pom.xml:
<?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.gy.cloud</groupId>
<artifactId>cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>cloud-a</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>cloud-a</name>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
2, 注册中心配置: application.yml :
# http://localhost:8761/ 可访问 eureka server 界面
server:
port: 8761
eureka:
instance:
hostname: localhost # 服务IP
client:
registerWithEureka: false
fetchRegistry: false
# 注册地址
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
# 服务名称
spring:
application:
name: eurka-server
3, 注册中心启动类 CloudAApplication :
package com.gy.cloud.clouda;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class CloudAApplication {
public static void main(String[] args) {
SpringApplication.run(CloudAApplication.class, args);
System.out.println("=== 启动服务注册中心成功 ===");
}
}
5, 注册中心启动成功后访问: http://localhost:8761/
简单的注册中心就搭建完成;
3, 新建 客户端服务 cloud-b , 向整个项目提供服务 :
cloud-b pom.xml :
<?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.gy.cloud</groupId>
<artifactId>cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>cloud-b</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>cloud-b</name>
<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>
cloud-b application.yml :
server:
port: 8762
# 服务名称
spring:
application:
name: service-b
# 服务注册地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
cloud-b 启动类 CloudBApplication :
package com.gy.cloud.cloudb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableEurekaClient
@SpringBootApplication
public class CloudBApplication {
public static void main(String[] args) {
SpringApplication.run(CloudBApplication.class, args);
System.out.println("=== 服务B启动成功 === ");
}
@Value("${server.port}")
private String port;
@GetMapping("/hi")
public String home(String name) {
name = name == null ? "SERVICE-B" : name;
return "Hi " + name + " , I am from port: " + port;
}
}
服务B配置完成, 启动成功后,查看: http://localhost:8761/
SERVICE-B 就会注册到 Eureka 中 ;
学习文档
方志朋的博客 : https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f1-eureka/
项目源码: https://gitee.com/ge.yang/spring-demo/tree/master/cloud
转载于:https://my.oschina.net/u/3681868/blog/2999721
最后
以上就是清爽百褶裙为你收集整理的SpringCloud(Finchley版)1 - Eureka 注册中心一, 简介 二, 创建 spring cloud 项目学习文档的全部内容,希望文章能够帮你解决SpringCloud(Finchley版)1 - Eureka 注册中心一, 简介 二, 创建 spring cloud 项目学习文档所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复