(Turn) micro service _ to create a simple registry Eureka

      Original Address: https: //www.cnblogs.com/lplshermie/p/9105329.html

Micro Services and distributed has become a very common technique, in order to keep pace with the times, recently started learning SpringCloud, start from Eureka. The two of them not be introduced, online instructions bunch, just open a search engine, enter keywords enough to understand it, and this paper is a very simple demo recording I follow the example set up eureka online registry, only records only for learning little guidance, the following direct start.

  A. Tools

  • IDE  :  Intellij IDEA  2018.1
  • JDK :  1.8
  • Maven :3.3.9

  II. Creating a master project maven

  ① Open IDEA, create a new project

  

  After selecting the Maven project

  

  Here's what not checked, direct the next step

  

  Click Next

  

  Then click Finish, you've created the project as shown below  

 

  III. Creating a Eureka-server module as a service registry

  ① Right-click the project created, New-> Module

   

  ② Select Spring Initializr, and then click Next

  

  ③ Input Module infos

  

  ④ Highlight Cloud Discovery, the right check Eureka Server

  

  ⑤ then all the way directly to the Finish created. After the creation of project structure shown in Fig.

  

  pom.xml items shown below

复制代码
<?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.norexis</groupId> <artifactId>eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>eureka-server</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <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.BUILD-SNAPSHOT</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-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> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
复制代码

  ⑦想要创建服务中心,只需要在项目的启动类Application.java上添加一个@EnableEurekaServer注解

  

  此外,还需要对eureka server进行一些配置,这里采用yml的形式,删除resources下的application.properties,创建application.yml

  

复制代码
server:
  port : 8761

eureka:
  instance:
    hostname : localhost
  client:
    registerWithEureka : false
    fetchRegistry : false
    serviceUrl:
      defaultZone : http://${eureka.instance.hostname}:${server.port}/eureka/
复制代码

  然后启动Application.java即可。

  至此,Eureka Server的创建完成,为了验证效果,可以打开浏览器,输入http://localhost:8761,即可看到eureka的注册中心界面

  

  可以看到此时Application里那里显示No instances available,因为此时还没有服务实例注册,我们需要创建一个服务实例

  三.创建Eureka Client服务实例

  ①创建Eureka Client的module过程同Eureka Server 的过程完全一样,就不再说明。

  ②创建的Eureka Client需要在Application启动类上添加注解 @EurekaDiscoveryClient

  

  ③同Server一样,创建一个新的yml配置文件,添加如下配置

  

复制代码
eureka:
  client:
    serviceUrl:
      defaultZone : http://localhost:8761/eureka/
server:
  port: 8762
spring:
  application:
    name : service-test
复制代码

  ④之后启动application,再次打开Eureka注册中心,可以看到已经成功注册服务,显示service-test

  

  ⑤在地址栏输入Http://localhost:8762/info?name=norexis,可以看到如下信息

  

  到这里,一个简单的eureka注册中心和服务实例均已完成

Guess you like

Origin www.cnblogs.com/hhhh2010/p/11100788.html