Service Governance Spring Cloud Eureka (a) the use of notes

Create a spring-cloud project

Building a Maven project: spring-cloud-demo
added to the pom.xml file:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.20.RELEASE</version>
</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>Dalston.SR4</spring-cloud.version>
    <spring-boot.version>1.5.20.RELEASE</spring-boot.version>
</properties>

<!--对子模块依赖包的版本统一控制,子模块需要显示引用,子模块也可以重新声明版本-->
<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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Build a service registry Eureka Service

Add module in spring-cloud-demo project: eureka-server
join rely on pom.xml file:

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

Create a file in the resource directory: application.properties, reads as follows:

################### Eureka服务端配置 ####################
server.port=8761
eureka.instance.hostname=localhost
#不向注册中心注册自己
eureka.client.register-with-eureka=false
#由于注册中心的职责就是维护服务实例,它并不需要去检索服务, 所以也设置为false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

Creating a startup class: EurekaServerApplication.java, add the following:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

Main method runs EurekaServerApplication class, thus Eureka build complete server
access Eureka server in the browser: http: // localhost: 8761 /

Service registration and discovery: Eureka build service registrants

Add module in spring-cloud-demo project: demo-service
added to rely in pom.xml file:

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

Create a file in the resource directory: application.properties, reads as follows:

########## Eureka客户端配置 ############
server.port=9806
spring.application.name=demo-service
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

Creating a startup class: DemoServiceApplication.java, add the following:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class DemoServiceApplication {

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

Main method runs DemoServiceApplication class, thus Eureka registrants to build complete
access Eureka server in the browser: http: // localhost: 8761 / , find demo-service already registered came

Adding a Controller: PortController.java, add the following:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PortController {

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

    @RequestMapping("/port")
    public String getPort() {
        return "I am demo-service, I'm from port : " + port;
    }
}

Re-run eureka-client project, the main method EurekaClientApplication class
visit in the browser: http: // localhost: 9806 / port, namely output: I am demo-service, I 'm from port: 9806

Availability registry

Create two profiles in the resource directory eureka-server project
application-peer1.properties

################### Eureka服务端配置 ####################
spring.application.name=eureka-server
server.port=8761
eureka.instance.hostname=peer1
#不向注册中心注册自己
eureka.client.register-with-eureka=false
#由于注册中心的职责就是维护服务实例,它并不需要去检索服务, 所以也设置为false
eureka.client.fetch-registry=false
#高可用配置:注册到其他配置中心
eureka.client.serviceUrl.defaultZone=http://peer2:8762/eureka/

application-peer1.properties

################### Eureka服务端配置 ####################
spring.application.name=eureka-server
server.port=8761
eureka.instance.hostname=peer2
#不向注册中心注册自己
eureka.client.register-with-eureka=false
#由于注册中心的职责就是维护服务实例,它并不需要去检索服务, 所以也设置为false
eureka.client.fetch-registry=false
#高可用配置:注册到其他配置中心
eureka.client.serviceUrl.defaultZone=http://peer1:8761/eureka/

Editing System C: \ Windows \ System32 \ hosts file in the drivers \ etc directory, add the following:

    127.0.0.1       peer1
    127.0.0.1       peer2

The eureka-server project labeled jar package, and then run eureka-server.jar named twice with java -jar and specify a different configuration file:

java -jar eureka-server-0.0.1.jar --spring.profiles.active=peer1
java -jar eureka-server-0.0.1.jar --spring.profiles.active=peer2

Background process

nohup java -jar eureka-server-0.0.1.jar --spring.profiles.active=peer1 >./log/eureka-1.log &

Then visit: http: // localhost: 8761 / , that is found under DS Replicas copy-items: peer2
then visit: http: // localhost: 8762 / , copy the item that is found under DS Replicas Item: peer1
upcoming registration center to register other Sign up centers to achieve the purpose of high availability registry

Service Registration Registration to Eureka cluster configuration:

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/,http://localhost:8762/eureka/

Guess you like

Origin www.cnblogs.com/yhongyin/p/11183842.html