springboot integrated eureka ultra-detailed configuration

Original link: https://blog.csdn.net/nanbiebao6522/article/details/80574463

Not repeat create the wheel, the following is the original:

Foreword

1. What is Eureka, what is the service registration and discovery


      Spring Boot as the most popular web framework. So what is it associated with Eureka have it?

Eureka open source Netflix is a RESTful service, mainly used for registration service discovery.
Eureka consists of two components: Eureka Eureka server and the client. Eureka server as a service registration server.
Eureka is a client java client, to simplify the interaction with the server load balancer as polling, to provide failover for the service and support.
Netflix used in its production environment is another client that weighted based traffic load balancing, resource utilization and error status.


2, create a Eureka-Server service registry


    
    Here need to use spring-cloud module of Eureka, he is a service registration and discovery module
      shown in our first new project to introduce a Spring-boot Eureka Server


        


Next>>>>Finish完成

Let's look at pom.xml build the code of good 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>
    <groupId>com.eureka</groupId>
    <artifactId>server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>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.RC2</spring-cloud.version>
    </properties>
    <dependencies>
        <!-- 引入的Eureka-server -->
        <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </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-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

    We see here an ordinary Spring-boot program is different, here it quoted a Eureka-Server package.
    So how do we use it, how does it start?

     这里只需要启动一个注解就可以啦,我们在Spring-Boot工程的启动类上加>>>>>>
    @EnableEurekaServer
    代码如下:
  


package com.eureka.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
 * 启动一个服务注册中心
 */
@EnableEurekaServer
@SpringBootApplication
public class ServerApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
    }
}

   
差点忘了,我们还需要配置application.yml

Eureka是一个高可用的组件,每一个实例注册之后需要向注册中心发送心跳包,在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。

eureka server的配置文件appication.yml:

server:
  port: 8081 #服务注册中心端口号
eureka:
  instance:
    hostname: 127.0.0.1 #服务注册中心IP地址
  client:
    registerWithEureka: false #是否向服务注册中心注册自己
    fetchRegistry: false #是否检索服务
    serviceUrl: #服务注册中心的配置内容,指定服务注册中心的位置
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/


我们来启动一下吧

我们在浏览器上输入http://127.0.0.1:8081

我们可以看到它的可视化界面

细心的朋友会发现,这里没有发现服务???No instance available

why?  因为我们还没有服务向注册中心注册服务,所以找不到啊。

3,先创建一个Eureka-Client客户端也就是服务提供者


        客户端在向注册中心它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从    每        个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。       

    创建客户端和服务端差不多,只是启动注解有点不一样,还有yml配置文件

  

Next>>>Finish完成啦

打开会发现pom.xml其实和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>
    <groupId>com.eureka</groupId>
    <artifactId>provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>provider</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.RC2</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>


怎么证明它是Client呢
很简单
在Spring-boot的启动类上通过注解@EnableEurekaClient 表明自己是一个eurekaclient.


 

package com.eureka.provider;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
 * Eureka客户端
 */
@RestController
@EnableEurekaClient
@SpringBootApplication
public class ProviderApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
 
 
    /**
     * 假如这个客户端要提供一个getUser的方法
     * @return
     */
    @GetMapping(value = "/getUser")
    @ResponseBody
    public Map<String,Object> getUser(@RequestParam Integer id){
        Map<String,Object> data = new HashMap<>();
        data.put("id",id);
        data.put("userName","admin");
        data.put("from","provider-A");
        return data;
    }
 
}


虽然加好了@EnableEurekaClient,总感觉差点什么,对了,配置文件yml

eureka:
  client:
    serviceUrl: #注册中心的注册地址
      defaultZone: http://127.0.0.1:8081/eureka/
server:
  port: 8082  #服务端口号
spring:
  application:
    name: service-provider #服务名称--调用的时候根据名称来调用该服务的方法


我们来启动看看吧


我们看到这个客户端已经向注册中心注册服务了,那么我们打开Eureka-server

我们看到我们启动的服务是不是加进去了呢

我们看到我们的服务是不是加进去了呢。

那么有人会问,那一大堆飙红的什么意思啊。因为注册的服务都是高可用的,这里只检测到一个服务,产生的预警,不影响使用,等下我们启动多个实例就不会了。

我们先来测试下客户端的方法是否可用   


显然是没有问题,那么我们提供好了服务,sei来消费呢?

