springcloud+eureka+feign completes registration service and call

springcloud+eureka+feign completes the registration service and call, which can be used directly. The case is for the generator and the consumer to call each other
1. Build a test framework. Below is my

frame description

spring-starter-project parent directory
eurekaServer service registry
spring-consumer consumer contains own methods and call generator method
spring-producer producer contains own method and call consumer method
parent directory structure delete src file

parent directory pom file

<?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>2.1.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.waf</groupId>
	<artifactId>spring-starter-project</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	
	<modules>
		<module>eurekaServer</module>
		<module>spring-producer</module>
		<module>spring-consumer</module>
	</modules>

</project>

Registry Directory Structure

 

The registration center pom file, and the registration center serves as the server for all calling interfaces
 

<?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>com.waf</groupId>
		<artifactId>spring-starter-project</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>eurekaServer</artifactId>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>
	</dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

 

Registration center yml file
 


server:
  port: 3000
eureka:
  server:
    enable-self-preservation: false  #关闭自我保护机制
    eviction-interval-timer-in-ms: 4000 #设置清理间隔(单位:毫秒 默认是60*1000)
  instance:
    hostname: localhost


  client:
    registerWithEureka: false #不把自己作为一个客户端注册到自己身上
    fetchRegistry: false  #不需要从服务端获取注册信息(因为在这里自己就是服务端,而且已经禁用自己注册了)
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka

Registration Center Startup Class
 

package com.waf.work;

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);
	}

}

Request localhost:3000 after startup

 

Let's take a look at the generator

pom file
 

<?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>com.waf</groupId>
		<artifactId>spring-starter-project</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>spring-producer</artifactId>


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

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        
	</dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


</project>

yml file
 


server:
  port: 2000
eureka:
  client:
    serviceUrl:
        defaultZone: http://localhost:3000/eureka/  #eureka服务端提供的注册地址 参考服务端配置的这个路径
  instance:

    instance-id: order-1 #此实例注册到eureka服务端的唯一的实例ID
    prefer-ip-address: true #是否显示IP地址
    leaseRenewalIntervalInSeconds: 1 #eureka客户需要多长时间发送心跳给eureka服务器,表明它仍然活着,默认为30 秒 (与下面配置的单位都是秒)
    leaseExpirationDurationInSeconds: 3 #Eureka服务器在接收到实例的最后一次发出的心跳后,需要等待多久才可以将此实例删除,默认为90秒

spring:
  application:
    name: server-producer #此实例注册到eureka服务端的name

Start class
 

package com.waf.work;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient
public class SpringProducerApplication {

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

}

Producer custom interface
 

package com.waf.work.service;

public interface ProducerService {
	
	String getProducer();
	
	String getProducerImpl();

}

 

Interface implementation class
 

package com.waf.work.service.impl;

import org.springframework.stereotype.Service;

import com.waf.work.service.ProducerService;

@Service
public class ProducerServiceImpl implements ProducerService {

	@Override
	public String getProducer() {
		// TODO Auto-generated method stub
		String memo = "i am local producer server";
		return memo;
	}

	@Override
	public String getProducerImpl() {
		// TODO Auto-generated method stub
		String memo = "i am publish producer server";
		return memo;
	}

}

 

Call consumer interface
 

package com.waf.work.feignService;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient("server-consumer")//消费者注册中心服务名
public interface ConsumerService {
	
	@RequestMapping("/test-consumer/getConsumerImpl")//RequestMapping级别的请求路径,这个
//路径在消费者控制层需要有实现,请看后面的例子
	String getConsumerFeignService();

}

Control layer
 

package com.waf.work.controller;

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

import com.waf.work.feignService.ConsumerService;
import com.waf.work.service.impl.ProducerServiceImpl;

@RestController
@RequestMapping("/test-producer")
public class ProducerController {
	
	@Autowired
	private ProducerServiceImpl producerService;
	
	@Autowired
	private ConsumerService consumerService;
	
	@RequestMapping("/getLocalProducer")//自定义方法
	public String getProducer() {
		String producer = producerService.getProducer();
		return producer;
	}
	
	@RequestMapping("/getProducerImpl")//可看消费者调用接口图,上面的那个(最终请求的方法)
	public String getProducerImpl() {
		String producer = producerService.getProducerImpl();
		return producer;
	}
	
	@RequestMapping("/getConsumerImpl")//通过服务调用的消费者接口
	public String getFeignConsumerServer() {
		String service = consumerService.getConsumerFeignService();
		return service;
	}

}

Look at eureka after startup, the generator is successfully registered, if you need to call the consumer interface, please look down and start the consumer module

 

The consumer module interface is similar to the producer (demonstration only)

 

pom file
 

<?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>com.waf</groupId>
		<artifactId>spring-starter-project</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>spring-consumer</artifactId>
	<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>
		</dependency>
		
		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</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>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

yml file
 


server:
  port: 1000
eureka:
  client:
    serviceUrl:
        defaultZone: http://localhost:3000/eureka/  #eureka服务端提供的注册地址 参考服务端配置的这个路径
  instance:

    instance-id: product-1 #此实例注册到eureka服务端的唯一的实例ID
    prefer-ip-address: true #是否显示IP地址
    leaseRenewalIntervalInSeconds: 1 #eureka客户需要多长时间发送心跳给eureka服务器,表明它仍然活着,默认为30 秒 (与下面配置的单位都是秒)
    leaseExpirationDurationInSeconds: 3 #Eureka服务器在接收到实例的最后一次发出的心跳后,需要等待多久才可以将此实例删除,默认为90秒

spring:
  application:
    name: server-consumer #此实例注册到eureka服务端的name


 

Start class
 

package com.waf.work;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient
public class SpringConsumerApplication {

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

}

 

Interface and implementation class
 

package com.waf.work.service;

public interface ConsumerService {
	
	String getConsumer();
	
	String getConsumerImpl();

}


package com.waf.work.service.impl;

import org.springframework.stereotype.Service;

import com.waf.work.service.ConsumerService;

@Service
public class ConsumerServiceImpl implements ConsumerService {

	@Override
	public String getConsumer() {
		// TODO Auto-generated method stub
		String memo = "i am local consumer server";
		return memo;
	}

	@Override
	public String getConsumerImpl() {
		// TODO Auto-generated method stub
		String memo = "i am publish consumer server";
		return memo;
	}

}

Call the producer service interface
 

package com.waf.work.feignService;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient("server-producer")
public interface ProducerService {
	
	@RequestMapping("/test-producer/getProducerImpl")
	String getProducerFeignService();

}

Control layer
 

package com.waf.work.controller;

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

import com.waf.work.feignService.ProducerService;
import com.waf.work.service.impl.ConsumerServiceImpl;

@RestController
@RequestMapping("/test-consumer")
public class ConsumerController {
	
	@Autowired
	private ConsumerServiceImpl consumerService;
	
	@Autowired
	private ProducerService producerService;
	
	@RequestMapping("/getLocalConsumer")
	public String getconsumer() {
		String consumer = consumerService.getConsumer();
		return consumer;
	}
	
	@RequestMapping("/getConsumerImpl")
	public String getConsumerImpl() {
		String service = consumerService.getConsumerImpl();
		return service;
	}
	
	@RequestMapping("/getProducerImpl")
	public String getFeignProducerService() {
		String service = producerService.getProducerFeignService();
		return service;
	}

}

Start the consumer module again

 

So far the project is built, let’s look at the call request, the
producer calls the consumer

Let's look at the consumer call generator

 

A complete spring cloud eureka feign case is complete. Of course, you can redefine the published service yourself.

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_38108719/article/details/91351002