Spring Cloud learning four Eureka component service governance

table of Contents

1. Version introduction

Two, Eureka introduction

3. Set up a service registration center

4. Registered service provider

5. High-availability registration center

6. Service discovery and consumption


1. Version introduction

SpringBoot version 2.1.9; SpringCloud version Greenwich.SR3

Two, Eureka introduction

Spring Cloud Eureka uses Netfix Eureka to implement service registration and discovery, which includes not only the server but also the client.

The Eureka server, also known as the service registry,

3. Set up a service registration center

To build a Springboot project, introduce dependencies as follows:

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

 Use the @EnableEurekaServer annotation to start the service registry and provide it to other applications for dialogue. The code is as follows:

@SpringBootApplication
public class EurekaServerApplication {

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

}

 By default, the service registry will also use itself as a client to try to register itself, so we have to disable this behavior; and the responsibility of the registry is to maintain service instances and does not need to retrieve services; so it needs to be in application. The properties are set as follows:

server.port=8888
eureka.instance.hostname=localhost
#禁用注册自己
eureka.client.register-with-eureka=false
#禁用检索
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

 After startup, visit: http://localhost:8888/  ; you can view Eureka's information board, as shown below:

 

4. Registered service provider

Build a springBoot application to join Eureka's service governance system.

Introduce Eureka dependency and web dependency in the new project, as follows:

        <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-web</artifactId>
		</dependency>

Create IndexController, interface index, add the annotation @EnableDiscoveryClient to the startup class by injecting the DiscoveryClient object, and print the service information; as follows:

@RestController
@ResponseBody
public class IndexController {
    @Autowired
    private DiscoveryClient client;
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(){
        List<ServiceInstance> indexService = client.getInstances("indexService");
        for (ServiceInstance ServiceInstance :
                indexService) {
            System.out.println("host:"+ServiceInstance.getHost()+"serviceId:"+ServiceInstance.getServiceId()+
                    "scheme:"+ServiceInstance.getScheme()+"instanceId:"+ServiceInstance.getInstanceId());
        }
        return "Hello Word";
    }
}

 

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaServerApplication {

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

You also need to name the service in the application.properties file through spring.application, and specify the registry address through eureka.client.service-url.defaultZone; as follows:

server.port=8080
spring.application.name=indexService
eureka.client.service-url.defaultZone = http://localhost:8888/eureka/

After the service is started, you can visit http://localhost:8888/ to  view the registration information of the service in the Instances currently registered with Eureka column; as shown below:

5. High-availability registration center

In a distributed environment such as a microservice architecture, we have to consider failures, so the deployment of registry services must be highly available, that is, to build a cluster of highly available service registry. In the design of EurekaServer, all nodes are both service providers and service consumers, as is the registry. We previously set eureka.client.register-with-eureka=false #disable retrieval eureka.client.fetch-registry=false The parameter is to prevent the service from registering itself. The high-availability EurekaServer actually registers itself as a service with other registries, so that a set of mutually registered service registries can be formed to realize the mutual concession of the service list and achieve the effect of high availability.

Create application-peer1.properties as the configuration of the service center of peer1, and point eureka.client.service-url.defaultZone to peer2; as follows:

spring.application.name=eurekaServer
server.port=9999
eureka.instance.hostname=peer1
eureka.client.service-url.defaultZone=http://peer2:1111/eureka/

 Create application-peer2.properties as the configuration of the service center of peer1, and point eureka.client.service-url.defaultZone to peer1; as follows:

spring.application.name=eurekaServer
server.port=1111
eureka.instance.hostname=peer2
eureka.client.service-url.defaultZone=http://peer1:9999/eureka/

Add the conversion of peer1 and peer2 to the hosts file in the C:\Windows\System32\drivers\etc directory; add the following:

127.0.0.1  peer1
127.0.0.1  peer2

 Then start peer1 and peer2 respectively through the spring.profiles.active property:

##*号代表你的包名
java -jar *******  --spring.profiles.active=peer1
java -jar *******  --spring.profiles.active=peer2

 Visit http://localhost:9999/ or http://localhost:1111/ and  you can see that the registered-replicas has the service of peer2 or peer1 node, as follows

By adding the following configuration to the configuration file of the service provider, the service can be registered to the Eureka cluster:

server.port=8080
spring.application.name=indexService
eureka.client.service-url.defaultZone =http://peer1:9999/eureka/,http://peer2:1111/eureka/

 Visit http://localhost:9999/ or http://localhost:1111/ again, you can see the following picture, the service indexService is registered in peer1 and peer2.

6. Service discovery and consumption

First, we use the command to start the indexService service with port numbers 8081 and 8082 respectively;

Java -Jar  *******.jar  --server.port=8081
Java -Jar  *******.jar  --server.port=8082

Create a project to implement service consumers, named ribbon-consumer, and introduce dependencies on web, eureka and ribbon; the pom file is as follows:

        <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.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
		</dependency>

Add the @EnableDiscoveryClient annotation to the main class to register the application as a client; at the same time, create a springbean instance of RestTemplate in the main class, and enable client load balancing through the @LoadBalanced annotation.

@EnableDiscoveryClient
@SpringBootApplication
public class RibbonConsumerApplication {

	@Bean
	@LoadBalanced
	RestTemplate restTemplate(){
		return new RestTemplate();
	};
	public static void main(String[] args) {
		SpringApplication.run(RibbonConsumerApplication.class, args);
	}

}

Create the ConsumerController class and call the indexService service with restTemplate; the code is as follows:

@RestController
public class ConsumerController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value = "/ribbon-consumer",method = RequestMethod.GET)
    public String helloConsumer(){
        return restTemplate.getForEntity("http://indexService/index",String.class).getBody();
    }
}

Configure the location of the eureka service registry in application.properties, which needs to be the same as indexService; as follows:

spring.application.name=ribbon-consumer
server.port=9000
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/

After starting the ribbon-consumer service, visit http://localhost:1111/ to see the information of the service;

By initiating a request to http://localhost:9000/ribbon-consumer , helloWord will be returned successfully. And see the following information in the ribbon-consumer service console:

Some information will be printed alternately on the two indexService consoles:

 

Guess you like

Origin blog.csdn.net/weixin_42228950/article/details/102639820