下面我们就来建立一个消费者

为了更简单易懂,我还是一步一步出图吧。


来,贴上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.eureka</groupId>
    <artifactId>consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>consumer</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.RC2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>


</project>


主要是启动类,里面内容就丰富啦,都在注释里

package com.eureka.consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * Eureka客户端-消费者
 */
@RestController
@EnableEurekaClient
@SpringBootApplication
public class ConsumerApplication {

    @Autowired
    RestTemplate restTemplate;

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

    /**
     * 实例化RestTemplate
     * @return
     */
    @LoadBalanced
    @Bean
    public RestTemplate rest() {
        return new RestTemplate();
    }

    /**
     * Rest服务端使用RestTemplate发起http请求,然后得到数据返回给前端----gotoUser是为了区分getUser怕小伙伴晕头
     * @param id
     * @return
     */
    @GetMapping(value = "/gotoUser")
    @ResponseBody
    public Map<String,Object> getUser(@RequestParam Integer id){
        Map<String,Object> data = new HashMap<>();
        /**
         * 小伙伴发现没有,地址居然是http://service-provider
         * 居然不是http://127.0.0.1:8082/
         * 因为他向注册中心注册了服务,服务名称service-provider,我们访问service-provider即可
         */
        data = restTemplate.getForObject("http://service-provider/getUser?id="+id,Map.class);
        return data;
    }


}

配置文件

eureka:
  client:
    serviceUrl: #注册中心的注册地址
      defaultZone: http://127.0.0.1:8081/eureka/
server:
  port: 8083  #服务端口号
spring:
  application:
    name: service-consumer #服务名称--调用的时候根据名称来调用该服务的方法

我们启动看看效果吧

看看我们的提供者和消费者是不是都进来了


那么我们看看我们消费者的方法是否可用 

哈哈,是不是很神奇

下面介绍个更神奇的东西--实现微服务负载均衡


我们把服务提供者复制一个工程出来,我们再做下小小的修改,看看是否能实现负载均衡。


我们需要修改两个文件

一个是启动类,改了哪些呢?看看就晓得咯

package com.eureka.provider;

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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Eureka客户端
 */
@RestController
@EnableEurekaClient
@SpringBootApplication
public class ProviderApplication {

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


    /**
     * 假如这个客户端要提供一个getUser的方法
     * @return
     */
    @GetMapping(value = "/getUser")
    @ResponseBody
    public Map<String,Object> getUser(@RequestParam Integer id){
        Map<String,Object> data = new HashMap<>();
        data.put("id",id);
        data.put("userName","admin");
        data.put("from","provider-B");//改这里是为了让大家更能理解它负载均衡的机制
        return data;
    }

}

还有就是yml配置文件

eureka:
  client:
    serviceUrl: #注册中心的注册地址
      defaultZone: http://127.0.0.1:8081/eureka/
server:
  port: 8088  #服务端口号--该端口不要冲突
spring:
  application:
    name: service-provider #服务名称--调用的时候根据名称来调用该服务的方法--名字绝对不能改,改了就访问不到了

我们来启动一下吧

看看Eureka-server后台的效果    ServerA      ServerB    


这个叫做Service-provider是不是有两个实例啊
那么,我们分别访问一下,看看效果怎么样

看到了吗,8082端口,from是provider-A,8088端口,from是provider-B.

 


那么我们访问消费者的服务器看看会出现什么样的情况呢  飞机直达


一开始是from A,你刷新一下,诶? 变成 from B了。
说明这个时候两台提供者在交替工作,从而达到了一个负载均衡的作用。

来来来,我给你画个图

Each micro-services are a Eureka-Client, we put each app (SpringBootApplication) are registered with a service registry.
Sometimes, a relatively large amount of work and services, we can register more than a few micro service with the same name, so that they work alternatively, reduce the pressure on the individual services.


I write to you to end slightly.

Also go to work tomorrow.

There are places where wrong, we hope to help correct me.

I also learned on the road.
--------------------- 
Author: TIny1995 
Source: CSDN 
Original: https: //blog.csdn.net/nanbiebao6522/article/details/80574463 
Disclaimer: This article as a blogger original article, reproduced, please attach Bowen link!

Original Address: https: //my.oschina.net/java1314/blog/3023108

Guess you like

Origin www.cnblogs.com/jpfss/p/11314673.html