Springboot + Springcloud 配置一个服务多实例 负载均衡实现(fegin)

网上资料中可能会出先很多的问题  最终可是是pom里面的版本问题   贴上我实际运行的配置环境。


Server

pom文件


    UTF-8
    UTF-8
    1.8
    Dalston.RELEASE



    
        org.springframework.cloud
        spring-cloud-starter
    
    
        org.springframework.cloud
        spring-cloud-starter-eureka-server
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    



    
        
            org.springframework.cloud
            spring-cloud-dependencies
            ${spring-cloud.version}
            pom
            import
        
        
            org.springframework.boot
            spring-boot-starter-parent
            1.5.3.RELEASE
            pom
            import
        
    

application.yml配置

server:
  port: 8761
eureka:
  instance:
    hostname: 192.168.10.109
    prefer-ip-address: true
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/
spring:
  application:
    name: spring-cloud-eureka

Application.class类

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

client -----------生产者

pom文件


    UTF-8
    UTF-8
    1.8
    Dalston.RELEASE



    
        org.springframework.cloud
        spring-cloud-starter
    
    
        org.springframework.cloud
        spring-cloud-starter-eureka-server
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    



    
        
            org.springframework.cloud
            spring-cloud-dependencies
            ${spring-cloud.version}
            pom
            import
        
        
            org.springframework.boot
            spring-boot-starter-parent
            1.5.3.RELEASE
            pom
            import
        
    
application.yml配置文件

spring:
  application:
    name: service
server:
  port: 8762



eureka:
  client:
    serviceUrl:
      defaultZone: http://192.168.10.109:8761/eureka/



生产者的controller类也就是要发布的方法

@RestController
public class CloudController {

    @RequestMapping("/cloud")
    public String cloud(String name){
        return "hello"+name+"---->2";
    }
}


client可以使用同一个服务多个实例的方法 来解决单点故障问题   只要更改server.port端口重新启动一下  就可以创建另一个实例

Application AMIs Availability Zones Status
SERVICE-HI n/a (2) (2) UP (2) - YouXiong:service:8763 , YouXiong:service:8762
SERVICE-RIBBON n/a (2) (2) UP (2) - YouXiong , YouXiong:service-fegin:8764


application.class类

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


消费者  这时候就是要获取实例    得到数据  我这里是使用了Fegin所以需要导入相应jar包

pom.xml文件


    UTF-8
    UTF-8
    1.8
    Dalston.RELEASE



    
        org.springframework.cloud
        spring-cloud-starter
    
    
        org.springframework.cloud
        spring-cloud-starter-eureka-server
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    
    
        org.springframework.cloud
        spring-cloud-starter-feign
        1.3.1.RELEASE
    



    
        
            org.springframework.cloud
            spring-cloud-dependencies
            ${spring-cloud.version}
            pom
            import
        
        
            org.springframework.boot
            spring-boot-starter-parent
            1.5.3.RELEASE
            pom
            import
        
    

application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://192.168.10.109:8761/eureka/
server:
  port: 8764
spring:
  application:
    name: service-fegin

application.class类

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class Application {

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

接受实例的接口类  接口的方法要与发布服务的方法一样  由于它是通过URL进行访问所以value值要能访问到发布服务的方法    并且只能是通过GET请求服务

@FeignClient(value="SERVICE")
public interface CloudService {

    //方法名字和value要和发布服务的controller中的一样,并且请求的方式要设置成GET请求
    @RequestMapping(value="/cloud",method= RequestMethod.GET)
    String cloud(@RequestParam("name") String name);

}

消费者的controller类访问

@RestController
public class ControllerFegin {
    @Autowired
    public CloudService cloudService;

    @RequestMapping("/cloud/{name}")
    public String test(@PathVariable("name") String name){
        return cloudService.cloud(name);
    }
}

访问这个地址http://192.168.10.109:8764/cloud/aaa

就会出现负载均衡的方式来访问   他会通过轮询的方式来访问提供的服务

helloaaa---->1

helloaaa---->2

你可能感兴趣的:(springcloud,springboot,springcloud,负载均衡,一个服务多个实例)