Spring Cloud (two) configuration Eureka Client

The foregoing review:

Spring Cloud (a) Eureka Server- monomer and Cluster Setup

In this section we will create two Eureka Client, registered on the section of Eureka Server, one as a service provider, a service as the caller.

A. Service provider (producer)

Add depend in 1.pom

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/>
    </parent>
​
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.RELEASE</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-client</artifactId>
            <version>2.1.0.RELEASE</version>
        </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>

2.Application startup class annotate

  • @EuableDiscoveryClient: auto-discovery is a client implementation

@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProducerApplication.class, args);
    }
}

3. Profiles

spring.application.name=spring-cloud-producer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

4. Provide Services

@RestController
public class HelloController {
​
    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
        return "hello " + name + ",welcome to Spring Cloud";
    }
}

II. Caller to the service (consumer)

Add depend in 1.pom

   
 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yfy</groupId>
    <artifactId>consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>consumer</name>
    <description>Demo project for Spring Boot</description>
​
    <properties>
        <java.version>1.8</java.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-client</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2.Application startup class annotate

  • @EnableDiscoveryClient : Enable service registration and discovery

  • @EnableFeignClients: Enable feign remote calls

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

3. Profiles

spring.application.name=spring-cloud-consumer
server.port=9001
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
feign.hystrix.enabled=true

4.Feign call implementation

@FeignClient(name= "spring-cloud-producer",fallback = HelloRemoteHystrix.class)
public interface HelloRemote {
​
    @RequestMapping(value = "/hello")
    String hello(@RequestParam(value = "name") String name);
}

5.web layer calls a remote service

@RestController
public class HelloController {

    @Autowired
    HelloRemote helloremote;

    @RequestMapping("/hello/{name}")
    public String index(@PathVariable("name") String name) {
        return helloremote.hello(name);
    }
}

III. Test

Start turn spring-cloud-eureka, spring-cloud-producer, spring-cloud-consumer three projects

Browser, type:http://localhost:9001/hello/yfy

return:hello yfy,welcome to Spring Cloud

4. Load balancing test

The controller method modifies producer

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
        return "hello " + name + ",welcome to Spring Cloud:product2";
    }
}

Then start a producer, port 9003

 

Browser, type:http://localhost:9001/hello/yfy

First Returns:hello yfy,welcome to Spring Cloud

The first return: `hello yfy, welcome to Spring Cloud: product2

Continue to carry out tests to go find two results appear alternately, that the two service centers automatically provides the functionality of load balancing services.

Guess you like

Origin blog.csdn.net/fy_java1995/article/details/94155943