related

  1. Spring Cloud Series 1 – Service Registration and Discovery Eureka

  2. Spring Cloud – Client calls Rest + Ribbon

  3. Spring Cloud Combat Series iii – Declarative client Feign

  4. Spring Cloud Series iv – Fuse Hystrix

  5. Spring Cloud Combat Series 5 – Service Gateway Zuul

  6. Spring Cloud Deployment Series (6) – Distributed Configuration center Spring Cloud Config

  7. Spring Cloud Combat Series (7) – Service Link Tracking Spring Cloud Sleuth

  8. Spring Cloud Combat Series (8) – Micro service monitoring Spring Boot Admin

  9. Spring Cloud OAuth 2.0

  10. Single sign-on JWT and Spring Security OAuth

preface

In this article, we introduce how to use the Eureka Service Registry to set up a simple server registration service. The clients can use the Ribbon load balancing to call service cases.

The body of the

1. Create a service provider

Create a service-hi Module with the following 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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3. RELEASE</version>
        <relativePath/> <! -- lookup parent from repository -->
    </parent>
    <groupId>io.ostenant.github.springcloud</groupId>
    <artifactId>service-hi</artifactId>
    <version>0.0.1 - the SNAPSHOT</version>
    <name>service-hi</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Dalston.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-cloud-starter-web</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>
</project>
Copy the code

Indicate that you are a EurekaClient by annotating @enableEurekaclient.

@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceHiApplication {
    public static void main(String[] args) { SpringApplication.run(ServiceHiApplication.class, args); }}Copy the code

Create a HelloController that provides a/HI HTTP service. Returns the port number of the current service instance in the response data.

@RestController
public class HelloController {

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

    @RequestMapping("/hi")
    public String hi(@RequestParam String name) {
        return "Hi " + name + ", I am from port: "+ port; }}Copy the code

The address of the service registry indicated in the configuration file, application.yml configuration file is as follows:

spring:
  active:
    profiles: service-hi1 # service-hi2

---

spring:
  profiles: service-hi1
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8763
spring:
  application:
    name: service-hi

---

spring:
  profiles: service-hi2
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8764
spring:
  application:
    name: service-hi
Copy the code

Active =hi1 and spring.profiles. Active =hi2 are used as the Boot command parameters of spring Boot respectively to start two service provider instances on port 8763 and 8764.

2. Create service consumers

Re-create the Service ribbon Module. In its pop. XML file, we introduce the starting dependency spring-cloud-starter-Eureka, spring-cloud-starter-ribbon, and spring-boot-starter-web, respectively. The code is as follows:

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3. RELEASE</version>
        <relativePath/> <! -- lookup parent from repository -->
    </parent>
    <groupId>io.ostenant.github.springcloud</groupId>
    <artifactId>service-ribbon</artifactId>
    <version>0.0.1 - the SNAPSHOT</version>
    <name>service-ribbon</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Dalston.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>
</project>
Copy the code

In the project configuration file, specify the address of the service registry for the service. The application name is service-ribbon and the port number is 8762. The complete configuration file application.yml is as follows:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8762
spring:
  application:
    name: service-ribbon
Copy the code

On the application startup class, register its information with the service center via @enableDiscoveryClient, register a RestTemplate object with the Spring Boot container, The @loadBalanced annotation indicates that this RestRemplate enables client load balancing.

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

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(a) {
        return newRestTemplate(); }}Copy the code

Write a test interface that injects the above RestTemplate object to consume the/HI interface of the service-hi service. In the URL of the RestTemplate, we replace the host name and port number of the specific service producer with its service name.

@RestController
public class HelloController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/hi")
    public String requestForHi(@RequestParam String name){
        return restTemplate.getForObject("http://SERVICE-HI/hi? name="+name,String.class); }}Copy the code

Visit http://localhost:8762/hi multiple times on your browser? Name =ribbon, alternate browser display:

Hi ribbon, I am from port: 8763

Hi ribbon, I am from port: 8764

During the call, the @loadBalanced annotation provided by the Ribbon queries the SERVICE list from the Eureka Server and finds the SERVICE instance of service-Hi based on the SERVICE name. If there are multiple service-Hi instances, the Ribbon filters one of them based on the client load balancing algorithm and replaces the service-HI in the URL with the host name and port number of the specific instance.

reference

  • An In-depth Understanding of Spring Cloud and Microservice Construction by Zhipeng Fang

Welcome to pay attention to the technical public number: Zero one Technology Stack

This account will continue to share learning materials and articles on back-end technologies, including virtual machine basics, multithreaded programming, high-performance frameworks, asynchronous, caching and messaging middleware, distributed and microservices, architecture learning and progression.