springcloud note 3 registration centers eureka, zookeeper and consumer

Posted by angelcool on Mon, 31 Jan 2022 20:44:52 +0100

1, eureka

1. Server

pom file

Add Eureka server

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

yaml file

# eureka clusters register with each other
server:
  port: 7002
#eureka:
#  instance:
#    hostname: eureka7002.com
#  client:
#    register-with-eureka: false
#    fetch-registry: false
#    com.fox.springcloud.service-url:
#      defaultzone: http://eureka7001.com:7001/eureka/
eureka:
  instance:
    hostname: eureka7002.com
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/
  server:
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 20000

Startup class

Add @ EnableEurekaServer annotation

2. Client (service registration + restTemplate)

pom file

Add Eureka client Maven coordinates

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

yaml file

eureka:
  client:
    register-with-eureka: true
    service-url:
#      defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
    fetch-registry: true
  instance:
    instance-id: payment8001
    prefer-ip-address: true
    lease-renewal-interval-in-seconds: 10
    lease-expiration-duration-in-seconds: 30

Startup class

Add @ EnableEurekaClient, @ EnableDiscoveryClient annotation

Note: @ EnableEurekaClient is only applicable to the scenario where Eureka is used as the registration center, @ EnableDiscoveryClient can be applicable to the scenario of other registration centers, such as nacos.

Call remote service

Use the service name instead of the ip port call of the service provider

Get the service of the registry

@Resource
private DiscoveryClient discoveryClient;

You can obtain the services of the registry and the corresponding instances through discoveryClient

3. Client (openfeign)

pom file

Add Eureka client and openfeign

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

yaml file

eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
feign:
  client:
    config:
      default:
        ReadTimeout: 5000
        ConnectTimeout: 5000

Startup class

Add @ EnableFeignClients annotation

Call remote service

The service name and interface path in PaymentFeignService should be consistent with that of the service provider. PaymentFeignService can be called by injecting PaymentFeignService into the project

@Component
@FeignClient("PAYMENT-SERVICE")
public interface PaymentFeignService {
    @GetMapping("/payment/lb")
    public String getPaymentLB();

    @GetMapping("/payment/timeout")
    public String getTimeout();
}

2, zookeeper

1.zookeeper server installation

Download zookeeper

wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.5.9/apache-zookeeper-3.5.9-bin.tar.gz

After installing zookeeper, check whether the firewall is closed

View firewall status
systemctl status firewalld.service
 Turn off the firewall
systemctl stop firewalld.service

When connecting to zookeeper, java projects should be consistent with the server version.

Enter the bin directory and start zookeeper

./zkServer.sh start
 connect zookeeper
./zkCli.sh

2. Client (service registration call)

pom file

		<!--SpringBoot integration Zookeeper client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            <exclusions>
                <!--Exclude the self-contained first zookeeper3.5.3-->
                <exclusion>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--add to zookeeper3.5.9 edition-->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.5.9</version>
        </dependency>

yaml file

spring:
  application:
    # Service alias --- the name of registering zookeeper to the registry
    name: cloud-provider-payment
  cloud:
    zookeeper:
      # Default localhost:2181
      connect-string: 192.168.137.200:2181

Startup class

Add @ EnableDiscoveryClient annotation

Call remote service

Replace ip port with service name

3, Consumer

1. Consumer server installation

The official website of consumer download and installation is very detailed https://www.consul.io/downloads

sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install consul

Check whether the installation is successful

consul -version

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-fzjpqgq6-1622675422322) (C: \ users \ 11244 \ appdata \ roaming \ typora user images \ image-20210523213447375. PNG)]

Start consumer

window start-up
consul agent dev
centos start-up(0.0.0.0 Allow remote access)
consul agent -dev -client 0.0.0.0 -ui &
Check whether the process started successfully
netstat -anp|grep 8500
 see Consul Members of the cluster
consul members

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-denc0mmf-1622675422332) (C: \ users \ 11244 \ appdata \ roaming \ typora \ typora user images \ image-20210523214456171. PNG)]

2. Client (service registration call)

pom file

Add consumer discovery

		<!--SpringCloud consul-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

yaml file

spring:
  application:
    # Service alias --- the name of the registered consumer to the registry
    name: cloud-provider-payment
  cloud:
    consul:
      host: 192.168.137.200
      port: 8500
      discovery:
        service-name: ${spring.application.name}

Startup class

Add @ EnableDiscoveryClient annotation

Call remote service

Replace the service name by ip

3. Problems encountered during use

Problem 1: the registered service provider cannot connect to the consumer

Error reporting using telnet ip port

telnet: connect to address 192.168.137.200: No route to host

View firewall status

firewall-cmd --state

It is found that the firewall is running. Close the firewall and telnet succeeds

service firewalld stop

Question 2: the service health status in consumer is unhealth

Page display fork

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-6t2sq6ud-1622675422325) (C: \ users \ 11244 \ appdata \ roaming \ typora user images \ image-20210530175405071. PNG)]

Click in

[the external chain image transfer fails. The source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-lj3anqzr-1622675422325) (C: \ users \ 11244 \ appdata \ roaming \ typora user images \ image-20210530175451398. PNG)]

Judging from the output information, it may be that the host name of the service runtime is used. Check that the host name is indeed DESKTOP-JOKG37G

127.0.0.1 DESKTOP-JOKG37G

The reason is that the spring cloud service is registered with the host name of the consumer, which is not recognized by the server where the consumer is located

resolvent:

1, Configure the host name mapping to the spring cloud service ip in the / etc/hosts file of the consumer server

2, Specify the ip address registered to the consumer spring cloud. consul. discovery. hostname

spring:
  application:
    name: cloud-consumer-order
  cloud:
    consul:
      host: 192.168.137.200
      port: 8500
      discovery:
        hostname: 192.168.137.166
        service-name: ${spring.application.name}
method:

1, In consul The server/etc/hosts The configuration hostname in the file is mapped to Springcloud service ip

2, Specify register to consul of ip address spring.cloud.consul.discovery.hostname

spring:
application:
name: cloud-consumer-order
cloud:
consul:
host: 192.168.137.200
port: 8500
discovery:
hostname: 192.168.137.166
service-name: ${spring.application.name}

Topics: Java Zookeeper eureka