eureka和feign简单配置

配置3个模块,

1.注册中心,

2.生产者

3.消费者

 

1.注册中心:

pom:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.2.RELEASE
         
    
    com.example
    erueka
    0.0.1-SNAPSHOT
    erueka
    Demo project for Spring Boot

    
        1.8
        Hoxton.SR6
    

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


配置文件:

server:
  port: 8091

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

启动类:

package com.example.erueka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EruekaApplication {

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

}

2.父工程



    4.0.0
    pom
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.2.RELEASE
         
    
    com.example
    demo728
    1.0.1-SNAPSHOT
    demo728
    Demo project for Spring Boot
    
        demo1
        demo3
    

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


3:生产者

pom:



    4.0.0
    
        demo728
        com.example
        1.0.1-SNAPSHOT
    
    com.example
    demo1
    1.0.1-SNAPSHOT
    demo1
    Demo project for Spring Boot

    
        1.8
    

    
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.SR6
                pom
                import
            
        
    


配置文件:

server:
  port: 8081
#注册到eureka客户端的名字
spring:
  application:
    name: demo1
#eureka相关实例
eureka:
  instance:
    lease-expiration-duration-in-seconds: 30
    lease-renewal-interval-in-seconds: 10
    #在eureka中显示ip地址
    prefer-ip-address: true
    ip-address: 127.0.0.1
  client:
    registerWithEureka: true
    fetchRegistry: true
    #eureka服务端的地址
    serviceUrl:
      defaultZone: http://localhost:8091/eureka/

启动类:

package com.example.demo1;

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
@EnableEurekaClient
@EnableFeignClients
public class Demo1Application {

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

}

controller:

package com.example.demo1.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Test1 {

    @GetMapping("/f1")
    String ff1(){
        return "123";
    }
}

4.消费者

pom:



    4.0.0
    
        demo728
        com.example
        1.0.1-SNAPSHOT
    
    com.example
    demo3
    0.0.1-SNAPSHOT
    demo3
    Demo project for Spring Boot

    
        1.8
    

    
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.SR6
                pom
                import
            
        
    


配置文件:

server:
  port: 8083
#注册到eureka客户端的名字
spring:
  application:
    name: demo3
#eureka相关实例
eureka:
  instance:
    lease-expiration-duration-in-seconds: 30
    lease-renewal-interval-in-seconds: 10
    #在eureka中显示ip地址
    prefer-ip-address: true
    ip-address: 127.0.0.1
  client:
    registerWithEureka: true
    fetchRegistry: true
    #eureka服务端的地址
    serviceUrl:
      defaultZone: http://localhost:8091/eureka/

启动类:

package com.example.demo3;

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
@EnableEurekaClient
@EnableFeignClients
public class Demo3Application {

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

}

调用生产者:

package com.example.demo3.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name = "demo1")
public interface Demo1Client {

    @GetMapping("/f1")
    String ff1();
}

 

5:zuul配置

pom:



    4.0.0
    
        demo728
        com.example
        1.0.1-SNAPSHOT
    
    com.example
    demo3
    0.0.1-SNAPSHOT
    demo3
    Demo project for Spring Boot

    
        1.8
    

    
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.SR6
                pom
                import
            
        
    


配置文件:

server:
  port: 8280

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8091/eureka
  instance:
    ip-address: 127.0.0.1
spring:
  application:
    name: zuul
zuul:
  sensitive-headers:
  routes:
    demo1: /demo1/**
    demo3: /demo3/**
    #zuul: /zuul/**

    host:
      socket-timeout-millis: 30000
      connect-timeout-millis: 30000

启动类:

package com.example.zuul;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {

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

}

 

参考:https://blog.csdn.net/nanbiebao6522/article/details/80574463

https://www.cnblogs.com/cl-rr/p/9644815.html

你可能感兴趣的:(典型demo)